while (std::cout << "Enter a number" && !(std::cin >> num)) { std::cin.clear(); //clear bad input flag std::cin.ignore(std::numeric_limits<std::streamsize>::max(), ' '); //discard input std::cout << "Invalid input; please re-enter. "; }