How to verify whether std::cin input is a number or not?

Started by
6 comments, last by Zakwayda 13 years, 1 month ago
So, I'm creating a connect4 game on the win32 console using C++, and... success! it works! my first game that I have written entirely myself, woop. The only problem is that it requires inputting numbers to cin at various intervals, and I have noticed that if you were to accidentally type a character instead of a number, it all goes tits up and loops the messages "Please enter your choice: " and "Sorry, that is not a valid choice" for thousands, maybe even millions of times if you were to let it.

Anyway as a result I've got a couple of questions - is there any way to avoid this problem? For example, is there a library function to validate whether whatever std::cin takes is an integer or not? I've got default cases and tried while/for loops but it doesn't seem to solve the problem.

Secondly, what's the best thing to do when I come into problems like this that stem from not knowing the capabilities of a library? I don't want to rely on gamedev.net and google all my life, however helpful they are!

Thanks in advance for any replies
Advertisement
First - congrats on your first game!

To give you specific help, it depends on how you're processing std::cin. Posting some code for that would be useful.

You might try inputting characters, rather than a number (which I assume you're doing). Then scan each character in the input and test for something like " <'0' || > '9' ".

If the characters are satisfactory, convert the string to a number with atoi(...) or similar.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.


int someVal;

while(!(cin >> someVal)) {
cin.reset();
cout << "Invalid value, try again.";
}

http://stackoverflow...numeric-input-c
Thanks for the response guys.

@UltimaX: Visual C++ doesn't seem to recognise the function cin.reset(), does it require a particular header file? As I'm using <iostream>


int getChoice()
{
int box = 0;
while ( (box != 1) && (box != 2) && (box != 3) && (box != 4) &&
(box != 5) && (box != 6) && (box != 7))
{
cout<<"\nPick a column (1 to 7) : ";
cin>>box;
}
return box;
}


this is the function I have written for getting a player's choice. Say you accidentally enter 'q' instead of '1', it will repeat "Pick a column (1 to 7) : " for thousands of times.
I think

Ignore

is what you were wanting to do.
If you want to check the number is 1 to 7, use this:

while( (box < 1) || (box > 7) )
{
// ...
}
Thanks for the replies everyone. Had a bit of trouble using 'ignore' and was confused by the other posts so I let it be for a while. Since then, I've got the book 'Accelerated C++' as seen it recommended here and on amazon, and after reading first few chapters learnt a lot, so can't recommend this enough to someone like me that's just starting out, seems to really explain the std library better and in more depth than anything i've used so far. With reference to the original problem though, I didn't know you could use "cin>>x" as a condition, and so whilst i'm still not sure how it converts that expression into a bool, i've used it and it works!

I didn't know you could use "cin>>x" as a condition, and so whilst i'm still not sure how it converts that expression into a bool, i've used it and it works!

operator>>() returns a reference to the input stream (to facilitate operator chaining, among other things). One of the stream base classes in turn includes an operator that allows for implicit conversion to 'bool', where the return value is determined by the current state of the stream's flags (basically, whether the stream is 'ok' or not). So when you write:

if (std::cin >> x)
operator>>() is invoked, a reference to std::cin is returned, the compiler tries to convert this return value to type 'bool', and is able to do so because of the conversion operator. The end effect is that the conditional basically checks to see if the state of the stream is still 'ok' after the extraction is attempted.

(IINM at least; I'm not looking at the documentation right now, so I can't guarantee I got all the details right.)

This topic is closed to new replies.

Advertisement