First if statement always 'true'.

Feb 25, 2014 at 11:26am
I've tried many different things but it seems no matter what I do, my first 'if' statement is always true, even when it should be false. This problem occurs in the following code,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void announceWinner()
{ 


		 	if (board [0] == 'X' || board [1] == 'X'  || board [2] == 'X' || board [3] == 'X' || board [4] == 'X' || board [5] == 'X' || board [6] == 'X' || board [7] == 'X' || board [8] == 'X')
		 	{
		 	cout << string(24, '\n' );	
            cout<<"X Wins!"; 
            cout<<endl<<endl;
       	 	}

         	else if (board [0] == 'O' || board [1] == 'O'  || board [2] == 'O' || board [3] == 'O' || board [4] == 'O' || board [5] == 'O' || board [6] == 'O' || board [7] == 'O' || board [8] == 'O')
			{
			cout << string( 24, '\n' );	
			cout<<"O Wins!"; 
			cout<<endl<<endl;
			}
}


Other information:

This is for a tic-tac-toe program.
Everything else works fine.

EDIT: Sorry, the code is longer than the area.
Last edited on Feb 25, 2014 at 11:29am
Feb 25, 2014 at 11:45am
Your condition will be true if one or more of the elements of board is 'X'. Are you absolutely certain that every single one of those 9 elements is something other than 'X' ?
Feb 25, 2014 at 12:06pm
The statement does not check for the winning condition, it just checks whether 'X' is somewhere within board
Feb 25, 2014 at 8:35pm
It has already checked for a winning condition, it is determining whether the winning condition is X, or Y.
Feb 25, 2014 at 8:45pm
Whatever you want to check for the first if evaluates to true if 'X' is anywhere on the board. The ideal time to determine whether the winner is X or Y O is when you're checking to see if there's a winner.
Last edited on Feb 25, 2014 at 8:48pm
Feb 25, 2014 at 9:43pm
That might just double my code, but it'll probably work. I'll try that and get back to you (:

Update:

Thanks Cire! I was hoping to avoid alot of code but this method seems to have worked (:
Last edited on Feb 25, 2014 at 9:52pm
Topic archived. No new replies allowed.