// John Bent's CS 110 Hand-out 16 // Input error checking // // this code is a loop which does not exit until an integer value has // been entered, it makes sure that an integer and not a real number has // been entered and it clears the buffer if a character was entered #include int main() { int I; double D; cout << "\n\nEnter an integer: "; cin >> D; // input the double I = (int) D; // assign it to the integer while (cin.fail() || I != D) { // if it is not a number or if the // truncated integer is not the same cin.clear(); while (cin.get() != '\n') ; // clear the buffer cout << "\nI'm sorry, that value is no good. " << "\nEnter an integer: "; cin >> D; // get the double I = (int) D; // assign it to the int } cout << "\nInteger successfully obtained with a value of " << D << endl << endl; return 0; }