io/copy-is-os-3.cc - Illustration of implicit stream conversion to a boolean value - and stream states. | Lecture 3 - slide 19 : 27 Program 1 |
// Try it... #include <iostream> #include <string> #include <limits> using namespace std; int main(){ int i; while (true){ cout << "Enter a number: "; cin >> i; // cin is converted to 'a boolean value' // via an implicit conversion operator - while cin is in a good state cout << "We got: " << i << " " << endl; if (cin.good()) cout << "cin is good" << endl; else if (cin.eof()) cout << "cin is at EOF" << endl; // when typing Cntrl D else if (cin.fail()) cout << "cin has failed" << endl; // when typing a non-digit else if (cin.bad()) cout << "cin is in really bad shape" << endl; cin.clear(); cin.ignore(numeric_limits<streamsize>::max(), '\n'); // flush input } }