#include // Ivan Novick // May 20, 2009 // C++ code to detect and ignore bad input from cin int main(int argc, char** argv){ int number; while(1) { std::cout << "Enter a number and I will multiply it by 2: "; std::cin >> number; // here is an example of how to detect bad input when expecting // a number and getting a char or some other input if (!std::cin){ std::cin.clear(); while(std::cin.get()!='\n'){ continue; } std::cout << "bad input detected" << std::endl; continue; } if (!number){ break; // no more input } std::cout << "The result is: " << number * 2 << std::endl; } return 0; }