为什么while(cin>>word)达不大要求?
测试程序如下:
#include <iostream>
#include <string>
using namespace std;
int main(){
string word;
while(cin>>word)
cout>>word<<endl;
system("pause");
return 0;
}
---------------------------
为什么会无限循环?
当输入到串尾后,循环判断应该为假的啊
推荐阅读
按 ctrl+z 或者 ctrl+c 或者 ctrl+break才会返回false;
while(cin>>word)
改为while(cin>>word)
{
if (word == \n)
break;
你的代码!
}
否则死循环!
测试程序如下:
#include <iostream>
#include <string>
using namespace std;
int main(){
string word;
while(cin>>word)
cout>>word<<endl;---------此处笔误cou<<word<<endl;
system("pause");
return 0;
}
---------------------------
为什么会无限循环?
当输入到串尾后,循环判断应该为假的啊
可是当用于文件的输入输出时确是可以的啊
//测试程序如下:我现在在网吧,中间有语法问题,还望大家见谅
#include <fstream>
#include <string>
using namespace std;
int main(){
fstream fp("1.txt",out);
string word;
while(fp>>word)
cout<<word<endl;
system("pause");
return 0;
}
使用ctrl+z 就可以产生一个 eof使其结束。
如果只想输入一个字符串使用:
cin>>word;
就可以了,不用循环。


讨论区