If you
need to check the input for non-number values, you'll have to take the input as a string and use
atof()
(the radius might not be an integer). That, however, has the problem that the "error" value for
atof()
is 0, meaning that you don't actually know if the input was a character or 0.
Add to that the question of whether or not your teacher is a malevolent son of a gun and input something like
12X.5
. This would still be an invalid input, but would not necessarily be caught by
atof()
(or
atoi()
), which, in such situations, tend to read the numbers until an invalid character (meaning the value returned by
atof()
in my example might be 123.00000).
The only really secure way of checking for incompatible input is by taking the input as a string and manually going through each character with a for() loop checking for
1 2 3 4 5
|
if((str[i]>'9' || str[i]<'0') && str[i]!='.')
{
cout << "Gimme a number, foo'!";
break;
}
|