Register | Login | |||||
Main
| Memberlist
| Active users
| ACS
| Commons
| Calendar
| Online users Ranks | FAQ | Color Chart | Photo album | IRC Chat |
| |
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 |
| ||
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. |
| ||
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:
|
|||
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 |
| ||
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++) | | | |