0

I'm working on a school project that involves taking a pre-designed 25x25 maze from a .txt file and inputting it into a two dimensional array, then printing that onto the console and moving a character through the maze. The main issue I'm having is moving the characters into the array. I feel like I'm close but the code I have causes the program to crash.

Here's the loop I'm using to move the characters into the array:

int col=0;
ifstream inFile;
inFile.open("maze.txt");
char temp = inFile.get();
while(!inFile.eof())
{
    for(int row=0; row<25; row++)
    {
        while(temp != '\n')
        {
            boundary[row][col] = temp;
            col++;
            temp=inFile.get();
        }
        temp=inFile.get();
    }
}
inFile.close();

Essentially, the point is to move both spaces and block characters into a 25x25 character array by taking all the characters in each row into the same row of the array until it reaches a new line character, then it should move to the next character on the next row and start the while loop again.

It compiles fine, but the program crashes before it can move to any other code. If you know how to fix it without drastically changing the format I'd appreciate it because this is just a small part, but I'm open to starting from scratch if I need to.

  • 1
    [`while(!inFile.eof())`](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) – πάντα ῥεῖ Apr 19 '16 at 20:47
  • 1
    You could start by having your program make sure it succeeded in opening the file. – Scott Hunter Apr 19 '16 at 20:48
  • how is `boundary` declared/allocated? where does it crash and what's the stack trace? plus, check for EOF after each call to get(), not just in an outer loop, as that would mean "go load another maze" if there are any excess characters in the input file, leading to an overflow of the `col` index in your current loop configuration. – Cee McSharpface Apr 19 '16 at 20:50
  • @ScottHunter I added some code to verify it and it does open the .txt file before it crashes. – Tube Socks Apr 19 '16 at 20:54
  • Did you try to debug your program? – FieryCod Apr 19 '16 at 20:54

2 Answers2

3

Reset col to zero after each row (before or after the while(temp != '\n') loop).

Cee McSharpface
  • 7,540
  • 3
  • 29
  • 65
  • That was the problem. I feel like an idiot for missing that. Thanks a ton – Tube Socks Apr 19 '16 at 20:58
  • That was **a** problem. You should look into resolving some of the notes in the comments beneath your question. The `while eof` will catch you sooner or later. – user4581301 Apr 19 '16 at 21:00
  • @user4581301 I added an if statement to each to prevent that. – Tube Socks Apr 19 '16 at 21:36
  • You'll need more than one if statement. Each `temp=inFile.get();` will need to be tested before you try to use the character read in case you didn't get one. – user4581301 Apr 19 '16 at 21:47
0

your input might have more columns in the text file than the amount of column you defined in your array. the better way would be to define the size of the array in the text file and then allocate using malloc but if you know the exact size of column and row won't change then you can use a statically defined array size. Also you don't need a for loop.

#define max_col = 25;
#define max_row = 25;

char boundary[max_row][max_col];

int col=0;
int row=0;

ifstream inFile;
inFile.open("maze.txt");
char temp = inFile.get();

while(!inFile.eof())
{

    boundary[row][col] = temp;
    if(++col > max_col || temp == '\n') { 
       col = 0;
       if(++row > max_row) return -1; // depends how you want to handle 
    }

    temp=inFile.get();
}

inFile.close();
ArmenB
  • 1,770
  • 2
  • 18
  • 40