for looping a matrix to check user input

Hey guys,

I'm messing around with a tic tac toe game using c++ and i've created a function for the user input. Instead of using numerous if statements to check the 3x3 matrix numbers (there would be 9) I'm trying to put this into a loop and have this so far. Not sure i'f i'm going about this wrong but nothing is happening when trying to input the 'X'.

This is the function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20

// this is the array
char board[3][3] = { '1', '2', '3', '4', '5', '6', '7', '8', '9' };

  void Input()
{
	int a;
	cout << "Enter the number of the cell you wish to place: ";
	cin >> a;
	for (int i = 0; i < 9; i++)
	{
		for (int j = 0; j < 9; j++)
		{
			if (board[i][j] == a)
			{
				board[i][j] = player;
			}
		}
	}
}
Last edited on
First off, on line 14, you are comparing a character (board) against and integer (a).
yeah you are right, for some reason it works with:

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
void Input()
{
	int a;
	cout << "Enter the number of the cell you wish to place: ";
	cin >> a;
	/*
	for (int i = 0; i < 9; i++)
	{
		for (int j = 0; j < 9; j++)
		{
			if (board[i][j] == a)
			{
				board[i][j] = player;
			}
		}
	}*/
	if (a == 1)
	{
		board[0][0] = player;
	}
	else if (a == 2)
	{
		board[0][1] = player;
	}
	else if (a == 3)
	{
		board[0][2] = player;
	}
	else if (a == 4)
	{
		board[1][0] = player;
	}
	else if (a == 5)
	{
		board[1][1] = player;
	}
	else if (a == 6)
	{
		board[1][2] = player;
	}
	else if (a == 7)
	{
		board[2][0] = player;
	}
	else if (a == 8)
	{
		board[2][1] = player;
	}
	else if (a == 9)
	{
		board[2][2] = player;
	}
}


but i'd rather just do it with a loop if possible
Topic archived. No new replies allowed.