1

Okay, I'm having a kind of weird issue here. I'm working on a program where a "maze," represented by various characters, is read from a file and solved by the computer.

Right now I'm attempting to have the maze be read into a 2d array. I'm also having the maze be printed twice at the moment, once as it is being read into memory, and again afterwards to ensure everything got saved correctly. The first printing goes correctly, but the second printing shows me that the last row of the array has somehow overridden all the other rows. Any attempt to access a specific value of the array will confirm this. To give you an idea of what Im talking about, here's what the maze is supposed to look like, and what the first printing shows:

S#.#...##.G
..##..#..#.
.#..#.#....
...##..#...
..#.....###
#...#...#..
..#..#.#...
.####.....#
...##.##...
.#....####.
###........

Meanwhile, here's what the second printing shows, and what any attempt to access the array will tell you:

###........
###........
###........
###........
###........
###........
###........
###........
###........
###........
###........

Obviously this is quite frustrating to me. Anyways, here's the code for reading and printing the array:

ifstream input(fileLocation);
//get the maze dimensions
input >> dimX;
input >> dimY;
maze = new char[(dimX - 1), (dimY - 1)]; //Allocate memory for the maze
//read the maze into memory and prints it for reference
cout << "Initial Maze:" << endl;
for (int i = 0; i < dimY; i++) {
    for (int j = 0; j < dimX; j++) {
        input >> maze[i, j];
        cout << maze[i, j];
        //recognizing the start space
        if (maze[i,j] == 'S') {
            currPos = &maze[i,j];
        }
        else if (maze[i, j] == 'G') { //recognizing the end space
            goalX = j;
            goalY = i;
        }
    }
    cout << endl;
}
input.close();


//second print for test

for (int i = 0; i < dimX; i++) {
    for (int j = 0; j < dimY; j++) {
        cout << maze[i, j];
    }
    cout << endl;
}

Any help at all would be appreciated. Please don't hesitate to let me know if you need more information.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
  • The syntax for 2D arrays is `array[i][j]`. See the linked question for a detailed explanation. – John Kugelman Mar 08 '20 at 03:55
  • Do you come from C#? In fact, there are no 2D arrays in C++, C++ only has array of array. We just call them 2D arrays for convenience. The comma expression has another meaning. – con ko Mar 08 '20 at 04:08
  • @SamVarshavchik Thank you for the advice. While we're at it, you should look at, and read, some of the other comments here, so that you can learn the correct approach to saying this sort of thing without getting flagged for rudeness :) – Ian Finnigan Mar 08 '20 at 04:37
  • @JohnDing I work with a variety of other languages, all of which I prefer over C++. I'm only using C++ now because I'm being forced to. Thank you for the explanation – Ian Finnigan Mar 08 '20 at 04:40

0 Answers0