I prefer to use <limits>
to check for an int
until it is passed.
#include <iostream> #include <limits> //std::numeric_limits using std::cout, std::endl, std::cin; int main() { int num; while(!(cin >> num)){ //check the Input format for integer the right way cin.clear(); cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); cout << "Invalid input. Reenter the number: "; }; cout << "output= " << num << endl; return 0; }