C++ Basic Language Problem
Hello I have an infuriating (really) problem with a noughts-and-crosses C++ program. The details of the program are irrelevant but this is what I am doing:
Create array
Get player's input into pos and then branch accordingly. This works well until this line is reached:
(X is a defined constant equal to 5)
Now look at the following screenshot:
![[Image: 33c9q86.png]](http://i56.tinypic.com/33c9q86.png)
(part of this image is cropped please open in a new window to see all of it)
Notice the points of CHECKPOINT A and CHECKPOINT B then look at the terminal output on the right. Somehow grid[1][0] changes! What is happening here?
Create array
Code:
int grid[2][2]Get player's input into pos and then branch accordingly. This works well until this line is reached:
Code:
grid[0][2] = X;Now look at the following screenshot:
![[Image: 33c9q86.png]](http://i56.tinypic.com/33c9q86.png)
(part of this image is cropped please open in a new window to see all of it)
Notice the points of CHECKPOINT A and CHECKPOINT B then look at the terminal output on the right. Somehow grid[1][0] changes! What is happening here?
~ Bring a Pen ~
Arrays in C++ are indexed from 0, so if you have an array with 2 elements, the legal indexes will be 0 and 1; writing to (the illegal) index 2 will write to memory elsewhere in your program, as you've discovered
Amending your declaration of "grid" as below…
…will likely fix the bug.
Amending your declaration of "grid" as below…Code:
int grid[3][3];Mark Bishop
Hahaha what a stupid mistake
. Thanks a bunch.
. Thanks a bunch.
~ Bring a Pen ~

