In order to learn C++, I'm translating a program I wrote in Python.
I wrote this
n = 0
while n < 2:
try:
n = int(raw_input('Please insert an integer bigger than 1: '))
except ValueError:
print 'ERROR!'
in order to get an integer bigger than 1 from the user.
This is what I wrote in C++ for the moment:
int n = 0;
while (n < 2) {
cout << "Please insert an integer bigger than 1: ";
cin >> n;
}
I took a look at try-catch and it seems pretty straight forward. My concern is about how to check the input is an integer. I read about cin.fail() but I couldn't find any official document and I didn't really get how it works.
So, how can I check if the input is integer?
More in general, how can I check if the input is "anything"?