Reputation: 21
After getting to the second if statement and getting the new char value, int number does not seem like it is getting replaced. Any hints on how I can change this so after that if statement the char value goes back to int number?
if (c < '9' || c > '0')
{
int number = c - '0';
if (board[number - 1] == 'X' || board[number - 1] == 'O')
{
cout << "Please enter another Number";
cin.get(c);
cin.ignore(numeric_limits<int>::max(), '\n');
}
if (board[number - 1] != 'X' || board[number - 1] != 'O' )
{
if (player == 'X')
{
player = 'O';
}
else
{
player = 'X';
}
cout << "player " << player << " your number is " << number << endl;
// Your implementation here...
board[number - 1] = player;
}
}
This keeps happening:
1 | 2 | 3
4 | 5 | 6
7 | 8 | 9
Please Enter Box Number: 1
player X your number is 1
X | 2 | 3
4 | 5 | 6
7 | 8 | 9
Please Enter Box Number: 1
Please enter another Number2
player O your number is 1
O | 2 | 3
4 | 5 | 6
7 | 8 | 9
Please Enter Box Number:
Upvotes: 0
Views: 153
Reputation: 91
The variable 'number' is not getting replaced because you aren't assigning anything to it after the 'if' statement. You need to reassign to 'number' after reading another character from cin. In fact, it's probably best if rather than a single if check to see if that spot in the board is unoccupied, you loop until the user enters an unfilled square:
while (board[number - 1] == 'X' || board[number - 1] == 'O') // loop until valid
{
cout << "Please enter another Number";
cin.get(c);
cin.ignore(numeric_limits<int>::max(), '\n');
number = c - '0'; // update array index
}
Upvotes: 1
Reputation: 320719
Logical expressions c < '9' || c > '0'
and board[number - 1] != 'X' || board[number - 1] != 'O'
are always true. They are tautologies. This probably means that they don't implement what you intended to implement. You have to review these expressions. As is, you code make little sense.
Upvotes: 4