verifying an input

I have an assignment to create a program that gives the area of a circle given a radius. But the thing is we are supposed to verify that the entered radius is valid, so no negatives or non-numbers like letters.

I used an if/else statement to make sure its positive but i don't know how to make the program stop if you input a letter. right now if i put a letter in for the radius it gives me 0 for the area when its not supposed to run at all.

i really need help, any advice is appreciated. Thanks
Try a simple while loop. Check if it's correct and get input again.
1
2
3
4
5
6
cin >> someint;
while (someint != 10)
{
    cout << "YOUR INTEGER MUST BE 10";
    cin >> someint;
}

If you are worried about C++ casting say, chars to integers, I have no solution. Sorry.
EDIT: You could get a character instead and then atoi (ascii to integer, I believe it's in cctype) it, but I don't consider that efficient enough to be worth it.
Last edited on
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;
}
That is not the only way. In C++, it is typically fine to use something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
cout << "Please enter an integer in the range [19, 37]: " << flush;
int n;
while (true)
  {
  cin >> n;

  if ((cin) && (n >= 19) && (n <= 37))
    break;

  cin.clear();
  cin.ignore( 1000, '\n' );
  cout << "Hey, follow instructions please: " << flush;
  }
cout << "Good job!\nYou entered the number '" << n << "'.\n";

Hope this helps.

[edit] Fixed typo in code above
Last edited on
There was an article on this, however I'm having trouble finding it :(
The method I use for this is to read the user input into a string with something like this:

1
2
std::string input;
getline(cin, input);


After that, one can use an istringstream object to read that into the numeric value:

1
2
3
istringstream sin(input);
double radius;
sin >> radius;


If that conversion was successful (sin evaluates as true), then the value the user input was a valid numeric value.

Hope that helps.
The article gcampton is looking for is
Using cin to get user input by Zaita
http://www.cplusplus.com/forum/articles/6046/


I have also written a response on obtaining verified input here
http://www.cplusplus.com/forum/beginner/13044/page1.html#msg62827
The OP's concern was that the input was actually an integer and not something like "4xyz".
My response uses a fancy class to do stuff, but that is not necessary. The important stuff is heavily commented inside the >> operator overload.

Hope this helps.
With a follow-up question on Duoas' little code, what's the value of cin after a "capture?"

I'm just asking because I'd never seen this before:
1
2
3
cin >> n;
if(cin)
{...}

Should there be an error in cin (such as a type-error), does cin get a null-value (0, NULL...)?
You are correct. It has the same effect as cin.good(), but is expressed by obtaining a (usually nonsense) pointer value.

It does not address the issue of 12X.5 that you listed above, but the last link I posted does.

Hope this helps.
@Duoas

entering something like:

1
2
3
Please enter an integer in range [19,37]:  2
Good job!
you entered the number '2'


I tried putting extra brackets around the conditional n>=19 || n<=37
but this didn't help.
Last edited on
Oops! Sorry about that. I fixed it for you above.

(I had originally written it with a negative test, and I forgot to flip the OR to an AND above. Which just goes to prove that any time I post something without first testing it for sanity, I will always goof up. Foo.)

Sorry.
Topic archived. No new replies allowed.