能帮忙看一下哪儿错了吗
#include <bits/stdc++.h>
using namespace std;
int main(){
char txt[200],a,b;
int i,ntxt=0;
while((txt[ntxt++]=getchar())!='\n');
a=getchar();
getchar();
b=getchar();
for(i=0;i<ntxt;i++){
if(txt[i]==a){
cout<<b;
}
else{
cout<<txt[i];
}
}
cout<<endl;
return 0;
}
你的while循环里没有语句啊,应该用大括号把语句括起来
你的代码想要实现什么?
不知道你这个问题是否已经解决, 如果还没有解决的话:问题分析: 1. 头文件应使用#include <iostream>
而不是#include <bits/stdc++.h>
。 2. 应明确指定使用std
命名空间,而不是使用using namespace std;
。 3. 数组txt
的大小应足够大,以容纳输入的文本内容,例如char txt[201]
。 4. 在读取输入文本时,应该在输入的最后添加一个字符串结束标志,可以将while
循环中的条件改为(txt[ntxt++]=getchar())!='\n' && ntxt < 200
。 5. 在接收a
、b
的输入时,应确保只接收一个字符,可以使用cin.get(a)
和cin.get(b)
。 6. 在使用cout
输出时,应将输出内容用双引号括起来,例如cout << txt[i];
改为cout << txt[i] << "";
。 7. 在输出完成后,应该换行,可以使用cout << endl;
。
代码修改如下:
#include <iostream>
using namespace std;
int main() {
char txt[201], a, b;
int i, ntxt = 0;
while ((txt[ntxt++] = getchar()) != '\n' && ntxt < 200);
cin.get(a);
cin.ignore();
cin.get(b);
for (i = 0; i < ntxt; i++) {
if (txt[i] == a) {
cout << b;
}
else {
cout << txt[i] << "";
}
}
cout << endl;
return 0;
}
这样修改后,代码中的错误应该可以解决。