check if cin got the wrong type

Solutions on MaxInterview for check if cin got the wrong type by the best coders in the world

showing results for - "check if cin got the wrong type"
Erica
21 Apr 2020
1while (std::cout << "Enter a number" && !(std::cin >> num)) {
2    std::cin.clear(); //clear bad input flag
3    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); //discard input
4    std::cout << "Invalid input; please re-enter.\n";
5}