Printing 2D dynamic array from file

Hi everyone, I'm working on an assignment that is required to use a 2D array to store data from a text file for a maze and then it should use a queue to solve the maze. I think i have the right idea with storing the maze in the array, but when I go to print it, it seems that it prints an empty array. It's probably a simple fix but I'm not seeing it. Any guidance on how to fix this issue would be much appreciated. Thanks in advanced. Here is my code for the 2D dynamic array:

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
32
  char **maze;
  int nRows = 0;
  int nCols = 0;
  ifstream ifile;


  ifile.open("maze.txt");
  cout << "Reading # of rows and columns..." << endl;
  ifile >> nRows >> nCols;
  cout << "nRows: "<< nRows <<"\nnCols: " << nCols << endl;

  maze = new char* [nRows]; // create rows of maze (height)
  for(int height = 0; height < nRows; height++)
    {
      maze[height] = new char[nCols]; // create cols of maze (length)
      for(int length = 0; length < nCols; length++)
        {
          while(!ifile.eof())
            {
              for(int i = 0; i < height; i++)
                {
              ifile >> maze[height][length];

                  cout << maze[height][length];
                  i++;
                }
            }
        }
      cout << endl;
    }



Last edited on
You shouldn't have the while loop nested in there like that.

You can just read in a single item in the inner-most nested loop, so that it doesn't consume all the input on the first element.

1
2
3
4
5
6
7
8
9
10
maze = new char*[nRows];
for(int height = 0; height < nRows; height++)
{

  maze[height] = new char[nCols];
  for(int length = 0; length < nCols; length++)
  {
    ifile >> maze[height][length];
  }
}


I recommend not looping on .eof, because it won't catch all errors, like an invalid character.
You can loop over it like:
while(ifile) { ... (see http://www.cplusplus.com/reference/ios/ios/operator_bool/)

If you want to make sure you don't try to read from the file if you've reached the end (or there are some invalid characters, or other error condition), you can add a check in the for-loop.
1
2
3
4
for(int height = 0; height < nRows && ifile; height++)
{
  for(int length = 0; length < nCols && ifile; length++)
  ...
That makes sense, and that is how I had it originally (without the "for(int i = 0; i<length..." but then how can I print it so that it is in the form of a 2d array? When I tried

1
2
3
4
5
6
7
8
9
 //1
//2
//3
//4
//5
//6
//7
ifile >> maze[height][length];
cout << maze[height][length];


it prints the output all on one line instead of in a square. When I add an "endl" at the end of the "cout" it prints every individual character on its own line. I gotta be just overlooking something obvious here.
Last edited on
Well you have to be a little clever about formatting the output. It's storing them correctly, you just have to format the output.

Try printing some spaces after each character.

1
2
3
4
5
6
7
8
for(int y = 0; y < nRows; y++)
{
  for(int x = 0; x < nCols; x++)
  {
    cout << maze[y][x] << "    ";
  }
  cout << "\n";
}


You can include <iomanip> if you want to use things like setw, setprecision, etc, to manipulate the output more.
Are you sure it's storing them correctly? When I write cout << maze[0][0]; it still prints out the entire array on a single line and then 4 more blank lines after that, as the dimensions read from the data file are 5 by 5. To me it looks as if the entire text file is being stored in the first element of the array and I can't figure out why it's behaving this way. So far I've tried adding spaces after the print statement, using the setw() function, changing the placement of my print statement and it keeps giving me incorrect output. When I do cout << maze[0][0] I get the following output:
1
2
3
4
5
6
7
8
Reading # of rows and columns...
nRows: 5
nCols: 5
S...#.#.##.#...##.#.....GG



Last edited on
Nevermind about the 4 extra lines, I forgot about that extra cout << endl; within the outer for loop. Also, I figured out that having while(ifile) within both for loops right before the read and print statements is what was causing the weird output all one line. Could you explain to me why that is?
Last edited on
Topic archived. No new replies allowed.