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:
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.
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
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.
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
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?