Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

neobie

why does this keep giving me infinite loop

This topic is 5278 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

If i enter a alphabet, it infinite loops...i''ve tried everything i noe.....hmmmmm..can someone help. isnt a letter == some value in decimal places?
int choice = 0;
cin >> choice;
while (choice != 1 || choice != 2 || choice != 3)
	{
		
            cout << "Invalid input. Try again: ";
            cin >> choice;
		
		
	}

Share this post


Link to post
Share on other sites
Advertisement
I can''t remember the exact "why" of why this occurs, but I do know how to fix it. Make "choice" a char buffer and read in a certain number of characters ( cin.get() ), rather than "cin >> choice". Then, in your while loop, do an atoi() for each comparison.

There are a few more steps involved to make this data entry completely bulletproof, but that should accomplish what you are trying to do there.

Share this post


Link to post
Share on other sites
Maybe b/c cin doesn't return the integers you see printed on the screen, but characther codes of ASCII. In this case you don't need to look up the codes, you only need to put quotes around the numbers tested for:


int choice = 0;
cin >> choice;
while (choice != '1' && choice != '2' && choice != '3') { cout << "Invalid input. Try again."; cin >> choice; }

And don't forget the &&'s.

[edited by - temp_ie_cant_thinkof_name on June 7, 2004 2:19:46 AM]

Share this post


Link to post
Share on other sites
try this


while (choice != 1 && choice != 2 && choice != 3)
{

cout << "Invalid input. Try again: ";
cin >> choice;

if(cin.fail())
{
cin.clear();
cin.ignore(256,'\n');
}
}


[edited by - JohnyB on June 7, 2004 2:35:38 AM]

Share this post


Link to post
Share on other sites
You told it to infinite loop. Remember Demorgan! Boolean expressions don''t conform to English vernacular. (a != 1 || a != 2) is always true. Demorgan says that expression is the same as !(a == 1 && a == 2). Always true. Remember this one as it will bite you often if you''re not careful.

Share this post


Link to post
Share on other sites
choice != 1 || choice != 2 || choice != 3
This uses 'or'. One of those expressions will always be true, and so the whole thing will always be true.

choice != 1 && choice != 2 && choice != 3
This uses 'and'. All of those expressions must be true for it as a whole to be true. This is what you wanted, you used the wrong operator.

Next error, an integer can't hold a character. When you enter an nonnumerical value it can't store it and it sets the fail bit. All operations will fail until you clear that bit. In his code, if the fail bit is set, it will clear it and read past the whatever caused the error.

[edited by - smart_idiot on June 7, 2004 3:38:38 AM]

Share this post


Link to post
Share on other sites
That last bit ("an integer can''t hold a character") isn''t exactly true.

It''s perfectly legal to say:


int x = ''a'';
printf("%c", x);


Is it not?

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!