expression must be a modifiable lvalue error
Jun 13, 2012 at 9:29am UTC
i have a project i need to write a tic tac toe game using arrays..
i've managed to write everything but i'm having problem when writing the function win() to determine who has won...
i have a
char square[3][3]={' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' ,' ' };
im writing this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
int win()
{int pwin=5;
if (square[0][0]=='X' &&square[0][1]=='X' && square[0][2]=='X' )
pwin= 1;
else if (square[1][0]=='X' &&square[1][1]=='X' && square[1][2]='X' )
pwin=1;
else if (square[2][0]=='X' &&square[2][1]=='X' && square[2][2]='X' )
pwin=1;
else if (square[0][0]=='X' &&square[1][0]=='X' && square[2][0]='X' )
pwin=1;
else if (square[0][1]=='X' &&square[1][1]=='X' && square[2][1]='X' )
pwin=1;
else if (square[0][2]=='X' &&square[1][2]=='X' && square[2][2]='X' )
pwin=1;
else if (square[0][0]=='X' &&square[1][1]=='X' && square[2][2]='X' )
pwin=1;
else if (square[0][2]=='X' &&square[1][1]=='X' && square[2][0]='X' )
pwin=1;
else if (square[0][0]=='O' &&square[0][1]=='O' && square[0][2]=='O' )
pwin= 2;
else if (square[1][0]=='O' &&square[1][1]=='O' && square[1][2]='O' )
pwin=2;
else if (square[2][0]=='O' &&square[2][1]=='O' && square[2][2]='O' )
pwin=2;
else if (square[0][0]=='O' &&square[1][0]=='O' && square[2][0]='O' )
pwin=2;
else if (square[0][1]=='O' &&square[1][1]=='O' && square[2][1]='O' )
pwin=2;
else if (square[0][2]=='O' &&square[1][2]=='O' && square[2][2]='O' )
pwin=2;
else if (square[0][0]=='O' &&square[1][1]=='O' && square[2][2]='O' )
pwin=2;
else if (square[0][2]=='O' &&square[1][1]=='O' && square[2][0]='O' )
pwin=2;
else if (square[0][0] != ' ' && square[0][1] != ' ' && square[0][2] != ' ' && square[1][0] != ' ' &&square[1][1] != ' '
&& square[1][2] != ' ' && square[2][0] != ' ' && square[2][1] != ' ' && square[2][2] != ' ' )
pwin=0;
else
pwin= -1;
return pwin;
in function main i wrote this
1 2 3 4 5 6 7 8
i=win();
if (i==1)
cout<<"player 1 won" ;
if (i==2)
cout<<"computer won" ;
if (i==0)
cout<<"Game draw" ;
now my problem is that im having an expression must be a modifiable lvalue error in win() function after the first if statement.... how to solve it?
thank you
Last edited on Jun 13, 2012 at 9:37am UTC
Jun 13, 2012 at 9:56am UTC
You are using = (assignment) instead of == (equal to comparison) in a few places.
Jun 13, 2012 at 10:33am UTC
thank you
Jun 13, 2012 at 10:40am UTC
As Peter87 pointed out using the assignment operator instead of the comparision operator and due to priorities of operations the statement
1 2
else if (square[1][0]=='X' &&square[1][1]=='X' && square[1][2]='X' )
pwin=1;
is interpretated by the compiler as
1 2
else if ( ( square[1][0]=='X' &&square[1][1]=='X' && square[1][2] ) = 'X' )
pwin=1;
that is you are trying to assign 'X' to the expression ( square[1][0]=='X' &&square[1][1]=='X' && square[1][2] )
Topic archived. No new replies allowed.