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

C++
1

c++ 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