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');

Recommended Answers

All 3 Replies

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

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

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

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.