This is the way I like to recommend. It evaluates the return value of the input, which will be false if the user enters something that is not a number. It can be expanded to fail if the user enters a number followed by a letter if you want to do that (e.g. you want to fail on 123abc), just ask or search.
Code:
int number = 0;
// ask user for number here
while (!(std::cin >> number))
{
std::cin.clear(); // clear fail state
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n'); // ignore bad input
// warn user and re-prompt here
}
Note that you need to #include <limits> and <ios> for numeric_limits and streamsize. You can also just use some big number inside the ignore like this:
Code:
std::cin.ignore(1000, '\n'); // ignore bad input