I am a (somewhat) experienced python programmer trying to migrate to C++. I want to be able to catch invalid user input. For instance, if a variable requires an integer, and
Streams don't throw exceptions by default (they can be configured to do so).
The usual code to read an int is:
int a;
if (std::cin >> a) {
# OK
} else {
# error
}
But do be sure that you know what >> actually does here. Specifically, newcomers to C++ are sometimes surprised that it doesn't necessarily read a whole line. So, if you want to validate a whole line of input, or for that matter if you want to retry on failure then you need more code.