0

I'm trying to read a text file into a 2D array. The rows and columns are listed in the first 2 lines of the text file so I just:

inFile >> rows;
inFile >> cols;

and that works fine.

However, whenever I initialize the array and print it...it doesn't come out properly. Why? There should be spaces in the maze but everything is filled with # like so:

# # # # # # # # # 
# # # # # # # # # 
# # # # # s # # # 
e # # # # # # # # 
# # # # # # # # # 
# # # # # # # # # 
# # # # 

    void setArray(int rows, int cols)
    {

        for (int i = 0; i < rows; i++)
        {
            grid[i] = new string[cols];
        }

        for(int i = 0; i < rows; i++)
        {
            for(int j = 0; j < cols; j++)
            {
                inFile >> grid[i][j];
            }
        }

        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                cout << grid[i][j] << " ";
            }
            cout << endl;
        }
    }
Josh Garza
  • 63
  • 1
  • 7

0 Answers0