Skip to main content
gamedev.net
🔒 Locked

checking for valid data

Started by Newbie-O-Rama Sep 8, 2000 at 9:23 PM 4 replies 250+ views
Original Post
Newbie-O-Rama
Newbie-O-Rama
OK, here is the newbie question of the night: If I write something like this: void decide(); int choice; cout << "Choose ''1'' or ''2''." << endl; cin >> choice; if (choice==11) { cout << "1"; } if (choice==2) { cout << "2"; } else if (!=2) { void decide(); } } How can I keep it from aborting if someone enters a ''3'' or a ''b'' when the program is run? Thank you.
Newbie-O-Rama
Newbie-O-Rama
Oops, typo there, I meant:

if (choice==1)

not:

if (choice==11)

dumb fingers......
Quantum
Quantum
your code there is a little confusing.. is that supposed to be inside the function?
try this:

        
void decide()
{
int choice;
do
{
cout << "Choose '1' or '2'." << endl;
cin >> choice;

if(choice==1)
cout << "1";

else if(choice==2)
cout << "2";
} while((choice!=1) && (choice!=2));
}


Edited by - Quantum on September 8, 2000 10:49:56 PM
Newbie-O-Rama
Newbie-O-Rama
Hey thanks!!!!!!!
It almost worked! It did not abort when I entered a wrong number, but when I tested it by entering a "b", it went into an endless loop (nasty things, really).
Is there another line or two of code to take care of letter input?
thanks again.
Null and Void
Null and Void
Yeah, cin >> ints, will loop up when you input a character.

Try including stdio.h and using scanf instead (scanf(choice,"%i"))

OR

You can change choice to a character (char). Then use ''1'' instead of 1. That will keep it from locking up...
Newbie-O-Rama
Newbie-O-Rama
THANK YOU!!!!!!!!!!!!
As you can tell, I''m fairly new (very new actually), and I appreciate your responses. I almost did not post because of all the "bad" responses I''ve seen to other newbie questions, but when I can''t find it in my school books, I have to ask someone, and everyone was new once, so it irks me when flames are sent to someone who is trying to learn.
You guys rock.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.