Acmlm's Board - I2 Archive - Programming - Testing if a value is a number or not... (c++)
User | Post |
Kenny3900
Posts: 5/9 |
I think if the user is stupid enough to enter a non-numerical data (), an error will be given out.
So use try/catch may help. |
Cellar Dweller
Posts: 14/269 |
if you don't mind falling back on C library functions you could input a string and process it with strtod() . The code should look something like this:
char *endptr;
double_var = strtod(input_string_ptr, &endptr); if (endptr == input_string_ptr) { punish_user_severely(); }
|
Weasel
Posts: 74/454 |
OK, here's a code fragment:
double variable; cin >> variable;
Gee, that was a lot, eh?
Let's say I have a stupid user that, instead of entering a real number into that fine variable, he enters a character, like - or \ or something stupid.
I want to have the program stop if this happens. I need some sort of test to see if the value entered is a number and not some other type like a string or character.
Any ideas on how to do this? |
|