Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ while loop not working

Tags:

c++

#include <iostream>
#include <string>

using namespace std;

int main()
{
    string option;
    cout << "Would like water, beer, or rum?--> ";
    cin >> option;
    while( option != "water" || option != "beer" || option != "rum" )
        {
        cout << "You did not choose a valid option. Try again.\n";
        cout << "Would you like water, beer, or rum?-->";
        cin >> option;
        }
}

Why doesn't this code ever exit the loop even though the user enters the right option?

like image 443
fifiman Avatar asked Mar 19 '26 10:03

fifiman


1 Answers

Read your condition out loud - "run the loop while option is not "water" or option is not "beer" or...".

When should it stop?

like image 59
Luchian Grigore Avatar answered Mar 20 '26 22:03

Luchian Grigore