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();
}
}
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.
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