Need help with tic tac toe program

I made a tic tac toe game program. Whenever a player makes a move it tells the new field statistics on new lines. How can I make it so that it does not use new lines but changes the previous lines to give the new field statistics.

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
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
int main()
{
	ifstream in;
	ofstream out;
	string x[3][3];
	for (int i = 0; i < 3; i++)
	{
		for (int j = 0; j < 3; j++)
		{
			x[i][j] =".";
		}
	}
	out.open ("rec.txt");
	out << "";
	out.close();
	string a, b, c, d;
	cout << "Player 1,enter your name";
	cin >> a;
	cout << "Player 2, enter your name";
	cin >> b;
	int row, col;
	for (int i = 0; i < 9; i++)
	{
		in.open("rec.txt");
		in >> c;
		in.close();
		if (c == "" || c == b)
		{
			d = a;
		}
		else
		{
			d = b;
		}
		do{
			cout << d << ", Enter your row and column" << endl;
			cout << "Row";
			cin >> row;
			cout << "Column";
			cin >> col;
		} while (x[row - 1][col - 1] == "o" || x[row - 1][col - 1] == "x" || row<1 || row>3 || col<1 || col>3);
		if (d == a)
		{
			x[row - 1][col - 1] = "x";
		}
		if (d == b)
		{
			x[row - 1][col - 1] = "o";
		}
		out.open("rec.txt");
		out << d;
		out.close();
		if ((x[0][0] == "x"&&x[0][1] == "x"&&x[0][2] == "x") || (x[1][0] == "x"&&x[1][1] == "x"&&x[1][2] == "x") || (x[2][0] == "x"&&x[2][1] == "x"&&x[2][2] == "x") || (x[0][0] == "x"&&x[1][0] == "x"&&x[2][0] == "x") || (x[0][1] == "x"&&x[1][1] == "x"&&x[2][1] == "x") || (x[0][2] == "x"&&x[1][2] == "x"&&x[2][2] == "x") || (x[0][0] == "x"&&x[1][1] == "x"&&x[2][2] == "x") || (x[0][2] == "x"&&x[1][1] == "x"&&x[2][0] == "x") || (x[0][0] == "o"&&x[0][1] == "o"&&x[0][2] == "o") || (x[1][0] == "o"&&x[1][1] == "o"&&x[1][2] == "o") || (x[2][0] == "o"&&x[2][1] == "o"&&x[2][2] == "o") || (x[0][0] == "o"&&x[1][0] == "o"&&x[2][0] == "o") || (x[0][1] == "o"&&x[1][1] == "o"&&x[2][1] == "o") || (x[0][2] == "o"&&x[1][2] == "o"&&x[2][2] == "o") || (x[0][0] == "o"&&x[1][1] == "o"&&x[2][2] == "o") || (x[0][2] == "o"&&x[1][1] == "o"&&x[2][0] == "o"))
		{
			for (int i = 0; i < 3; i++)
			{
				for (int j = 0; j < 3; j++)
				{
					cout << x[i][j] << "\t";
				}
				cout << "\n";
			}
			cout << d << " has won the game";
			break;
		}
		else
		{
			if (i == 8)
			{
				
				for (int i = 0; i < 3; i++)
				{
					for (int j = 0; j < 3; j++)
					{
						cout << x[i][j] << "\t";
					}
					cout << "\n";
				}
				cout << "Draw game.";
				break;
			}
		}
		for (int i = 0; i < 3; i++)
		{
			for (int j = 0; j < 3; j++)
			{
				cout << x[i][j] << "\t";
			}
			cout << "\n";
		}
	}	
	system("pause");
}
closed account (3qX21hU5)
You can't. Once it has been printed to the console you can't change it. Though you could clear the screen and reprint the whole board with the updated board. This will give the illusion that it is replacing stuff.

If you are just messing around with your own stuff you could use system calls to do this but I would highly highly recommend you not get into the habit of doing so.

Instead you can use platform specific to clear the screen. Here is a tutorial which shows the options http://www.cplusplus.com/articles/4z18T05o/ .
Last edited on
Topic archived. No new replies allowed.