If you are not terminating the program by returning end of file (i.e. Ctrl-D
) or terminating the program altogether (i.e. Ctrl-C
).
That is, if you exit the loop via incorrect data type, such as typing in the letter d
instead of an integer, you can follow the while
loop with cin.clear()
and getline(cin, str)
, where str
is some string
you declare ahead of time.
You should be able to accept input for the second cin
at after this.
So,
string str;
int n;
while(cin>>n)
cout << n << endl;
cin.clear();
getline(cin, str);
cout << "Break from loop" << endl;;
cin >> n;
cout << n;