### Tic-Tac-Toe problem

This is my code:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118`` ``````#include using std::cin; using std::cout; using std::endl; void printGrid(const char[]); void goX(char[]); int main() { char grid[] = " "; cout<<"Q"<<"|"<<"W"<<"|"<<"E"<>xchoice; while(true) { if((xchoice='Q')||(xchoice='q')) { if (grid[0]=' ') grid[0]='X'; else cout<<"choose another one. "<

This is just the beginning, and it's not working right, the output is

 ``12345678910111213141516171819202122232425`` ``````Q|W|E -+-+- A|S|D -+-+- Z|X|C | | -+-+- | | -+-+- | | Input your choice. W X| | -+-+- | | -+-+- | | Input your choice. E X| | -+-+- | | -+-+- | |``````

I can't find where is wrong. I think the logic is right.
Can anyone help me?
Last edited on
In all of your if statements, you should be using the equals sign
`==`
`=`

There is a big difference between the two. The equals sign tests to see if the left hand side is equal to the right hand side. The assignment operator assigns the rhs to the lhs. As long as the rhs is not zero,
`if (lhs = rhs)`
will evaluate to TRUE.

While you're at it, why not use numbers to represent the spaces instead of letters? That way, you won't have to bother converting back and forth, and you won't have do distinguish between upercase and lowercase.
Last edited on
ok, I fixed it. But there still has problem.

 ``12345678910111213141516171819202122232425262728293031323334353637`` ``````void goX(char grid[]) { char xchoice; cout<<"Input your choice."<>xchoice; while(true) { if((xchoice=='Q')||(xchoice=='q')) { if (grid[0]==' ') grid[0]='X'; else cout<<"choose another one. "<

but output turns like this:
 ``` Q|W|E -+-+- A|S|D -+-+- Z|X|C | | -+-+- | | -+-+- | | Input your choice. w | | -+-+- | | -+-+- | | Input your choice. q X| | -+-+- | | -+-+- | | ```

It only showed the "Q" choice, others didn't display at all...
I also tried other options, only q worked.

Can anyone help?
Thanx!
All of the "break" statements need to be inside the "if" blocks. Otherwise, the while loop will just go through the first if statement and then skip the rest.
 ``1234567891011121314151617181920212223242526272829`` ``````void goX(char grid[]) { char xchoice; cout<<"Input your choice."<>xchoice; while(true) { if((xchoice=='Q')||(xchoice=='q')) { if (grid[0]==' ') grid[0]='X'; else cout<<"choose another one. "<
You never update xChoice within the while loop.
Topic archived. No new replies allowed.