C++ while loops with strings

Regardless of what value I input the while statement won't recognize the input as matching either of the three predetermined values. I am very new to this... What am I doing wrong?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    string po1;
    string po2;
    string poExercise;
    
    cout << "Which lift(s) would you like to perform progressive overload on?" << endl;
    cout << "Type 'squat', 'bench' or 'deadlift'." << endl;
    cin >> po1;
    
    while ((po1 != "squat") || (po1 != "bench") || (po1 != "deadlift"))
    {
        cout << "Your input was invalid." << endl;
        cout << "Which lift(s) would you like to perform progressive overload on?" << endl;
        cout << "Type 'squat', 'bench' or 'deadlift'." << endl;
        cin >> po1;
    }
@lewi0027
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    string po1;
    string po2;
    string poExercise;
    
    cout << "Which lift(s) would you like to perform progressive overload on?" << endl;
    cout << "Type 'squat', 'bench' or 'deadlift'." << endl;
    cin >> po1;
    
    while ((po1 != "squat") || (po1 != "bench") || (po1 != "deadlift"))
    {
        cout << "Your input was invalid." << endl;
        cout << "Which lift(s) would you like to perform progressive overload on?" << endl;
        cout << "Type 'squat', 'bench' or 'deadlift'." << endl;
        cin >> po1;
    }

That statement will always be true.

All the ORs are checked for truth. Given any value for pol, at least 2 will be true.
Last edited on
How would I write this to check that the string matches one of those three values and if it doesn't to keep making them input it until they get it right? If that is going to be a lot more complex I can just provide a list of numbers and have them choose that way. I was trying to figure this out though.

NVM I just replaced them with &&. I am not very good at intuiting this process.
Last edited on
The loop should probably be:
 
while ((pol != "squat") && (pol != "bench") && (pol != "deadlift"))
Last edited on
Input is valid if the input is either squat or bench or deadlift. So in C++, input is valid:

1
2
if (po1 == "squat" || po1 == "bench" || po1 == "deadlift")
    // valid input 


Therefore for invalid input, it is the opposite of this, ie in C++

1
2
if (!(po1 == "squat" || po1 == "bench" || po1 == "deadlift"))
    // invalid input 


So the while loop could be:

 
while (!(po1 == "squat" || po1 == "bench" || po1 == "deadlift"))


But using boolean logic (de morgan's theorem), this can be rewritten as:

 
while (po1 != "squat" && po1 != "bench" && po1 != "deadlift")


See https://en.wikipedia.org/wiki/De_Morgan%27s_laws

Thanks Seeplus for the link about DeMorgan laws. Interesting short reading...

1
2
3
4
5
// it's running in all cases because you misunderstood || (or) and && (and)
// whatever your entry , it corresponds to all possible variants :/
while ((po1 != "squat") || (po1 != "bench") || (po1 != "deadlift"))
// it's running until you enter one of these three entries
while (po1 != "squat" && po1 != "bench" && po1 != "deadlift")

However, as mentioned Seeplus, for me the most readable condition will be this one :
1
2
// only one of these entries can break the condition
while (!(po1 == "squat" || po1 == "bench" || po1 == "deadlift"))

Some interesting explanations at the link below :
https://www.learncpp.com/cpp-tutorial/logical-operators/
Last edited on
Topic archived. No new replies allowed.