The problem is, let's say the user chooses square 1 to move to (1 = top left, 2 top middle, 3 top right, 4 middle left, etc.). Then say the other player tries to move onto that square. If they try to do that, it displays the "Invalid move press enter to continue" message, but instead of waiting for getchar(), it ignores it and carries on. However, if someone enters an invalid move entirely, like for example 'd', then the getchar() fires and correctly waits for a key press.
What am I missing here? Can't work out why it's doing it (I'd imagine it's possibly because I'm using the wrong function to deal with the input?).
Thanks in advance.
--hellz
Edit: Bah, had to change from source tags to code tags. Something keeps breaking them when I use them, so only some of the code is displayed. :|
[edited by - hellz on August 12, 2003 7:33:02 PM]

Share on other sites

Each number corresponds with a move the user can make. So, I prompt them for their next move (which lies between 1 and 9). Imagine someone enters 1 as their move, the grid at the start looks like this:

X| | ----- | | ----- | |

Now, if someone enters 1 again, the program should indicate that the move is invalid. It does this, but it doesn''t wait for a key press to then attempt to move again.

Output (haven''t fixed the layout yet, so excuse the debuginess ):

| |----- | |----- | |

1|2|3-----4|5|6-----7|8|9

Make your next move (1-9): 1

Now the next move:

X| |----- | |----- | |

1|2|3-----4|5|6-----7|8|9

Make your next move (1-9): 1

It displays the "Invalid move. Press enter to continue." message, but it doesn''t wait for getchar(). It just carries on into the next iteration of the main game loop. Only reason you can see the invalid move message, is because you see it flicker on the screen.

Share this post

Link to post

Share on other sites

What if someone enters something like 77? Does the message disappear right away? I think it will, because you are not clearing the buffer after getting this error. I think that when you enter a number with cin << nextMove; the ''\n'' character stays in the buffer all the way until you get to getchar() which then takes ''\n'' char from the buffer and keeps going.

K, that might have been confusing, but just try putting a cin.clear(); before getchar();

0

Share this post

Link to post

Share on other sites

Hmm, I see where you''re heading (thanks for the reply btw ). I infact just put cin.ignore(1, ''\n''); after the first if statement (as you''re right, I''m not catching the newline - can''t believe I didn''t see that, lol). It fixes the problem of when a number is entered, but if a character is entered, you have to press enter twice for it to recognise it''s an invalid move. The first keypress skips to a newline, then the second makes the program display the error message.

Quite odd, but there''s obviously a logic error in my code with my error handling. I tried using cin.clear() before getchar() but that didn''t have any affect.