#include <iostream>
#include <string>
using namespace std;
int main()
{
string world;
while (getline(cin, world))
cout << world << endl;
system("pause");
return 0;
}
getine(cin,s)是将cin中的一行(以回车换行为结束标志)赋给s,返回cin。
在上面的程序中,如果cin的是 hello world回车,那么就会输出hello world并换行(endl)。
但是如果这样hello world^z再回车,会没有输出显示hello world的,而是光标换行继续等待你的输入。
此时我在输入^z,此时就会显示出来hello world但是后面会有一个方框代表无法显示。并且结束循环。
结论:当有while(cin>>)或者是类似的循环时,只有ctrl+z单独输入时才可以结束输入流。
当ctrl+z跟在其它后面字符后面时,会被认为也是一个字符(无法显示),就会打印出来一个方框。