Register | Login
Views: 19364387
Main | Memberlist | Active users | ACS | Commons | Calendar | Online users
Ranks | FAQ | Color Chart | Photo album | IRC Chat
11-02-05 12:59 PM
0 user currently in Programming. | 3 guests
Acmlm's Board - I2 Archive - Programming - Testing if a value is a number or not... (c++)
  
User name:
Password:
Reply:
 

UserPost
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?
Acmlm's Board - I2 Archive - Programming - Testing if a value is a number or not... (c++)


ABII


AcmlmBoard vl.ol (11-01-05)
© 2000-2005 Acmlm, Emuz, et al



Page rendered in 0.010 seconds.