-9

I wanted to make a Tic-Tac-Toe game and this is the part where I get the user to input what symbol he wants to use (X or O). I created the do while loop to ensure that the characters given will be X or O.

char symbol;

  do
  {
    printf("Choose Symbol X/O: ");
    scanf("%c", &symbol);
  } while ((symbol != 'X') || (symbol != 'O'));
Dimitris
  • 1
  • 2
  • 8
    Think about it: `(symbol != 'X') || (symbol != 'O')` is _always_ true. Try to reason in your head what happens when the symbol is either X or O. – B Remmelzwaal Feb 10 '23 at 18:45
  • If `symbol` is `'X'`, its not `'0'` . And vice versa. It can be neither (in which case both expressions are true), but if its either, its not the other (so one of the expressions is true). Pretty sure you want `&&` there; not `||`. – WhozCraig Feb 10 '23 at 18:47

1 Answers1

0

symbol will always be not 'X' or not 'O'. You want '&&' here, and I added the space before %c to ignore the newline:

#include <stdio.h>

int main() {
    char symbol;
    do {
        printf("Choose Symbol X/O: ");
        scanf(" %c", &symbol);
    } while (symbol != 'X' && symbol != 'O');
}

If you prefer you can use De Morgan's laws to negate the condition:

!(symbol == 'X' || symbol == 'O')

Which you could (after including string.h) write as:

!strchr("XO", symbol)
Allan Wind
  • 23,068
  • 5
  • 28
  • 38