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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#include <iostream>
#include <cstring>
#include <cctype>
#include <string>
#include <ctime>
#include <cstdlib>

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<item; count++)
		{
			boardptr= &board[count];
			cout<< *boardptr <<" | ";
		}
		cout<< endl;
		return 0;
}

int GetUserMove()
{
	int usermove=0;
	bool validchoice=false;
	char *moveptr;
	cout<<"Please enter a move between 1-9, that has not already been taken." <<endl;
	cin>> usermove;

	while (validchoice==false)										//Checks if user input is valid.
	{
		if(isdigit(usermove))
			{ 
			validchoice=false;
			cout<<"Invalid choice. Please reenter your selection." <<endl;
				cin>> usermove;
			}
		else
		{
			if(usermove<1 || usermove>9)
				{
					validchoice=false;
					cout<<"Invalid choice. Please reenter your selection." <<endl;
					cin>> usermove;
					 
			     }
			else
			{
				validchoice=true;
				moveptr=&board[usermove-1];
			if (*moveptr=='X' || *moveptr=='O')
			   {
				validchoice=false;
				cout<<"Invalid choice. Please reenter your selection." <<endl;
				cin>> 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<<compmove;
						cout<< "\n";
						DisplayBoard();
					}
				}
		}


	return 0;
}

int CheckForWinner()
{	
	if (((board[0] ='O') && (board[1]='O') && (board[2]='O'))
	 || ((board[3] ='O') && (board[4]='O') && (board[5]='O'))
	 || ((board[6] ='O') && (board[7]='O') && (board[8]='O'))
	 || ((board[0] ='O') && (board[3]='O') && (board[6]='O')) 
	 || ((board[1] ='O') && (board[4]='O') && (board[7]='O'))
	 || ((board[2] ='O') && (board[5]='O') && (board[8]='O'))
	 || ((board[0] ='O') && (board[4]='O') && (board[8]='O'))
	 || ((board[2] ='O') && (board[4]='O') && (board[6]='O')))
	{
		return 3;
	}
	if (((board[0] ='X') && (board[1]='X') && (board[2]='X'))
	 || ((board[3] ='X') && (board[4]='X') && (board[5]='X'))
	 || ((board[6] ='X') && (board[7]='X') && (board[8]='X'))									//Tests if there is a winner.
	 || ((board[0] ='X') && (board[3]='X') && (board[6]='X')) 
	 || ((board[1] ='X') && (board[4]='X') && (board[7]='X'))
	 || ((board[2] ='X') && (board[5]='X') && (board[8]='X'))
	 || ((board[0] ='X') && (board[4]='X') && (board[8]='X'))
	 || ((board[2] ='X') && (board[4]='X') && (board[6]='X')))
	{
		return 2;
	}
	if (((board[0] =='X') || (board[0]=='O')) 
	  && ((board[1]=='X') || (board[1]=='O')) 
	  && ((board[2]=='X') || (board[2]=='O')) 
	  && ((board[3]=='X') || (board[3]=='O'))											//Tests if there is a tie 
	  && ((board[4]=='X') || (board[4]=='O')) 
	  && ((board[5]=='X') || (board[5]=='O')) 
	  && ((board[6]=='X') || (board[6]=='O')) 
	  && ((board[7]=='X') || (board[7]=='O')) 
	  && ((board[8]=='X') || (board[8]=='O')))
	{
		return 1;
	}
	
	else												
	{
		return 0;
	}
}

void main()
{
	bool gameinprogress=true;
	DisplayBoard();

	while (gameinprogress=true)
	{
	GetUserMove();
	GetComputerMove();
	CheckForWinner();

	if (CheckForWinner() == 0)
		{
			gameinprogress=true;
		}

	else if (CheckForWinner() == 2)
		{
			gameinprogress=false;
			cout<< "You have won!!";
		}
	else if (CheckForWinner() == 3)
		{
			gameinprogress=false;
			cout<<"The computer has won!!";
		}
	else if (CheckForWinner() == 1)
		{
			gameinprogress=false;
			cout<<"It's a tie!!";
		}
		
	}
	

}
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.