I am writing a tic-tac-toe game to learn C++.
I would like to store the game board in a 2D char array:
char playerBoard[5][14] {
{'2',' ','>',' ','_',' ','|',' ','_',' ','|',' ','_','\n'},
{'1',' ','>',' ','_',' ','|',' ','_',' ','|',' ','_','\n'},
{'0',' ','>',' ','_',' ','|',' ','_',' ','|',' ','_','\n'},
{' ',' ',' ',' ','^',' ',' ',' ','^',' ',' ',' ','^','\n'},
{' ',' ',' ',' ','0',' ',' ',' ','1',' ',' ',' ','2','\n'}
};
I would like to print the array with cout:
for (int i = 0; i < 5; i++) {
cout << playerBoard[i];
}
When I try, the entire contents of playerboard
, starting at the first array, are printed with each iteration:
2 > _ | _ | _
1 > _ | _ | _
0 > _ | _ | _
^ ^ ^
0 1 2
1 > _ | _ | _
0 > _ | _ | _
^ ^ ^
0 1 2
0 > _ | _ | _
^ ^ ^
0 1 2
^ ^ ^
0 1 2
0 1 2
The entire contents of playerBoard is printed with:
cout << playerBoard[0];
What is going on here?