It is easy to use a while loop to check for the int and prompt the user to try again if it is not a vali integer.
Code:
int value = 0;
// Prompt for input
while (!(std::cin >> value))
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// Tell the user they typed in bad input here and prompt again.
}
This is simple and works in most every case. One case where it might not work is if you want it to error out when the user types extra characters within or after the number (e.g. 1234abc or 123r5). To test for that, simply change the while loop to:
Code:
int value = 0;
// Prompt for input
while (!(std::cin >> value) || std::cin.get() != '\n')
{
std::cin.clear();
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// Tell the user they typed in bad input here and prompt again.
}
Note that you have to #include <limits> to use numeric_limits. You can also just use a large number there.