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?


char pick;
cout << "What color do you want to bet on?\n";
cout << "[r]ed\t[b]lue\t[g]reen\n";
cin >> pick;
               case 'r':
                    cout << "You want red to win!\n";
                    case 'g':
                         cout<< "You want green to win!\n";
                         case 'b':
                              cout << "You want blue to win!\n";
}while (pick != 'r','b','g');

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

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

Edited 6 Years Ago by Jsplinter: n/a

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 6 Years Ago by dadam88: n/a

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 article has been dead for over six months. Start a new discussion instead.