Answers for "check if cin didn't get int"

C++
0

check if cin didn't get int

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(), '\n'); //discard input
    std::cout << "Invalid input; please re-enter.\n";
}
Posted by: Guest on September-03-2021

Browse Popular Code Answers by Language