# Problem with reading from an array

• 04-09-2008
Afro
Problem with reading from an array
As the title suggests im having trouble with writing from an array.

It seems as if even the i give the board[x][y] the correct values it's still reading from
the wrong point, i've looked through my code and cant find anything that's wrong, maybe you can help?

This is a simplified version of the code, with only the areas that are a problem.

Code:

```#include <iostream> #define WHITE_KING 6 // define all chess pieces for ease of use #define WHITE_QUEEN 5 #define WHITE_TOWER 4 #define WHITE_RUNNER 3 #define WHITE_HORSE 2 #define WHITE_PEASANT 1 #define EMPTY 0      #define BLACK_PEASANT -1 #define BLACK_HORSE -2 #define BLACK_RUNNER -3 #define BLACK_TOWER -4 #define BLACK_QUEEN -5 #define BLACK_KING -6 using namespace std; void initboard(void); void drawboard(void); int board[7][7];//global board variable int main() {     initboard();     drawboard();         system("PAUSE");//hold } void initboard(void)//initiate the board {     int x,y;     for(y=0;y<=7;y++)//make sure all values are at zero     {         for(x=0;x<=7;x++)//loop through columns values, then go to the next row and redo         {           board[x][y]=EMPTY;         }     }     for(x=0;x<=7;x++)board[x][1]=WHITE_PEASANT;//white should be at the top rows     for(x=0;x<=7;x++)board[x][6]=BLACK_PEASANT;//black should be at the bottom rows         board[0][7]=BLACK_TOWER;//boring initialization stuff     board[1][7]=BLACK_HORSE;     board[2][7]=BLACK_RUNNER;     board[3][7]=BLACK_QUEEN;     board[4][7]=BLACK_KING;     board[5][7]=BLACK_RUNNER;     board[6][7]=BLACK_HORSE;     board[7][7]=BLACK_TOWER;             board[0][0]=WHITE_TOWER;     board[1][0]=WHITE_HORSE;     board[2][0]=WHITE_RUNNER;     board[3][0]=WHITE_QUEEN;     board[4][0]=WHITE_KING;     board[5][0]=WHITE_RUNNER;     board[6][0]=WHITE_HORSE;     board[7][0]=WHITE_TOWER; } void drawboard(void) {     int x,y;     cout << " ";//proper alignment     for(x=0;x<=7;x++)cout << "  " << x << "  "; // the numbers above     cout << endl;// new row for layout             for(y=0;y<=7;y++)     {       cout << y;//numbers at the side of the grid             for(x=0;x<=7;x++)//loop through columns values, then go to the next row and redo       {       if(board[x][y]>=0)cout << " | " << board[x][y] << " | ";       else cout << " |" << board[x][y] << " | ";//one less space for negative value alignment       }       cout << endl;       } }```

If anyone has encountered this problem (or simply knows that i am doing something wrong) please do tell! I've been trying to fix this for a long while!

• 04-09-2008
mike_g
Your board is sized to be 7 elements wide on each dimension, yet you are trying to deal with a board that is 8*8:
Code:

`for(y=0;y<=7;y++)`
You might want to change the size of your board:
Code:

`int board[8][8];//global board variable`
• 04-09-2008
Dino
Your board is 7X7, so your can't use an index of 7 - you have to stop at 6. Therefore, don't code
Code:

`x <= 7`
code
Code:

`x < 7`
• 04-09-2008
Dino
Duh. Boards are 8X8. Good point Mike!
• 04-09-2008
Afro
oh man, thanks. That's such a silly error to make.
But i thought defining an array as [7][7] would give eight different variables?
As in: 0 1 2 3 4 5 6 7?
Or i might just be totally off there.

EDIT: Because otherwise, i should also have to set up my loops so they go between the values 1 and 8 instead of 0 and 7, correct?
• 04-09-2008
Dino
When you define an array, you specify the number of elements you want: 8 in this case.

When you iterate through an array, since you start at zero, you stop after 8 iterations - which is 7.
• 04-09-2008
Afro
Ah, alright, makes sense now, thanks for the help Todd, and Mike :)