0

I have tired several things....And i still can't pinpoint on what I can do differently.
I want the dowhile loop to run untill r, b or g is entered. I have tried commas like in the below code and other things exp...

while (pick != r || pick != 'g' || pick != 'b'); <---really thought that would work with the OR operators....

Each time it just keeps performing the dowhile loop even if I enter one of the 3 letters...It works when I only have one conidition in the dowhile loop, so are you only able to put one?

If so, how would i rewrite this code?

Thanks,
Derek

char pick;
do
{
cout << "What color do you want to bet on?\n";
cout << "[r]ed\t[b]lue\t[g]reen\n";
cin >> pick;
switch(pick)
{
               case 'r':
                    cout << "You want red to win!\n";
                    break;
                    case 'g':
                         cout<< "You want green to win!\n";
                        break;
                         case 'b':
                              cout << "You want blue to win!\n";
                              break;
               }
}while (pick != 'r','b','g');
3
Contributors
3
Replies
4
Views
7 Years
Discussion Span
Last Post by arkoenig
0

That is an infinite loop. Try replacing the OR operators with AND operators.

while(pick != 'r' && pick != 'g' && pick != 'b')
{}

Edited by Jsplinter: n/a

0

That is confusing seems like u would have to type r g and b with the && and....can you explain why || doesn't work?

You did fix my problem :-)
Thank you

Edited by dadam88: n/a

0

The expression pick != 'r' || pick != 'g' is always true.

The reason is that either pick != 'r' is true, in which case the whole expression is true, or pick != 'r' is false, in which case pick must be equal to 'r'. And if that is the case, then pick != 'g' must be true, so the whole expression is still true.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.