#include <iostream> #include <limits> using namespace std; int main() { double input = -1; bool valid= false; do { cout << "Enter a number: " << flush; cin >> input; if (cin.good()) { //everything went well, we'll get out of the loop en return the value valid = true; } else { //something went wrong, we reset the buffer's state to good cin.clear(); //and empty it cin.ignore(numeric_limits<streamsize>::max(),'\n'); cout << "Invalid input; please re-enter." << endl; } } while (!valid); }