Infinite Loop. using while.

***************************************************************************
getline(cin,strcharchoice);

strcharchoice = strcharchoice[0];
charchoice = atoi(strcharchoice.c_str());

while(charchoice != 1 || charchoice != 2 || charchoice != 3)
{
cout << "Error! That be in invalid choice!" <<endl;
cout << "Please enter a number correspondin' t' a character." << endl;
getline(cin,strcharchoice);
strcharchoice = strcharchoice[0];
charchoice = atoi(strcharchoice.c_str());
} //loop to catch a error in character selection, waits until error is corrected.
***************************************************************************


That is the section of code giving my trouble for a game im making, when using getline I take in a function as a string, then using atoi convert the first part to an integer to make sure its a valid selection. However if i do in fact enter a valid selection it dosen't register as one. any insight?

where charchoice is an int
Last edited on
The problem is the while condition itself. Analyze it logically and try to find what's wrong with it.
(charchoice != 1 || charchoice != 2 || charchoice != 3)

Can you think of ANY value for charchoice in which this will not always be true? This will loop forever.

Edit: I'm thinking of writing a very short article entitled "The meaning of OR". This comes up every other day.
Last edited on
yes. wow.. didn't even realize that.. fixed the way i attacked the loop.
Topic archived. No new replies allowed.