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++) | |
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
User Post
Weasel
Missionary in Peru
Level: 34

Posts: 74/454
EXP: 236444
For next: 17207

Since: 03-15-04
From: Washington

Since last post: 467 days
Last activity: 339 days
Posted on 03-22-04 08:30 AM Link | Quote
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?
Cellar Dweller

Flurry
!!!
Level: 27

Posts: 14/269
EXP: 107817
For next: 8342

Since: 03-15-04
From: Arkansas

Since last post: 16 days
Last activity: 34 min.
Posted on 03-22-04 09:56 AM Link | Quote
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();
}
Kenny3900
Newcomer
Level: 6

Posts: 5/9
EXP: 659
For next: 248

Since: 03-15-04

Since last post: 576 days
Last activity: 339 days
Posted on 03-22-04 02:15 PM Link | Quote
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.
Add to favorites | "RSS" Feed | Next newer thread | Next older thread
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.012 seconds.