Interesting issue with 2d array?

I was bored and made a tic-tac-toe game, and used a 2d char array to make the map. I originally had it char MapArray[4][4] but after defining the elements to a blank template and then using a for loop to print it, the output is all weird. Nothing was wrong with my function to set up the template, and I created a debug function to print single elements and I confirmed that elements in [x][4] weren't being defined correctly. As a dirty fix I just enlarged the array to [5][5] but I'm curious on what was going on here.

1
2
3
4
5
6
7
8
9
void Map::setBlankMap(){
    int column;
    for(column = 0; column <= 5; column++){
        int row = 0;
        for(row = 0; row <= 5; row++){
            Map::MapArray[column][row] = ' ';
        }
    }
}


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
void Map::setMap()
{
    int forSetRow;
    Map::MapArray[0][0] = ' ';
    Map::MapArray[0][1] = '|';
    Map::MapArray[0][2] = ' ';
    Map::MapArray[0][3] = '|';
    Map::MapArray[0][4] = ' ';
    for(forSetRow = 0; forSetRow <= 4; forSetRow++)
    {
        Map::MapArray[1][forSetRow] = '_';
    }

    Map::MapArray[2][0] = ' ';
    Map::MapArray[2][1] = '|';
    Map::MapArray[2][2] = ' ';
    Map::MapArray[2][3] = '|';
    Map::MapArray[2][4] = ' ';

    for(forSetRow = 0; forSetRow <= 4; forSetRow++)
    {
        Map::MapArray[3][forSetRow] = '_';
    }

    Map::MapArray[4][0] = ' ';
    Map::MapArray[4][1] = '|';
    Map::MapArray[4][2] = ' ';
    Map::MapArray[4][3] = '|';
    Map::MapArray[4][4] = ' ';

}


1
2
3
4
5
6
7
8
9
10
11
12
13
void Map::printMap()
{
    int Collumn;
    for (Collumn = 0; Collumn <= 4; Collumn++){
        int printRow = 0;
        for(printRow = 0; printRow <= 4; printRow++){
            std::cout << Map::MapArray[Collumn][printRow];
            if(printRow == 4){
                std::cout << " " << std::endl;
            }
        }
    }
}


1
2
3
4
Map::Map(){
    Map::setBlankMap();
    Map::setMap();
}
Topic archived. No new replies allowed.