While loop.

I've been having some trouble with a while loop in a program for a tic tac toe game and i can't seem to figure out what I'm doing wrong.

what happens is even if the player enters a 1,2 or 3 it still displays the message.
1
2
3
4
5
6
7
8
9
10
11
12
13
 for (int t=0;t<9;++t){
cout<<"\nEnter where "<<player1<<" would like to go:\n";
cout<<"\nColumn: ";
cin>>column;
cout<<"Row: ";
cin>>row;
while(column||row!=1||2||3){
    cout<<"You can only enter a number that is: one, two or three.\nPlease try again";
cout<<"\nColumn: ";
cin>>column;
cout<<"Row: ";
cin>>row;
}

I've only been learning c++ for about a month now so its probably just a newbie mistake.

Thanks for any help.
Last edited on
while(column||row!=1||2||3)

Unfortunately you can't test two variables at a same time like that.
(It would be quite elegant though.)

What happens is:

test column... if 0 (false) then:
test row != 1... if false then:
test 2... is always true because 2 is not 0 and stop here...
3 is never tested but it would be true as well


And that's why the message is always displayed.

So the correct test would be:

1
2
3
4
5
6
while ((column != 1 && column != 2 && column != 3) ||
    (row != 1 && row != 2 && row != 3))

// slightly simplified

while (column < 1 || column > 3 || row < 1 || row > 3)

Alright, thank you.
It now works fine.
Last edited on
Topic archived. No new replies allowed.