Resume reading from iostream::cin after Ctrl+Z (EOF)? (“ignore” doesn't work)

纵然是瞬间 提交于 2019-11-28 08:16:40

问题


Why does the outer loop in the following program terminate when we provide ctrl+z for the inner loop only?

#include<iostream>
int main()
{
    string s1,s2;

    while(cin >> s1)
    {
        cout<<"In loop1\n";
        while(cin>>s2)
            cout<<"In loop 2\n";
        cin.ignore();
    }
}

回答1:


Hitting Ctrl+z (on Windows) closes the standard input stream. Once it's closed, it stays closed. It doesn't magically reopen once the inner loop is finished. There's just no reason why it would.




回答2:


Ctrl-Z puts cin into an error state so cin.ignore does nowt. try cin.Clear() instead.



来源:https://stackoverflow.com/questions/10147996/resume-reading-from-iostreamcin-after-ctrlz-eof-ignore-doesnt-work

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!