c++ - 为什么eof ctrl+d模拟工作不正常?
大家讲道理
大家讲道理 2017-04-17 11:41:31
0
2
325

下面一段C++测试代码,while loop使用eof来跳出循环。

string s1;
while (cin >> s1)
{
    cout << s1 << endl;
}
// cout << cin << cin << cin << endl; //加入输出 000
// cin.ignore(10000,'0'); //加入无效
// cin.clear() // 加入无效

string s3, s4;
cout << "input two strings: " << endl;
getline(cin, s3);
getline(cin, s4);
if (s3 == s4){
    cout << "the strings are equal!" << endl;
}
else if (s3 > s4) {
    cout << "s3 is larger!" << endl;
} else {
    cout << "s4 is larger!" << endl;
}

在mac终端运行结果如下,粗体为我的输入,斜体为cout打印。
可以看到要求输入两string的时候压根就没有输入机会就玩儿蛋了。
hello world
hello
world
input two strings:
the strings are equal!

另外加入注释代码也不能让cin 重新工作。
如果注释貂s1 相关循环代码,s3 s4测试代码可以真常工作。

请问这是为什么?mac 终端 ctrl+d 模拟eof 之后需要做什么特殊设置才能工作吗?谢谢。

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!