Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I am trying to use nested for loops to populate an array. It compiles but gives an error on execution. I am using bloodshed IDE (mingw) compiler. Any idea what I am doing wrong?
Here is the code snippet
char grid [9][9];
for (int i = 0; i <= 9; i++){
for (int ii = 0; ii <= 9; ii++){
grid[i][ii] = '.';
}

Try using
char grid [9][9];
for (int i = 0; i <= 8; i++){
for (int ii = 0; ii <= 8; ii++){
grid[i][ii] = '.';
}
You have 9 rows and you start with zero, so you want to end at 8.

Take out the equal signs. Your indeces are from 0 ... 8 grid[9][9] is an array out of bounds exception, which I presume is the error you get. Also, so are grid[9][x], grid[x][9]
You can also initialize (fill up your array) by hard coding it:
grid[9][9]={'.', '.', '.', '.', '.', '.', '.', '.','.',
'.', '.', '.', '.', '.', '.', '.', '.','.',
'.', '.', '.', '.', '.', '.', '.', '.','.',
'.', '.', '.', '.', '.', '.', '.', '.','.',
'.', '.', '.', '.', '.', '.', '.', '.','.',
'.', '.', '.', '.', '.', '.', '.', '.','.',
'.', '.', '.', '.', '.', '.', '.', '.','.',
'.', '.', '.', '.', '.', '.', '.', '.','.',
'.', '.', '.', '.', '.', '.', '.', '.', '.'
};

![]() |
grand total in mysql
|
VB.NET output from DataGr...
|

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |