python - Check if input is integer -
in order learn c++, i'm translating program wrote in python.
i wrote this
n = 0 while n < 2: try: n = int(raw_input('please insert integer bigger 1: ')) except valueerror: print 'error!' in order integer bigger 1 user.
this wrote in c++ moment:
int n = 0; while (n < 2) { cout << "please insert integer bigger 1: "; cin >> n; } i took @ try-catch , seems pretty straight forward. concern how check input integer. read cin.fail() couldn't find official document , didn't how works.
so, how can check if input integer?
more in general, how can check if input "anything"?
for situation this, you'd want read input string, inspect string (e.g., "contains digits, maximum of n digits"). if , if passes inspection, parse int out of it.
it's possible combine inspection , conversion--for example, boost lexical_cast<int>(your_string) attempt parse int out of string, , throw exception if can't convert whole thing int.
Comments
Post a Comment