Advertisement

DOS input question

Started by February 12, 2000 01:27 PM
0 comments, last by Bigshot 24 years, 7 months ago
I do Windows programming almost exclusively now, but I wanted to know something about good old DOS. If I''m using ''scanf'' or ''cin'' and I''m trying to input a number, how can I check whether the user typed something besides a number? Without error checking the program would go ape-shit. I know it''s very simple, but I never learned how to error check input that used cin or scanf. I no longer program in DOS but I''d like to know the solution to this problem anyway. Thanks, Al
Alexbigshot@austin.rr.comFoolish man give wife grand piano. Wise man give wife upright organ.
The easiest and most robust to do line-based input, is to first read everything into a string variable, then parse that. Then, if you realize that you got something you didn''t want, you haven''t lost it. As for parsing to look for numbers, I''d suggest using the standard string class, and check out some of the following functions:

atoi(), atof(), atol()
string.find_[first/last]_[of/notof]()

For example, you can use string.find_first_of() to locate the first whitespace character on the line, then use atol() to convert it to a number. It will tell you if no legal number was contained there.

-Brian

This topic is closed to new replies.

Advertisement