| validing incoming data |
[07 Feb 2006|04:55pm] |
Hi everyone. I'm rather new in C++ and I'm running into a problem with something I'm trying to attempt.
Basically here's what I'm trying to do:
I'm asking the user to input an integer number, up to 100 numbers, to be stored into an integer array. No problem. The problem is that I want it to break the loop and stop asking for numbers if anything ELSE is entered (be it string, float, char, short, whatever). That's where I'm having problems.
At first I thouht I could do it by comparing the footprints of the incoming number, as a character uses less bytes than an int, which uses less than a float, and so on. I figured "well, if they don't match then it's not an integer, boom I'm out of the loop." But no, it's not working. In fact, it's producing crazy results in which I type in a character, or a float/double/whatever and it basically loops the last number over and over until the array is filled.
Does anyone know of a way to help me fix this problem?
( The old codeCollapse )
Of course I'm going to be doing some operations on the array once I figure out how to make the loop break when a non-integer is entered.
Thanks for any help. =========
Edit:
My thanks goes to coises. I eventually decided to allow floating point numbers with a warning that they would result in a loss of accuracy, as I'll be dropping the decimal point and casting it into an int. I really don't want to deal with strings and conversions or checking every digit that comes in to make sure it's within 0 to 9, and I think this will be easier. I mean I have it say "INTEGER" at least two or three times before the first number is even entered. But it was coises who helped me get it working so that it'd stop if a nondigit was entered.
( the fixed codeCollapse )
|
|