Tic-Tac-Toe Input Validation

Hello. This is my first time posting on this website and I would like some help with C++ Programming. I am a beginner programmer and I need more practice. Please look over the code below. I need help with the input validation if the user inputs a letter. As of now, the code loops infinitely if I type something other than 1 or 2.

#include <iostream>
using namespace std;

char space[10] = { 'o','1','2','3','4','5','6','7','8','9' };

int status();
void board();
int gameplay();
int reset();

int main()
{
int again;
do
{
gameplay();
again = reset();
} while (again == 1);

return 0;
}

// Function to check the status of the game if Player has won, or a draw has been met

int status()
{
if (space[1] == space[2] && space[2] == space[3])

return 1;
else if (space[4] == space[5] && space[5] == space[6])

return 1;
else if (space[7] == space[8] && space[8] == space[9])

return 1;
else if (space[1] == space[4] && space[4] == space[7])

return 1;
else if (space[2] == space[5] && space[5] == space[8])

return 1;
else if (space[3] == space[6] && space[6] == space[9])

return 1;
else if (space[1] == space[5] && space[5] == space[9])

return 1;
else if (space[3] == space[5] && space[5] == space[7])

return 1;
else if (space[1] != '1' && space[2] != '2' && space[3] != '3'
&& space[4] != '4' && space[5] != '5' && space[6] != '6'
&& space[7] != '7' && space[8] != '8' && space[9] != '9')

return 0;
else
return -1;
}


// Function that creates the Tic-Tac-Toe Board

void board()
{
system("cls");

cout << " | | " << endl;
cout << " " << space[1] << " | " << space[2] << " | " << space[3] << endl;

cout << "_____|_____|_____" << endl;
cout << " | | " << endl;

cout << " " << space[4] << " | " << space[5] << " | " << space[6] << endl;

cout << "_____|_____|_____" << endl;
cout << " | | " << endl;

cout << " " << space[7] << " | " << space[8] << " | " << space[9] << endl;

cout << " | | " << endl << endl;
}

int gameplay()
{
int player = 1, determinant, choice;

char selection;
do
{
board();
if ((player = (player % 2)))
player = 1;
else
player = 2;

cout << "Player " << player << ", enter a number: ";
cin >> choice;

if ((selection = (player == 1)))
selection = 'X';
else
selection = 'O';

if (choice == 1 && space[1] == '1')

space[1] = selection;
else if (choice == 2 && space[2] == '2')

space[2] = selection;
else if (choice == 3 && space[3] == '3')

space[3] = selection;
else if (choice == 4 && space[4] == '4')

space[4] = selection;
else if (choice == 5 && space[5] == '5')

space[5] = selection;
else if (choice == 6 && space[6] == '6')

space[6] = selection;
else if (choice == 7 && space[7] == '7')

space[7] = selection;
else if (choice == 8 && space[8] == '8')

space[8] = selection;
else if (choice == 9 && space[9] == '9')

space[9] = selection;
else
{
cout << "The space selected is an invalid move, please press ENTER and select again.";

player--;
cin.ignore();
cin.get();
}
determinant = status();

player++;
} while (determinant == -1);
board();
if (determinant == 1)

cout << "==>\aPlayer " << --player << " is victorious! ";
else
cout << "==>\aGame draw";

cin.ignore();
cin.get();
return 0;
}

// Function to ask User(s) for another game and reset board
int reset()
{
int again;
cout << "Do you wish to play another game? (1 for Yes, 2 for No)" << endl;
cin >> again;
space[0] = { 'o' };
space[1] = { '1' };
space[2] = { '2' };
space[3] = { '3' };
space[4] = { '4' };
space[5] = { '5' };
space[6] = { '6' };
space[7] = { '7' };
space[8] = { '8' };
space[9] = { '9' };

//Input Validation for letters and numbers
while (again < 1 || again > 2)
{
cout << "Invalid! Please enter 1 or 2." << endl;
cin >> again;
}

return again;
}
The reason why you will infinitely loop when you type anything other than 1 or 2 is because of the following code:
1
2
3
4
5
while (again < 1 || again > 2)
{
cout << "Invalid! Please enter 1 or 2." << endl;
cin >> again;
}


other than that I am not sure what you are talking about.

Please also make sure you use the tags code /code with square brakets ([) when you type code into this forum.

-Hirokachi
closed account (48T7M4Gy)
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
#include <iostream>
using namespace std;

char space[10] = { 'o','1','2','3','4','5','6','7','8','9' };

int status();
void board();
int gameplay();
int reset();

int main()
{
int again;
do
{
gameplay();
again = reset();
} while (again == 1);

return 0;
}

// Function to check the status of the game if Player has won, or a draw has been met

int status()
{
if (space[1] == space[2] && space[2] == space[3])

return 1;
else if (space[4] == space[5] && space[5] == space[6])

return 1;
else if (space[7] == space[8] && space[8] == space[9])

return 1;
else if (space[1] == space[4] && space[4] == space[7])

return 1;
else if (space[2] == space[5] && space[5] == space[8])

return 1;
else if (space[3] == space[6] && space[6] == space[9])

return 1;
else if (space[1] == space[5] && space[5] == space[9])

return 1;
else if (space[3] == space[5] && space[5] == space[7])

return 1;
else if (space[1] != '1' && space[2] != '2' && space[3] != '3'
&& space[4] != '4' && space[5] != '5' && space[6] != '6'
&& space[7] != '7' && space[8] != '8' && space[9] != '9')

return 0;
else
return -1;
}


// Function that creates the Tic-Tac-Toe Board

void board()
{
system("cls");

cout << " | | " << endl;
cout << " " << space[1] << " | " << space[2] << " | " << space[3] << endl;

cout << "_____|_____|_____" << endl;
cout << " | | " << endl;

cout << " " << space[4] << " | " << space[5] << " | " << space[6] << endl;

cout << "_____|_____|_____" << endl;
cout << " | | " << endl;

cout << " " << space[7] << " | " << space[8] << " | " << space[9] << endl;

cout << " | | " << endl << endl;
}

int gameplay()
{
int player = 1, determinant, choice;

char selection;
do
{
board();
if ((player = (player % 2)))
player = 1;
else
player = 2;

cout << "Player " << player << ", enter a number: ";
cin >> choice;

if ((selection = (player == 1)))
selection = 'X';
else
selection = 'O';

if (choice == 1 && space[1] == '1')

space[1] = selection;
else if (choice == 2 && space[2] == '2')

space[2] = selection;
else if (choice == 3 && space[3] == '3')

space[3] = selection;
else if (choice == 4 && space[4] == '4')

space[4] = selection;
else if (choice == 5 && space[5] == '5')

space[5] = selection;
else if (choice == 6 && space[6] == '6')

space[6] = selection;
else if (choice == 7 && space[7] == '7')

space[7] = selection;
else if (choice == 8 && space[8] == '8')

space[8] = selection;
else if (choice == 9 && space[9] == '9')

space[9] = selection;
else
{
cout << "The space selected is an invalid move, please press ENTER and select again.";

player--;
cin.ignore();
cin.get();
}
determinant = status();

player++;
} while (determinant == -1);
board();
if (determinant == 1)

cout << "==>\aPlayer " << --player << " is victorious! ";
else
cout << "==>\aGame draw";

cin.ignore();
cin.get();
return 0;
}

// Function to ask User(s) for another game and reset board
int reset()
{
int again;
cout << "Do you wish to play another game? (1 for Yes, 2 for No)" << endl;
cin >> again;
space[0] = { 'o' };
space[1] = { '1' };
space[2] = { '2' };
space[3] = { '3' };
space[4] = { '4' };
space[5] = { '5' };
space[6] = { '6' };
space[7] = { '7' };
space[8] = { '8' };
space[9] = { '9' };

//Input Validation for letters and numbers
while (again < 1 || again > 2)
{
cout << "Invalid! Please enter 1 or 2." << endl;
cin >> again;
}

return again;
}
I have one for you against a computer. I will post it
Also has time functions in it......
Sorry, but I can not post it.
will send link
Link is. http://www.programgames.com/page4.html
download forth edition
Topic archived. No new replies allowed.