-1

Trying to test if input is "TF" or "MC". The while condition keeps coming out as true even though argument is written: line != "TF" || line != "MC"

Not understanding how the loop keeps repeating even though I input TF or MC. I have also verified that the tranform method is making the string capital.

do {

    cout << "\nEnter the Question type (TF) for True/False or (MC) for Multiple Choice:\n";
    getline(cin, line);
    transform(line.begin(), line.end(), line.begin(), ::toupper);

    } while (line != "TF" || line != "MC");

I expected the loop to only initiate once and exit.

2 Answers2

4

If the person types "MC", the expression

line != "TF"

will be set to true, which will make the OR statement true.

(And thus repeating the while even though the person typed a valid answer)

What you are looking for is to check if the answer is neither one of the options, which can be checked as follows:

(line != "TF" && line != "MC")

The opposite would be reasonable too. That is, to check if the person typed a valid answer, and keep repeating it while it is not the case:

while(!(line == "TF" || line == "MC"))

Both statements are equivalent, as stated in the comments, by the De Morgan's Laws.

Naslausky
  • 3,443
  • 1
  • 14
  • 24
0

This might be overkill, but it does explain the relationships between the OR, AND, and NOT operators: https://en.wikipedia.org/wiki/Boolean_algebra

user11499890
  • 131
  • 5