Problem with reading from an array

This is a discussion on Problem with reading from an array within the C++ Programming forums, part of the General Programming Boards category; As the title suggests im having trouble with writing from an array. It seems as if even the i give ...

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    12

    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!

    Thanks in advance.
    Last edited by Afro; 04-09-2008 at 11:42 AM.

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    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

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    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
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Duh. Boards are 8X8. Good point Mike!
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    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?
    Last edited by Afro; 04-09-2008 at 11:53 AM.

  6. #6
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    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.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  7. #7
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    Ah, alright, makes sense now, thanks for the help Todd, and Mike

    /thread over

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with copying a string into array in a struct
    By JFonseka in forum C Programming
    Replies: 15
    Last Post: 05-04-2008, 06:07 AM
  2. Problem with file and array
    By paok in forum C Programming
    Replies: 5
    Last Post: 05-01-2008, 05:19 AM
  3. problem copy from array to another
    By s-men in forum C Programming
    Replies: 3
    Last Post: 09-07-2007, 02:51 PM
  4. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 05:15 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21