Answers for "check if cin got the wrong type"

C++
0

check if cin got the wrong type

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