How to check if the input is a valid integer without any other chars?

You could read a string, extract an integer from it and then make sure there’s nothing left:

std::string line;
std::cin >> line;
std::istringstream s(line);
int x;
if (!(s >> x)) {
  // Error, not a number
}
char c;
if (s >> c) {
  // Error, there was something past the number
}

Leave a Comment