1

I'm trying to read a "pointer-based" maze. The first letter in each row corresponds to the room letter...then the letters that follow are North node, East node, South node, and West node respectively. The asterisk indicates an empty room or not a valid option.

So this would be a sample input:

A E B * *
B * * * A
C G D * *
D * * * C
E I F A *
F J G * E
G K H C F
H L * * G
I * J E *
J * K F I
K * * G J
L * * H *

Notes say:

We can't assume the rooms will be in order alphabetically A - Z, We are expecting a maximum of 12 rooms and there is a space between each letter or asterisk.

Here is what I have so far:

void Maze::read_maze(string FileName) {
    string line;
    ifstream inStream;
    inStream.open(FileName.c_str());
    int test = inStream.peek();
    int i = 0;
    if (!(inStream.fail())) {
        while (!inStream.eof() && test != EOF) {
            getline(inStream, line);
            Node n(line[0]);
            i++;
            rooms[i] = n;
            if (!(line[2] == '*')) {
                Node North = find_Node(line[2]);
                (find_Node(line[0])).set_North(&North);
            }
            if (!(line[4] == '*')) {
                Node East = find_Node(line[4]);
                (find_Node(line[0])).set_East(&East);
            }
            if (!(line[6] == '*')) {
                Node South = find_Node(line[6]);
                (find_Node(line[0])).set_South(&South);
            }
            if (!(line[8] == '*')) {
                Node West = find_Node(line[8]);
                (find_Node(line[0])).set_West(&West);
            }
        }
        currentRoom = find_Node('A');
    } else {
        cout << "Could not open the file, please choose another.\n" << endl;
        exit(1);
    }
}

When I begin parsing the file, my program calls the function I have that checks if the file is empty. So I think I'm reading the file in incorrectly.

tyler_7
  • 63
  • 1
  • 9
  • What help do you need? You seem to have some code already - does it work? – Jonathan Potter Nov 29 '14 at 08:08
  • @JonathanPotter Thank you for your reply, when I begin parsing the file my program calls the function I have that checks if the file is empty. So I think I'm reading the file in, incorrectly. – tyler_7 Nov 29 '14 at 08:12
  • A few notes. You are using more parentheses than necessary. `(find_Node(line[0])).set_North(&North);` can just be `find_Node(line[0]).set_North(&North);`, and `!(line[2] == '*')` can be `line[2] != '*'`. You should not be using [EOF](http://stackoverflow.com/questions/1782080/) in a C++ program. Perhaps most importantly is that when asking a question, you should pare it down to a *minimal misunderstanding*; if you're getting code saying a file is empty, does the maze problem definition really matter for your question? What if it was *"print 0 if file contains * and 1 if it's a letter"*? – HostileFork says dont trust SE Nov 29 '14 at 08:36
  • Readability makes a huge difference when reviewing code. For instance, you could have an 'invalid Node object', e.g. "static Node invalid;", which find_Node() could return (what *does* it return when it can't find a Node specified?). Then you don't need the check for == '*', as you can simply write rooms[i].set_West(find_Node(line[8]); ... if line[8] was a *, you'll set the invalid Node. – Mark Toller Nov 29 '14 at 08:45
  • All of your set calls (e.g. set_West ()) are taking either a reference or a pointer to a Node object on the stack, which will go out of scope after exiting the if () block, leaving a dangling pointer or reference. – Mark Toller Nov 29 '14 at 15:15
  • @MarkToller I appreciate your reply, I've taken the suggestions here and I'm still having trouble parsing the text within the file. I've tested with cout and it's not entering any of the if statements for Node North - West. Might there be issues with my logic on parsing the file? – tyler_7 Nov 30 '14 at 00:57

1 Answers1

1

Can you not use a debugger to check the values as the program runs? If not, then try a

cost << line <<  endl; 

After the getline, to verify the actual data read in for each line.

Mark Toller
  • 106
  • 4
  • after adding cout << line << endl; it prints out the exact content of the text file. There has to be something going wrong with my get and set methods. – tyler_7 Nov 30 '14 at 16:54
  • One more point, the line rooms[i] = n; should probably be rooms [i] = find_Node (line [0]); as the room may already have been added as a location from another room.. – Mark Toller Dec 02 '14 at 08:11
  • One more point, the line rooms[i] = n; should probably be rooms [i] = find_Node (line [0]); as the room may already have been added as a location from another room... In this case, find_Node should create a new room and return it if it didn't exist.... – Mark Toller Dec 02 '14 at 08:11