Trouble with program loops.

Okay, so I'll cut right to the chase here and link my program.


while (mathType != "added" || mathType != "subtracted" || mathType != "multiplied" || mathType != "divided")
{
cout << "The system would now like to know whether the user wants these numbers to be added, subtracted, multiplied, or divided: ";
cin >> mathType;
cout << endl;
}

*EDIT*

Okay, so it turns out the problem is actually that the || symbols aren't working. What can I use to include more than one "while" exception?
Last edited on
This is a common beginner mistake. mathType != "added" || mathType != "subtracted" is false if both mathType != "added" and mathType != "subtracted" is false. That is never the case because if mathType != "added" is false then mathType != "subtracted" is true (mathType can't be two strings at the same time). The solution is to use &&.
Ah okay, thanks a ton. If you wouldn't mind me asking, when can the || symbols be used then?
Last edited on
It all depends on what what you want to check.
a && b is true if both a and b is true.
a || b is true if at least one of a and b is true.

You could have used || in your code but then we have to change a few other things:
while (!(mathType == "added" || mathType == "subtracted" || mathType == "multiplied" || mathType == "divided"))
Topic archived. No new replies allowed.