### expression must be a modifiable lvalue error

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:

 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 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
 12345678 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
You are using = (assignment) instead of == (equal to comparison) in a few places.
thank you
As Peter87 pointed out using the assignment operator instead of the comparision operator and due to priorities of operations the statement

 12 else if(square[1][0]=='X' &&square[1][1]=='X' && square[1][2]='X') pwin=1;

is interpretated by the compiler as

 12 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.