关于PTA中C++不能用gets

简介: pta中用c++时如果使用gets将会显示编译错误

pta中用c++时如果使用gets将会显示编译错误

此时可以用cin.getlin(a,10005);代替gets(a);

引入例题


1-7 A-B (20 分)


本题要求你计算A−B。不过麻烦的是,A和B都是字符串 —— 即从字符串A中把字符串B所包含的字符全删掉,剩下的字符组成的就是字符串A−B。

输入格式:

输入在2行中先后给出字符串A和B。两字符串的长度都不超过104,并且保证每个字符串都是由可见的ASCII码和空白字符组成,最后以换行符结束。

输出格式:

在一行中打印出A−B的结果字符串。

输入样例:

I love GPLT!  It's a fun game!

aeiou

输出样例:

I lv GPLT!  It's  fn gm!

错误示范

#include<bits/stdc++.h>
using namespace std;
int main(){
 char a[10005];
char b[10005];
gets(a);//只会遇到换行符时停止读入但不会读入换行符
gets(b);
int i,j;
int len=strlen(a);
int len1=strlen(b);
for(i=0;i<len;i++){
  for(j=0;j<len1;j++){
    if(a[i]==b[j])break;
  }
  if(j==len1)printf("%c",a[i]);
}
  return 0;
}

微信截图_20220414141906.png

微信截图_20220414141906.png

进行cin.getline修改后

#include<bits/stdc++.h>
using namespace std;
int main(){
 char a[10005];
char b[10005];
cin.getline(a,10005);//只会遇到换行符时停止读入但不会读入换行符
cin.getline(b,10005);
int i,j;
int len=strlen(a);
int len1=strlen(b);
for(i=0;i<len;i++){
  for(j=0;j<len1;j++){
    if(a[i]==b[j])break;
  }
  if(j==len1)printf("%c",a[i]);
}
  return 0;
}

微信截图_20220414142027.png微信截图_20220414142036.png


相关文章
|
3月前
|
C++
【PTA】L1-016 验证身份(C++)
【PTA】L1-016 验证身份(C++)
44 0
【PTA】L1-016 验证身份(C++)
|
3月前
|
C++
【PTA】L1-011 A-B (C++)
【PTA】L1-011 A-B (C++)
60 0
【PTA】L1-011 A-B (C++)
|
3月前
|
C++
【PTA】​L1-005 考试座位号​ (C++)
【PTA】​L1-005 考试座位号​ (C++)
71 0
【PTA】​L1-005 考试座位号​ (C++)
|
3月前
|
测试技术 C++
【PTA】​L1-003 个位数统计​ (C++)
【PTA】​L1-003 个位数统计​ (C++)
46 0
【PTA】​L1-003 个位数统计​ (C++)
|
3月前
|
C++
【PTA】L1-020 帅到没朋友 (C++)
【PTA】L1-020 帅到没朋友 (C++)
58 0
【PTA】L1-020 帅到没朋友 (C++)
|
3月前
|
C++
【PTA】​ L1-080 乘法口诀数列​(C++)
【PTA】​ L1-080 乘法口诀数列​(C++)
53 0
【PTA】​ L1-080 乘法口诀数列​(C++)
|
3月前
|
C++
【PTA】​L1-078 吉老师的回归​(C++)
【PTA】​L1-078 吉老师的回归​(C++)
73 0
【PTA】​L1-078 吉老师的回归​(C++)
|
3月前
|
C++
【PTA】​L1-079 天梯赛的善良​ (C++)
【PTA】​L1-079 天梯赛的善良​ (C++)
72 0
【PTA】​L1-079 天梯赛的善良​ (C++)
|
3月前
|
C++
【PTA】​ L1-077 大笨钟的心情​(C++)
【PTA】​ L1-077 大笨钟的心情​(C++)
77 0
【PTA】​ L1-077 大笨钟的心情​(C++)
|
3月前
|
C++
【PTA】​ L1-070 吃火锅​(C++)
【PTA】​ L1-070 吃火锅​(C++)
103 0
【PTA】​ L1-070 吃火锅​(C++)