0

I am creating a tic tac toe program in c++ and am almost finished with it, the issue I'm having is when the game needs to end in a tie. I created an if-statement to check when specific combinations are selected and it finds it true even when only one condition is met.

Here is the code...

//takes the users selections and sees if all spots are filled in without a match.
if((b1== "O" || "X") && (b2 == "O" || "X") && (b3 == "O" || "X")){
        cout << "The game ended in a tie.";
      // Flip = 3 ends the game in a tie.
        flip = 3;
    }


ex... the game ends in a tie when i change `b1` to and `"O"` or an `"X"` even though `b2` and `b3` are still empty.
Devin Bowen
  • 127
  • 1
  • 1
  • 13
  • 3
    `(b1== "O" || "X")` -> `(b1 == "O" || b1 == "X")`, etc. – Retired Ninja Mar 24 '21 at 02:04
  • 1
    `if((b1== "O" || "X")` -- I'll make a bet that you won't see an `if` condition that looks like this in any of the C++ reading material you have. So that should give you suspicion that this is wrong. That's the danger when learning C++ -- if you go and "do your own thing" away from what you're learning, you wind up with valid, but wrong code being used. – PaulMcKenzie Mar 24 '21 at 02:22

1 Answers1

3

The condition:

b1== "O" || "X"

evaluates as:

(b1=="O") || "X"

which is always true. You probably want:

(b1=="O" || b1=="X")
Allan Wind
  • 23,068
  • 5
  • 28
  • 38