### The problem with Tic Tac Toe

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!!

 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221 #include #include #include #include #include #include using namespace std; const int item=9; char board[item] = {'1','2','3','4','5','6','7','8','9'}; //Game board array. int DisplayBoard() //Displays game board to user. { char *boardptr; cout<<"| "; for (int count=0; count<3; count++) { boardptr= &board[count]; cout<< *boardptr <<" | "; } cout<< endl; cout<< "-------------"; cout<< endl; cout<<"| "; for (int count=3; count<6; count++) { boardptr= &board[count]; cout<< *boardptr <<" | "; } cout<< endl; cout<< "-------------"; cout<< endl; cout<<"| "; for (int count=6; count> usermove; while (validchoice==false) //Checks if user input is valid. { if(isdigit(usermove)) { validchoice=false; cout<<"Invalid choice. Please reenter your selection." <> usermove; } else { if(usermove<1 || usermove>9) { validchoice=false; cout<<"Invalid choice. Please reenter your selection." <> usermove; } else { validchoice=true; moveptr=&board[usermove-1]; if (*moveptr=='X' || *moveptr=='O') { validchoice=false; cout<<"Invalid choice. Please reenter your selection." <> usermove; } else { *moveptr='X'; //If input is valid it is accepted as cout<<"\n"; //a game move. DisplayBoard(); } } } } return 0; } int GetComputerMove() { srand (unsigned int(time(NULL))); //Randomizes computer move. bool isvalid=0; while (isvalid==false) { char *compmoveptr; int compmove = rand() % 9; //Validates computer move. compmove=(compmove-1); if (compmove<1 || compmove>9) { isvalid=false; } else { compmoveptr=&board[compmove]; if (*compmoveptr=='X' || *compmoveptr=='O') { isvalid=false; } else { isvalid=true; *compmoveptr='O'; cout<<"\n"; cout<<"Computer is moving to "; compmove=(compmove+1); //Places computer move. cout<
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!!
Topic archived. No new replies allowed.