Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c++

stdin

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();
    }
}
like image 736
user1232138 Avatar asked Dec 28 '22 02:12

user1232138


2 Answers

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

like image 41
Tony Hopkinson Avatar answered Jan 05 '23 14:01

Tony Hopkinson


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.

like image 151
sepp2k Avatar answered Jan 05 '23 16:01

sepp2k