input check help

New to c++, i'm sure that will be obvious when you look at my code.
Writing a Tic Tac Toe program for a class. I would like to check input for each user in my For loop. If the user accidentally enters a letter instead of a number or a number lower than 0 or higher than 2 I would like to provide the user with the ability to re-enter their incorrect choice. However I cannot get it to work no matter what I try. Please Help!!

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
#include <iostream>
#include <process.h>
#include <string>

using namespace std;

const int ROWS = 3;
const int COLS = 3;
void gameplay(char array[][COLS], int);
void board(char array[][COLS],int);

int main()
{
	int count;
	char array[3][3] = { '*','*','*','*','*','*','*','*','*' };
	cout << "Welcome to Tic Tac Toe!" << endl;
	board(array, ROWS);
	gameplay(array, ROWS);

	
}

void board(char array[][COLS],int rows)
{

	for (int i = 0; i < rows; i++)
	{
		for (int j = 0; j < rows; j++)
		{
			cout << array[i][j] << " ";
		}
		cout << endl;
	}

	
}

void gameplay(char array[][COLS], int rows)
{
	int row;
	int col;
	int count = 0;
	cout << endl;

	for (int count = 1; count < 8; count++)
	{
		cout << "Player 1 you are X." << endl;
		cout << "Please pick row number." << endl;
		cin >> row;
		cout << "Please pick a column number." << endl;
		cin >> col;

		if (row == 0 || row == 1 || row == 2 || col == 0 || col == 1 || col == 2)
		{

			if (array[row][col] == '*')
			{
				array[row][col] = 'X';
				board(array, ROWS);
			}
			else
			{
				cout << "This position is taken, please pick another" << endl;


			}
			if (array[0][0] == 'X' & array[0][1] == 'X' & array[0][2] == 'X')
			{
				cout << "X has won." << endl;
				system("pause");
				exit(0);
			}

			else if (array[1][0] == 'X' && array[1][1] == 'X' && array[1][2] == 'X')
			{
				cout << "X has won." << endl;
				system("pause");
				exit(0);
			}
			else if (array[2][0] == 'X' && array[2][1] == 'X' && array[2][2] == 'X')
			{
				cout << "X has won." << endl;
				system("pause");
				exit(0);
			}
			else if (array[0][0] == 'X' && array[1][0] == 'X' && array[2][0] == 'X')
			{
				cout << "X has won." << endl;
				system("pause");
				exit(0);
			}
			else if (array[0][1] == 'X' && array[1][1] == 'X' && array[2][1] == 'X')
			{
				cout << "X has won." << endl;
				system("pause");
				exit(0);
			}
			else if (array[0][0] == 'X' && array[1][0] == 'X' && array[2][0] == 'X')
			{
				cout << "X has won." << endl;
				system("pause");
				exit(0);
			}

			else if (array[0][0] == 'X' && array[1][1] == 'X' && array[2][2] == 'X')
			{
				cout << "X has won." << endl;
				system("pause");
				exit(0);
			}

			else if (array[0][2] == 'X' && array[1][1] == 'X' && array[2][2] == 'X')
			{
				cout << "X has won." << endl;
				system("pause");
				exit(0);
			}
		}
			else
		{
			cout << " INVALID ENTRY" << endl;
		}



			cout << "Player 2, you are O." << endl;
			cout << "Please pick row number." << endl;
			cin >> row;
			cout << "Please pick a column number." << endl;
			cin >> col;

			if (row == 0 || row == 1 || row == 2 || col == 0 || col == 1 || col == 2)
			{
				if (array[row][col] == '*')
				{
					array[row][col] = 'O';
					board(array, ROWS);
				}
				else
				{
					cout << "This position is taken, please pick another" << endl;


				}
				if (array[0][0] == 'Y' & array[0][1] == 'Y' & array[0][2] == 'Y')
				{
					cout << "Y has won." << endl;
				}

				else if (array[1][0] == 'X' & array[1][1] == 'X' & array[1][2] == 'X')
				{
					cout << "Y has won." << endl;
				}
				else if (array[2][0] == 'Y' & array[2][1] == 'Y' & array[2][2] == 'Y')
				{
					cout << "Y has won." << endl;
				}
				else if (array[0][0] == 'Y' & array[1][0] == 'Y' & array[2][0] == 'Y')
				{
					cout << "Y has won." << endl;
				}
				else if (array[0][1] == 'Y' & array[1][1] == 'Y' & array[2][1] == 'Y')
				{
					cout << "Y has won." << endl;
				}
				else if (array[0][0] == 'Y' & array[1][0] == 'Y' & array[2][0] == 'Y')
				{
					cout << "Y has won." << endl;
				}

				else if (array[0][0] == 'Y' & array[1][1] == 'Y' & array[2][2] == 'Y')
				{
					cout << "Y has won." << endl;
				}

				else if (array[0][2] == 'Y' & array[1][1] == 'Y' & array[2][2] == 'Y')
				{
					cout << "Y has won." << endl;

				}
			}
			else
				cout << " INVALID ENTRY" << endl;

		}
	}

If you want to test that the user gave good input just use a while loop and test if the data is good. It would look something like:

1
2
3
4
5
6
7
	cout << "Please pick row number." << endl;
	cin >> row;
	while (row < 0 || row >2) {
		cout << "Invalid Input" << endl;
		cout << "Please pick row number." << endl;
		cin >> row;
	}


You don't need to worry about them entering a character because if you looking for an int and a char is supplied, using ASCII, it is converted to its integer equivalent which will be greater then 2.
Thank you!!! I actually just did figured it out but I did it a completely different way. Now I cant get my counter to work properly. Kind of stressing because i'm nearing my due date. I don't know why the counter is ending after only two rounds even though it is set to 14.

#include <iostream>
#include <process.h>
#include <string>

using namespace std;

const int ROWS = 3;
const int COLS = 3;
void gameplay1(char array[][COLS], int);
void gameplay2(char array[][COLS], int);
void board(char array[][COLS],int);

int main()
{

int count = 0;
char array[3][3] = { '*','*','*','*','*','*','*','*','*' };
cout << "Welcome to Tic Tac Toe!" << endl;
board(array, ROWS);
for (int count = 1; count < 14; count++)
{

gameplay1(array, ROWS), count++;
gameplay2(array, ROWS), count++;
if (count = 14)
cout << "Game Draw! No spaces left." << endl;
system("pause");
exit(0);
}



}

void board(char array[][COLS],int rows)
{

for (int i = 0; i < rows; i++)
{
for (int j = 0; j < rows; j++)
{
cout << array[i][j] << " ";
}
cout << endl;
}


}

void gameplay1(char array[][COLS], int rows)
{
int row;
int col;
int count = 0;
cout << endl;



cout << "Player 1 you are X." << endl;
cout << "Please pick row number." << endl;
cin >> row;
cout << "Please pick a column number." << endl;
cin >> col;

if (row == 0 || row == 1 || row == 2 || col == 0 || col == 1 || col == 2)
{

if (array[row][col] == '*')
{
array[row][col] = 'X';
board(array, ROWS);
}
else
{
cout << "This position is taken, please pick another" << endl;
return gameplay1(array, ROWS);

}
if (array[0][0] == 'X' & array[0][1] == 'X' & array[0][2] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}

else if (array[1][0] == 'X' && array[1][1] == 'X' && array[1][2] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}
else if (array[2][0] == 'X' && array[2][1] == 'X' && array[2][2] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}
else if (array[0][0] == 'X' && array[1][0] == 'X' && array[2][0] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}
else if (array[0][1] == 'X' && array[1][1] == 'X' && array[2][1] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}
else if (array[0][0] == 'X' && array[1][0] == 'X' && array[2][0] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}

else if (array[0][0] == 'X' && array[1][1] == 'X' && array[2][2] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}

else if (array[0][2] == 'X' && array[1][1] == 'X' && array[2][2] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);

}
}
else
{
cout << " INVALID ENTRY" << endl;
return gameplay1(array, ROWS);
}
}
void gameplay2(char array[][COLS], int)
{
int row;
int col;
cout << "Player 2, you are O." << endl;
cout << "Please pick row number." << endl;
cin >> row;
cout << "Please pick a column number." << endl;
cin >> col;

if (row == 0 || row == 1 || row == 2 || col == 0 || col == 1 || col == 2)
{
if (array[row][col] == '*')
{
array[row][col] = 'O';
board(array, ROWS);
}
else
{
cout << "This position is taken, please pick another" << endl;
return gameplay2(array, ROWS);
}
if (array[0][0] == 'Y' & array[0][1] == 'Y' & array[0][2] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}

else if (array[1][0] == 'X' & array[1][1] == 'X' & array[1][2] == 'X')
{
cout << "Y has won." << endl;
exit(0);
}
else if (array[2][0] == 'Y' & array[2][1] == 'Y' & array[2][2] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}
else if (array[0][0] == 'Y' & array[1][0] == 'Y' & array[2][0] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}
else if (array[0][1] == 'Y' & array[1][1] == 'Y' & array[2][1] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}
else if (array[0][0] == 'Y' & array[1][0] == 'Y' & array[2][0] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}

else if (array[0][0] == 'Y' & array[1][1] == 'Y' & array[2][2] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}

else if (array[0][2] == 'Y' & array[1][1] == 'Y' & array[2][2] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}
}

else
{
cout << " INVALID ENTRY" << endl;
return gameplay2(array, ROWS);
}
}

Last edited on
I see a few issues.

if (count = 14) You are actually setting count to 14. It should be if (count ==14), you use == to check for equality.

Also you need to look at the placement of your braces {}. The way you have them now the exit (0) command will execute the first time through your loop and exit the program. I think you need brackets around the instructions you want to execute after the nested if statement like :
if (count == 14){
cout << "....";
system("pause");
exit (0);
} //this closes the nested or second if statement
} // this closes the first if loop statement
return 0; //returns 0 to the main function, signifies program ended successfully
} // closes main function

Last edited on
Thank you! so I changed what you said but I think I have the counter wrong, it needs to go 9 rounds. The game is tic tac toe so after the 9th turn you have ran out of spots to place the X or O. So after 9 turns I would like to prompt the users that the game is a draw, however The count either ends at 8 turns and not 9 no matter what I try.

#include <iostream>
#include <process.h>
#include <string>

using namespace std;

const int ROWS = 3;
const int COLS = 3;
void gameplay1(char array[][COLS], int);
void gameplay2(char array[][COLS], int);
void board(char array[][COLS],int);

int main()
{

int count = 0;
char array[3][3] = { '*','*','*','*','*','*','*','*','*' };
cout << "Welcome to Tic Tac Toe!" << endl;
board(array, ROWS);

for (int count = 1; count < 13; count++)
{

gameplay1(array, ROWS), count++;
gameplay2(array, ROWS), count++;
if (count == 13)
{
cout << "Game Draw! No spaces left." << endl;
system("pause");
exit(0);
}
}



}

void board(char array[][COLS],int rows)
{

for (int i = 0; i < rows; i++)
{
for (int j = 0; j < rows; j++)
{
cout << array[i][j] << " ";
}
cout << endl;
}


}

void gameplay1(char array[][COLS], int rows)
{
int row;
int col;
int count = 0;
cout << endl;



cout << "Player 1 you are X." << endl;
cout << "Please pick row number." << endl;
cin >> row;
cout << "Please pick a column number." << endl;
cin >> col;

if (row == 0 || row == 1 || row == 2 || col == 0 || col == 1 || col == 2)
{

if (array[row][col] == '*')
{
array[row][col] = 'X';
board(array, ROWS);
}
else
{
cout << "This position is taken, please pick another" << endl;
return gameplay1(array, ROWS);

}
if (array[0][0] == 'X' & array[0][1] == 'X' & array[0][2] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}

else if (array[1][0] == 'X' && array[1][1] == 'X' && array[1][2] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}
else if (array[2][0] == 'X' && array[2][1] == 'X' && array[2][2] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}
else if (array[0][0] == 'X' && array[1][0] == 'X' && array[2][0] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}
else if (array[0][1] == 'X' && array[1][1] == 'X' && array[2][1] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}
else if (array[0][0] == 'X' && array[1][0] == 'X' && array[2][0] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}

else if (array[0][0] == 'X' && array[1][1] == 'X' && array[2][2] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);
}

else if (array[0][2] == 'X' && array[1][1] == 'X' && array[2][2] == 'X')
{
cout << "X has won." << endl;
system("pause");
exit(0);

}
}
else
{
cout << " INVALID ENTRY" << endl;
return gameplay1(array, ROWS);
}
}
void gameplay2(char array[][COLS], int)
{
int row;
int col;
cout << "Player 2, you are O." << endl;
cout << "Please pick row number." << endl;
cin >> row;
cout << "Please pick a column number." << endl;
cin >> col;

if (row == 0 || row == 1 || row == 2 || col == 0 || col == 1 || col == 2)
{
if (array[row][col] == '*')
{
array[row][col] = 'O';
board(array, ROWS);
}
else
{
cout << "This position is taken, please pick another" << endl;
return gameplay2(array, ROWS);
}
if (array[0][0] == 'Y' & array[0][1] == 'Y' & array[0][2] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}

else if (array[1][0] == 'X' & array[1][1] == 'X' & array[1][2] == 'X')
{
cout << "Y has won." << endl;
exit(0);
}
else if (array[2][0] == 'Y' & array[2][1] == 'Y' & array[2][2] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}
else if (array[0][0] == 'Y' & array[1][0] == 'Y' & array[2][0] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}
else if (array[0][1] == 'Y' & array[1][1] == 'Y' & array[2][1] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}
else if (array[0][0] == 'Y' & array[1][0] == 'Y' & array[2][0] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}

else if (array[0][0] == 'Y' & array[1][1] == 'Y' & array[2][2] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}

else if (array[0][2] == 'Y' & array[1][1] == 'Y' & array[2][2] == 'Y')
{
cout << "Y has won." << endl;
exit(0);
}
}

else
{
cout << " INVALID ENTRY" << endl;
return gameplay2(array, ROWS);
}
}

Try this, I changed you for loop to a while loop and changed the way your count variable is incremented.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int main()
{

	unsigned int count = 0;
	char array[3][3] = { '*','*','*','*','*','*','*','*','*' };
	cout << "Welcome to Tic Tac Toe!" << endl;
	board(array, ROWS);

	while (true)
	{
		gameplay1(array, ROWS), ++count;
		if (count == 9)
		{
			cout << "Game Draw! No spaces left." << endl;
			system("pause");
			exit(0);
		}
		gameplay2(array, ROWS), ++count;
	}
	return 0;
}
Last edited on
Topic archived. No new replies allowed.