Hello guys and Gals! This code is for an assignment for a beginners programming class, so its intended to be simplistic. I have run into three errors as I can tell. First when validating user input, if a letter is entered the loop is infinite. Second, when checking if there is a winner, tie, or continuing gameplay the first choice is always chosen, and subsequently printed out to the screen. Lastly, after the first run through of my main loop, the user input is not accepted, and then is input into incorrect board locations. Any help cleaning up this code would be much appreciated. Thanks in advance!!
lines 55 and 63 are causing the infinite loop. What's going on is that when cin expect integer input but receives something else, a failbit is set and cin will not take in any more input, so usermove always equals 0 (the value you initiated it to). What you need to do is reset the failbit and clear any remaining input. Here's how to reset: cin.clear();
You'll find the following either useful or confusing :p http://www.cplusplus.com/reference/ios/ios/clear/
I'll leave clearing remaining input up to you.
The second problem is a very common mistake. In CheckForWinner() (board[0] ='O')
should be (board[0] =='O')
and so on. If you wish to reduce the likelihood of making this mistake in the future you can use the following less intuitive test: ('O' == board[0])
then if you try something like ('O' = board[0])
The compiler will complain since the assignment is not valid.
The third problem may be a result of the second problem, not sure. Let me/us know if the problem persists after fixing the second issue.
Thanks so much! Your input helped me fix problems 2 and 3. Luckily the instructor told me not to worry about the infinite loop. I couldn't figure out cin.clear so lucky me! Again thanks so much!!