How to clear cin inputs

I am wanting to clear all previous cin inputs after Menu() and Names() I have tried things such as cin.clear() and fflush(stdin) without anything happening.
here is my code for an idea:
When a player enters 'Y' then Display() should show clearing all inputs done to the grid so that players may play again.

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
  int main()
{
	system("cls");
	Menu();
	Names();
	string choice2;
	b = 0;
	Display();
	//loop that will run until a player has won or tied
	while (1)
	{
		b++;
		Input();
		Display();
		//if player wins/tie function
		//if X wins
		if (Win() == 'X')
		{
			system("cls");
			cout << X << " has Won!" << endl;
			cout << "Would you like to Play again, y/n." << endl;
			cin >> choice2;
			if (choice2 == "y" || choice2 == "Y")
			{
				Display();
			}
			else if (choice2 == "n" || choice2 == "N")
			{
				return 0;
			}
		}
		//if O wins
		else if (Win() == 'O')
		{
			system("cls");
			cout << O << " has Won!" << endl;
			cout << "Would you like to Play again, y/n." << endl;
			cin >> choice2;
			if (choice2 == "y" || choice2 == "Y")
			{
				Display();
			}
			else if (choice2 == "N" || choice2 == "n")
			{
				return 0;
			}
		}
		//if players tie
		else if (Win() == '/' && b == 9)
		{
			system("cls");
			cout << "It's a Tie!" << endl;
			cout << "Would you like to Play again, y/n." << endl;
			cin >> choice2;
			if (choice2 == "y" || choice2 == "Y")
			{
				Display();
			}
			else if (choice2 == "N" || choice2 == "n")
			{
					return 0;
			}
		}
		TogglePlayer();
	}
}
I really don't understand what you are asking. Clear what? - the screen? - some board? Please explain. (Also, provide some simplified, but compileable, code to illustrate your "idea".)

Unless you are a Time Lord I can't see that you can take back what you have already entered at the keyboard.
To give you the run down, it is a tic-tac-toe game for which I have made a 3x3 grid which 2 players can fill in, once one player has 3 in a row the Win() function declares they have won, after this it will ask whether to play again or not, by selecting yes it will clear the grid that the players had just filled in and display the grid as it was before it was filled in so that they can play again, if you would like to take a look, here is my full code:
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include <iostream>
#include <string>
using namespace std;

//array to create the rows and columns for which players may choose to put their symbols in from 1-9
char grid[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
//what the default player is
char player = 'X';
//Players names
string X = "Player1";
string O = "Player2";
//integer for when players tie
int b;
//Players can choose to play game or quit the game.
void Menu()
{
	system("cls");
	cout << "---------------------------------" << endl;
	cout << "            MAIN MENU"             << endl;
	cout << "---------------------------------" << endl;
	cout << endl;
	cout << "Hello! and welcome to tic-tac-toe!" << endl;
	cout << endl;
	cout << "To play the game, simply type 'y'." << endl;
	cout << "Or if  you wish to quit, type 'n'." << endl;
	string choice1;
	cout << "Enter here: "; 
	cin >> choice1;
	if (choice1 == "Y" || choice1 == "y")
	{
		return;
	}
	else if (choice1 == "N" || choice1 == "n")
	{
		exit(0);
	}
	else
	{
		cout << "That is not the correct input, please try again." << endl;
		system("PAUSE");
		Menu();
	}
}
//Players enter names
void Names()
{
	system("cls");
	cout << "Player 1 enter your name: ";
	cin >> X;
	cout << "Player 2 enter your name: ";
	cin >> O;
}
//what the game will display
void Display()
{
	//so that there is only one grid being displayed (clears previous display once an input is executed)
	system("cls");

	cout << "-------------------------------------------------------------------------------" << endl;
	cout << "Welcome to Tic-Tac-Toe!       Player 1: " << X << "  Player 2: " << O << endl;
	cout << "-------------------------------------------------------------------------------" << endl;
	cout << X << " goes first." << endl;
	//r = row c = column
	for (int r = 0; r < 3; r++)
	{
		for (int c = 0; c < 3; c++)
		{
			cout << grid[r][c] << " ";
		}
		cout << endl;
	}
}
//what the players will be inputting
void Input()
{
	int a;
	//players turn
	cout << "It's " << player << "'s turn. " << "Type the number of the field: ";
	cin >> a;

	if (a == 1)
		//if a field is already in use
	{
		if (grid[0][0] == '1')
			grid[0][0] = player;
		else
		{
			cout << "That field is already in use, please try again!" << endl;
			Input();
		}
	}
	else if (a == 2)
	{
		if (grid[0][1] == '2')
			grid[0][1] = player;
		else
		{
			cout << "That field is already in use, please try again!" << endl;
			Input();
		}
	}
	else if (a == 3)
	{
		if (grid[0][2] == '3')
			grid[0][2] = player;
		else
		{
			cout << "That field is already in use, please try again!" << endl;
			Input();
		}
	}
	else if (a == 4)
	{
		if (grid[1][0] == '4')
			grid[1][0] = player;
		else
		{
			cout << "That field is already in use, please try again!" << endl;
			Input();
		}
	}
	else if (a == 5)
	{
		if (grid[1][1] == '5')
			grid[1][1] = player;
		else
		{
			cout << "That field is already in use, please try again!" << endl;
			Input();
		}
	}
	else if (a == 6)
	{
		if (grid[1][2] == '6')
			grid[1][2] = player;
		else
		{
			cout << "That field is already in use, please try again!" << endl;
			Input();
		}
	}
	else if (a == 7)
	{
		if (grid[2][0] == '7')
			grid[2][0] = player;
		else
		{
			cout << "That field is already in use, please try again!" << endl;
			Input();
		}
	}
	else if (a == 8)
	{
		if (grid[2][1] == '8')
			grid[2][1] = player;
		else
		{
			cout << "That field is already in use, please try again!" << endl;
			Input();
		}
	}
	else if (a == 9)
	{
		if (grid[2][2] == '9')
			grid[2][2] = player;
		else
		{
			cout << "That field is already in use, please try again!" << endl;
			Input();
		}
	}
}
//function to change player
void TogglePlayer()
{
	if (player == 'X')
		player = 'O';
	else
		player = 'X';
}
//function to check if a player has won
int Win()
{
	//checking if 3 in row for Player 1 (X)
	//rows
	if (grid[0][0] == 'X' && grid[0][1] == 'X' && grid[0][2] == 'X')
		return 'X';
	if (grid[1][0] == 'X' && grid[1][1] == 'X' && grid[1][2] == 'X')
		return 'X';
	if (grid[2][0] == 'X' && grid[2][1] == 'X' && grid[2][2] == 'X')
		return 'X';

	//columns
	if (grid[0][0] == 'X' && grid[1][0] == 'X' && grid[2][0] == 'X')
		return 'X';
	if (grid[0][1] == 'X' && grid[1][1] == 'X' && grid[2][1] == 'X')
		return 'X';
	if (grid[0][2] == 'X' && grid[1][2] == 'X' && grid[2][2] == 'X')
		return 'X';

	//diagonals
	if (grid[0][0] == 'X' && grid[1][1] == 'X' && grid[2][2] == 'X')
		return 'X';
	if (grid[2][0] == 'X' && grid[1][1] == 'X' && grid[0][2] == 'X')
		return 'X';

	//checking if 3 in row for Player 2 (O)
	//rows
	if (grid[0][0] == 'O' && grid[0][1] == 'O' && grid[0][2] == 'O')
		return 'O';
	if (grid[1][0] == 'O' && grid[1][1] == 'O' && grid[1][2] == 'O')
		return 'O';
	if (grid[2][0] == 'O' && grid[2][1] == 'O' && grid[2][2] == 'O')
		return 'O';
	//columns
	if (grid[0][0] == 'O' && grid[0][1] == 'O' && grid[2][0] == 'O')
		return 'O';
	if (grid[0][1] == 'O' && grid[1][1] == 'O' && grid[2][1] == 'O')
		return 'O';
	if (grid[0][2] == 'O' && grid[1][2] == 'O' && grid[2][2] == 'O')
		return 'O';
	//diagonals
	if (grid[0][0] == 'O' && grid[1][1] == 'O' && grid[2][2] == 'O')
		return 'O';
	if (grid[2][0] == 'O' && grid[1][1] == 'O' && grid[0][2] == 'O')
		return 'O';

	//return default if there is in-correct input
	return '/';
}
int main()
{
	system("cls");
	Menu();
	Names();
	string choice2;
	b = 0;
	Display();
	//loop that will run until a player has won or tied
	while (1)
	{
		b++;
		Input();
		Display();
		//if player wins/tie function
		//if X (Player 1) wins
		if (Win() == 'X')
		{
			system("cls");
			cout << X << " has Won!" << endl;
			cout << "Would you like to Play again, y/n." << endl;
			cin >> choice2;
			if (choice2 == "y" || choice2 == "Y")
			{
				Display();
			}
			else if (choice2 == "n" || choice2 == "N")
			{
				return 0;
			}
		}
		//if O (Player 2) wins
		else if (Win() == 'O')
		{
			system("cls");
			cout << O << " has Won!" << endl;
			cout << "Would you like to Play again, y/n." << endl;
			cin >> choice2;
			if (choice2 == "y" || choice2 == "Y")
			{
				Display();
			}
			else if (choice2 == "N" || choice2 == "n")
			{
				return 0;
			}
		}
		//if players tie
		else if (Win() == '/' && b == 9)
		{
			system("cls");
			cout << "It's a Tie!" << endl;
			cout << "Would you like to Play again, y/n." << endl;
			cin >> choice2;
			if (choice2 == "y" || choice2 == "Y")
			{
				Display();
			}
			else if (choice2 == "N" || choice2 == "n")
			{
					return 0;
			}
		}
		TogglePlayer();
	}
}
Thank-you for the runnable code.

You basically need to reset the values in grid[r][c] back to their initial values whenever you want to start a new game.

Create a new function setGrid() as below and put it somewhere above main.
1
2
3
4
5
6
7
8
9
10
11
12
void setGrid()
{
        int num = 0;
        for (int r = 0; r < 3; r++)
        {
                for (int c = 0; c < 3; c++)
                {
                        num++;
                        grid[r][c] = '0' + num;      // resets to initial chars 1-9
                }
        }
}


Call it with the line
setGrid();
after each of your three
1
2
		if (choice2 == "y" || choice2 == "Y")
			{

pairs of lines in int main(). Then it will regenerate the original grid before you start a new game.

Last edited on
Your a life savour!

Also, with doing this once the grid resets I am needing the players to also reset, in other words I need it so player 1 or X is the first to go always, is there anyway I can do this as currently it just goes to whomever was the next player to go.
You need to:
(1) Put your call to TogglePlayer() in an else{} block on its own.
(2) Set player = 'X' in setGrid() (or any other place where it will be run on starting a new game).
When you say put my TogglePlayer() in an else block are you meaning put a else block around its current position at the bottom of main such as:
1
2
3
4
5
6
		else
		{
			TogglePlayer();
		}
	}
}
Okay, I have managed to get it all working how I had wanted but now when I choose 'Y' to play again and enter a number to put my X into one of the grids nothing happens the it just goes down to the next line... if you wanna see for yourself:

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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
#include <iostream>
#include <string>
using namespace std;

//array to create the rows and columns for which players may choose to put their symbols in from 1-9
char grid[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };
//what the default player is
char player = 'X';
//Players names
string X = "Player1";
string O = "Player2";
//integer for when players tie
int b;
//Players can choose to play game or quit the game.
void Menu()
{
	system("cls");
	cout << "---------------------------------" << endl;
	cout << "            MAIN MENU"             << endl;
	cout << "---------------------------------" << endl;
	cout << endl;
	cout << "Hello! and welcome to tic-tac-toe!" << endl;
	cout << endl;
	cout << "To play the game, simply type 'y'." << endl;
	cout << "Or if  you wish to quit, type 'n'." << endl;
	string choice1;
	cout << "Enter here: "; 
	cin >> choice1;
	if (choice1 == "Y" || choice1 == "y")
	{
		return;
	}
	else if (choice1 == "N" || choice1 == "n")
	{
		exit(0);
	}
	else
	{
		cout << "That is not the correct input, please try again." << endl;
		system("PAUSE");
		Menu();
	}
}
//Players enter names
void Names()
{
	system("cls");
	cout << "Player 1 enter your name: ";
	cin >> X;
	cout << "Player 2 enter your name: ";
	cin >> O;
}
//what the game will display
void Display()
{
	//so that there is only one grid being displayed (clears previous display once an input is executed)
	system("cls");

	cout << "-------------------------------------------------------------------------------" << endl;
	cout << "Welcome to Tic-Tac-Toe!       Player 1: " << X << "  Player 2: " << O << endl;
	cout << "-------------------------------------------------------------------------------" << endl;
	cout << X << " goes first." << endl;
	//r = row c = column
	for (int r = 0; r < 3; r++)
	{
		for (int c = 0; c < 3; c++)
		{
			cout << grid[r][c] << " ";
		}
		cout << endl;
	}
}
//what the players will be inputting
void Input()
{
	int a;
	//players turn
	cout << "It's " << player << "'s turn. " << "Type the number of the field: ";
	cin >> a;

	if (a == 1)
		//if a field is already in use
	{
		if (grid[0][0] == '1')
			grid[0][0] = player;
		else
		{
			cout << "That field is already in use, please try again!" << endl;
			Input();
		}
	}
	else if (a == 2)
	{
		if (grid[0][1] == '2')
			grid[0][1] = player;
		else
		{
			cout << "That field is already in use, please try again!" << endl;
			Input();
		}
	}
	else if (a == 3)
	{
		if (grid[0][2] == '3')
			grid[0][2] = player;
		else
		{
			cout << "That field is already in use, please try again!" << endl;
			Input();
		}
	}
	else if (a == 4)
	{
		if (grid[1][0] == '4')
			grid[1][0] = player;
		else
		{
			cout << "That field is already in use, please try again!" << endl;
			Input();
		}
	}
	else if (a == 5)
	{
		if (grid[1][1] == '5')
			grid[1][1] = player;
		else
		{
			cout << "That field is already in use, please try again!" << endl;
			Input();
		}
	}
	else if (a == 6)
	{
		if (grid[1][2] == '6')
			grid[1][2] = player;
		else
		{
			cout << "That field is already in use, please try again!" << endl;
			Input();
		}
	}
	else if (a == 7)
	{
		if (grid[2][0] == '7')
			grid[2][0] = player;
		else
		{
			cout << "That field is already in use, please try again!" << endl;
			Input();
		}
	}
	else if (a == 8)
	{
		if (grid[2][1] == '8')
			grid[2][1] = player;
		else
		{
			cout << "That field is already in use, please try again!" << endl;
			Input();
		}
	}
	else if (a == 9)
	{
		if (grid[2][2] == '9')
			grid[2][2] = player;
		else
		{
			cout << "That field is already in use, please try again!" << endl;
			Input();
		}
	}
}
//function to change player
void TogglePlayer()
{
	if (player == 'X')
		player = 'O';
	else
		player = 'X';
}
//function to check if a player has won
int Win()
{
	//checking if 3 in row for Player 1 (X)
	//rows
	if (grid[0][0] == 'X' && grid[0][1] == 'X' && grid[0][2] == 'X')
		return 'X';
	if (grid[1][0] == 'X' && grid[1][1] == 'X' && grid[1][2] == 'X')
		return 'X';
	if (grid[2][0] == 'X' && grid[2][1] == 'X' && grid[2][2] == 'X')
		return 'X';

	//columns
	if (grid[0][0] == 'X' && grid[1][0] == 'X' && grid[2][0] == 'X')
		return 'X';
	if (grid[0][1] == 'X' && grid[1][1] == 'X' && grid[2][1] == 'X')
		return 'X';
	if (grid[0][2] == 'X' && grid[1][2] == 'X' && grid[2][2] == 'X')
		return 'X';

	//diagonals
	if (grid[0][0] == 'X' && grid[1][1] == 'X' && grid[2][2] == 'X')
		return 'X';
	if (grid[2][0] == 'X' && grid[1][1] == 'X' && grid[0][2] == 'X')
		return 'X';

	//checking if 3 in row for Player 2 (O)
	//rows
	if (grid[0][0] == 'O' && grid[0][1] == 'O' && grid[0][2] == 'O')
		return 'O';
	if (grid[1][0] == 'O' && grid[1][1] == 'O' && grid[1][2] == 'O')
		return 'O';
	if (grid[2][0] == 'O' && grid[2][1] == 'O' && grid[2][2] == 'O')
		return 'O';
	//columns
	if (grid[0][0] == 'O' && grid[0][1] == 'O' && grid[2][0] == 'O')
		return 'O';
	if (grid[0][1] == 'O' && grid[1][1] == 'O' && grid[2][1] == 'O')
		return 'O';
	if (grid[0][2] == 'O' && grid[1][2] == 'O' && grid[2][2] == 'O')
		return 'O';
	//diagonals
	if (grid[0][0] == 'O' && grid[1][1] == 'O' && grid[2][2] == 'O')
		return 'O';
	if (grid[2][0] == 'O' && grid[1][1] == 'O' && grid[0][2] == 'O')
		return 'O';

	//return default if there is in-correct input
	return '/';
}
//function to reset grid
void resetGrid()
{
	char player = 'X';
	//loop used to reset the grid once player selects to play again
	int num = 0;
	for (int r = 0; r < 3; r++)
	{
		for (int c = 0; c < 3; c++)
		{
			//resets to initial chars 1-9
			num++;
			grid[r][c] = '0' + num;
		}
	}
}
//function to play again
bool playAgain()
{
	string choice2;

	cout << "Would you like to Play again, y/n." << endl;
	cout << "Enter here: ";
	while (true)
	{
		cin >> choice2;
		if (choice2 == "y" || choice2 == "Y")
		{
			char player = 'X';
			resetGrid();
			Display();
			Input();
		}
		else if (choice2 == "n" || choice2 == "N")
			return 0;
		else
		{
			cout << "That is not the correct input, please try again." << endl;
			playAgain();
		}
	}
}

int main()
{
	system("cls");
	Menu();
	Names();
	b = 0;
	Display();
	//loop that will run until a player has won or tied
	while (1)
	{
		b++;
		Input();
		Display();
		//if player wins/tie function
		//if X (Player 1) wins
		if (Win() == 'X')
		{
			system("cls");
			cout << X << " has Won!" << endl;
			playAgain();
		}
		//if O (Player 2) wins
		else if (Win() == 'O')
		{
			system("cls");
			cout << O << " has Won!" << endl;
			playAgain();
		}
		//if players tie
		else if (Win() == '/' && b == 9)
		{
			system("cls");
			cout << "It's a Tie!" << endl;
			playAgain();
		}
		//change players turns
		else
		{
			TogglePlayer();
		}
	}
}
I have managed to get what I initially wanted working somewhat, but now once I have selected to play again and try intputting fields this occurs and I am unsure on how to fix this, it obviously has to do with the play again function but can't seem to tell what it is.
http://prntscr.com/d32ivh

any possible fixes?
Topic archived. No new replies allowed.