Problems with two tic tac toe program

So i have to code a tic tac toe, and i'm having problems when i try to overwrite some of the char values in the matrix, for example, when i try to place an 'X' line 1, column 0 (game[1][0]) the values of booth game[1][0] and game[0][2] get overwritten by 'X', someone help plz

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
#include <iostream>
using namespace std;

int main(){
	int x=0,y=0;
	char game[2][2];
	
	for(int i=0;i<=2;i++){
		for(int j=0;j<=2;j++){
			game[i][j]='*';
		}
	}
	for(int i=0;i<=2;i++){
		cout<<endl;
		for(int j=0;j<=2;j++){
			cout<<game[i][j];
		}
	}
	
	while(true){
		cout<<"\nInput Line: \n";
		cin>>x;
		cout<<"Input Column: \n";
		cin>>y;
		game[x][y]='X';
		
		for(int i=0;i<=2;i++){
		cout<<endl;
		for(int j=0;j<=2;j++){
			cout<<game[i][j];
		}
	}
	}
	
	return 0;
}
You're going out of bounds of your array.
Since this is a tic tac toe board... Make your game be int game[3][3]. Valid indices are then 0, 1, 2 for each dimension.
oh i was thinking that the array also started counting from 0, thanks!
oh i was thinking that the array also started counting from 0, thanks!


Array indices do start counting from 0, yes.

However, the numbers you use in the declaration are the sizes of the array. 3 elements is still 3 elements, regardless of whether you start counting the indices from 0, 1, or 152434436645.
Topic archived. No new replies allowed.