C++Primer(第4版)简单问题之一

来源:百度知道 编辑:UC知道 时间:2024/09/22 12:40:09
C++Primer(第4版)第71页的以下程序:

#include <string>
#include "iostream"

using std::string;
using std::cin;
using std::cout;
using std::endl;

int main()
{
string word;
// read until end-of-file, writing each word to a new line
while (cin >> word)
cout << word << endl;
return 0;
}

如何能够正常退出循环?
请仁兄调试程序后,回答!

while(cin>>word)
等待输入后检测流cin的状态,如果有效就继续.
想要退出循环就要让cin变成状态无效,有以下几种办法:
1.让程序出错,
2.非法输入,但是对于string,不存在可能的非法输入
3.键入eof.也就是ctrl+z

cin >> word
1.读取出错,表达式的值为非正
2.读不到东西了,也就是到了文件尾,表达式为非正

ctrl+z
再按两次回车就OK了,我就奇怪了,刚调试了,就是这样的

ctrl+Z我以前也不会,学的

ctrl+z