#include <iostream>
using std::cin;
using std::cout;
int main()
{
cout << "Please type some numbers separated by spaces or semicolons.\n";
cout << "Type CTRL+Z followed by ENTER on windows (or CTRL+D on unix) to exit.\n";
for (;;) {
int value = 0;
if ( !( cin >> value ) ) {
// cin is not good, more checks required.
// NOTE: the value is likely to contain garbage!
if ( cin.bad() ) {
cout << "cin is corrupted, exiting the program.\n";
return 1; // signal error back to the operating system
}
if ( cin.eof() ) {
cout << "EOF found, exiting the program.\n";
return 0; // signal success to the operating system
}
if ( cin.fail() ) {
cin.clear(); // go back to good state.
// do something to recover...
char ch = 0;
if ( cin >> ch ) {
if ( ch == ';' ) {
// semicolon is data delimiter, and can be ignored:
continue;
}
cout << "Sorry, character [" << ch << "] was unexpected at this time. Exiting...\n";
break;
}
}
}
cout << "You entered: " << value << '\n';
}//for
return 0;
}