I am making a maze that consists of 48 nodes and the user needs to start in A1 and make there way to H6. The maze is created by reading a file, each line in the file looks something like A1 A2 B1 - - - where A1 is the current node A2 is the node to the north, B1 is the node to the east and the next place holder is to the south then the next is to the west and the last is a chute or ladder. Not to worried about the chute/ladders currently as it is not in the source yet. It is creating the nodes fine and it appears to link fine when debugged. The CurrentRoom pointer appears to follow the north pointer on the first pass but when
cout << myMaze.get_CurrentRoom()->getNodeName(); on line 348 is passed through again the program has been terminating with a segmentaion fault.
Thank you for anything you can help with even if you spot some error in my code. I am new to c++ so please go easy :)

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include <sstream>
#include <vector>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
   using namespace std;
   class Node
    {
    public:
      Node(){};
      Node(string newname)
      {
         name = newname;
         attachedNodes[0] = NULL;
         attachedNodes[1] = NULL;
         attachedNodes[2] = NULL;
         attachedNodes[3] = NULL;
         attachedNodes[4] = NULL;

      }
      void attachLadderChuteNode(Node *newNode);
      Node *getLadderChuteNode();
      string move_options();

      Node *getAttachedNode(int direction)
      {
         return attachedNodes[direction];
      }
      void setNodeName(string newname) 
      {
         name = newname;
      } 
      string getNodeName()
      {
         return name;
      }
      void attachNewNode(Node *newNode, int direction)
      {
         if (direction == 0) 
         {
            attachedNodes[0] = newNode;
         }
         if (direction == 1)
         {
            attachedNodes[1] = newNode;
         }
         if (direction == 2)
         {
            attachedNodes[2] = newNode;
         }
         if (direction == 3)
         {
            attachedNodes[3] = newNode;
         }
         if (direction == 4)
         {
            attachedNodes[4] = newNode;
         }
      }
      private:
      string name;
      Node *attachedNodes[4];
      Node *ladderOrChuteNodes;

    };





   class MazeMovement
    {
    private:
      int StepsTaken;
      bool MazeDone;
      string Name;
      vector<Node> Rooms;
      Node *CurrentRoom;

      public:
      MazeMovement() {StepsTaken = 0;}
      bool is_MazeDone();
      void Movement(char Direction);
      Node* get_CurrentRoom();
      Node get_CurrentNode();
      int get_StepsTaken();
      void Read_Maze(string file);
      void addRoom(Node roomIn);
      Node getAt(int i);
      Node find_Node(string Name);
      void updateNode(Node n);
      void set_CurrentRoom(Node *n);
      string getName();
    };
   void MazeMovement:: addRoom(Node roomIn) //Adds the nodes to a vector
   {
      Rooms.push_back(roomIn);
   }
   Node MazeMovement:: getAt(int i) //Gets the node at the specified index in the vector
   {
      return Rooms[i];
   }
   Node* MazeMovement:: get_CurrentRoom()
   {
      return CurrentRoom;
   }
   Node MazeMovement:: get_CurrentNode()
   {
      return *CurrentRoom;
   }
   void MazeMovement:: set_CurrentRoom(Node *n)
   {
      CurrentRoom = n;
   }

   int MazeMovement:: get_StepsTaken()
   {
      return StepsTaken;
   }
   bool MazeMovement:: is_MazeDone()
   {
      if(get_CurrentRoom()->getNodeName() == "H6")
      {
         MazeDone = true;
      }
      return MazeDone;
   }
   void MazeMovement:: Movement(char Direction)
   {
      string Moves = get_CurrentRoom()->move_options();
      switch(Direction)
      {
         case 'N':
         case 'n':
            size_t nfound;
            nfound = Moves.find("North");
            if(int(nfound) >= 0)
            {
               CurrentRoom = CurrentRoom ->getAttachedNode(0); //suppposed to set the current room to the node from the chosen pointer
               StepsTaken++;
            }
            else
            {
               cout << "Invalid selection. Please try again. \n";
            }
            break;
         case 'S':
         case 's':
            size_t sfound;
            sfound = Moves.find("South");
            if(int(sfound) >= 0)
            {
               CurrentRoom = CurrentRoom -> getAttachedNode(1);  
               StepsTaken++;
            }
            else
            {
               cout << "Invalid selection. Please try again. \n";
            }
            break;
         case 'E':
         case 'e':
            size_t efound;
            efound = Moves.find("East");
            if(int(efound) >= 0)
            {
               CurrentRoom = CurrentRoom -> getAttachedNode(2); 
               StepsTaken++;
            }
            else
            {
               cout << "Invalid selection. Please try again. \n";
            }
            break;
         case 'W':
         case 'w':
            size_t wfound;
            wfound = Moves.find("West");
            if(int(wfound) >= 0)
            {
               CurrentRoom = CurrentRoom -> getAttachedNode(3);
               StepsTaken++;
            }
            else
            {
               cout << "Invalid selection. Please try again. \n";
            }
            break;
         default:
            cout << "Invalid selection. Please try again. \n";
      }
   }
   void MazeMovement::Read_Maze(string FileName)
   {
      string line;
      ifstream inStream;
      inStream.open(FileName.c_str(), ios::in);
      int test = inStream.peek();
      int i = 0;
      if (!(inStream.fail()))
      {
         while(getline(inStream, line))
         {
            Node nodeIn;
            string north, south, east, west;
            int position1, position2;
            position1 = line.find(' ');
            nodeIn = find_Node(line.substr(0, position1));
            position2 = line.find(' ', position1 + 1);
            north = line.substr(position1 + 1, (position2 - position1) - 1); //Get the north node
            position1 = line.find(' ', position2);
            position2 = line.find(' ', position1 + 1);
            east = line.substr(position1 + 1, (position2 - position1) - 1);//Get the east node
            position1 = line.find(' ', position2);
            position2 = line.find(' ', position1 + 1);
            south = line.substr(position1 + 1, (position2 - position1) - 1);//Get the south node
            position1 = line.find(' ', position2);
            position2 = line.find(' ', position1 + 1);
            west = line.substr(position1 + 1, (position2 - position1) - 1);//Get the west node


            if(!(north[0] == '*'))//Checks to see if the node is valid, and if it is sets them
            {
               Node North = find_Node(north);
               nodeIn.attachNewNode(&North, 0);
            }
            if(!(east[0] == '*'))
            {
               Node East = find_Node(east);
               nodeIn.attachNewNode(&East, 1);
            }
            if(!(south[0] == '*'))
            {
               Node South = find_Node(south);
               nodeIn.attachNewNode(&South, 2);
            }
            if(!(west[0] == '*'))
            {
               Node West = find_Node(west);
               nodeIn.attachNewNode(&West, 3);
            }
            updateNode(nodeIn);
            CurrentRoom = &Rooms[0];

         }
      }
      else
      {
         cout << "Could not open the file name entered!" << endl;
         exit(1);
      }
   }

   string Node::move_options()
   {
      string options;
      if(!(attachedNodes[0] == NULL))
      {
         options += "North\n";
      }
      if(!(attachedNodes[2] == NULL))
      {
         options += "South\n";
      }
      if(!(attachedNodes[1] == NULL))
      {
         options += "East\n";
      }
      if(!(attachedNodes[3] == NULL))
      {
         options += "West\n";
      }
      if(options.empty())
      {
         options += "There is no where for you to move, sorry. Please choose another maze!";
      }
      return options;
   }
   Node MazeMovement::find_Node(string Name)
   {
      Node found;
      for(int i=0; i < Rooms.size(); i++)
      {

         string current = Rooms[i].getNodeName();
         if(strcmp(current.c_str(), Name.c_str()) == 0)
         {
            found = Rooms[i];
            return found;
         }
      }
      return found;
   }
   void MazeMovement:: updateNode(Node n)
   {
      for(int i=0; i < Rooms.size(); i++)
      {
         string updatedNode = n.getNodeName();
         string current = Rooms[i].getNodeName();
         if(strcmp(current.c_str(), updatedNode.c_str()) == 0)
         {
            Rooms[i] = n;
         }
      }
   }
   string MazeMovement:: getName() 
   {
      string name = get_CurrentRoom()->getNodeName();
      return name;
   }

   int main()
   {


      string FileName;
      MazeMovement myMaze;
      ostringstream in;

      for (int i = 1; i < 7; i++) {  //This creates all 48 nodes and puts them in a vector
         char nodeChar = 'A';
         for (int j = 0; j < 8; j++) {
            in << i;

            string nodeName = nodeChar + in.str();
            in.str("");
            Node node(nodeName);
            myMaze.addRoom(nodeName);
            nodeChar++;
         }
      }
      cout << "Please enter the name of the file: ";
      getline(cin,FileName);
      FileName += ".txt";
      myMaze.Read_Maze(FileName);
      cout << "========================================================================= " << endl;
      cout << " Welcome to the Ladder and Chute Maze! " << endl;
      cout << "========================================================================= " << endl;
      do
      {
         string SelectedDirection;
         char selection;
         cout << "You are currently in Room ";
         cout << myMaze.get_CurrentRoom()->getNodeName();
         cout << " of the Ladder and Chute Maze, you can go " +
            myMaze.get_CurrentRoom()->move_options() +".\n What is your choice?";
         getline(cin, SelectedDirection);
         selection = SelectedDirection[0];
         myMaze.Movement(selection);
      }
      while(!myMaze.is_MazeDone());
      {
         cout << "Congratulations! You have reached the finish point. \nYou took ";
         cout << myMaze.get_StepsTaken();
         cout << " steps." << endl;
         exit(1);
      }


      return 0;
   }

Recommended Answers

All 8 Replies

I really don't have the time at present to go through all your code.
One thing that immediately stands out is:

private:
    Node *attachedNodes[4];

Your code is using attachedNodes[0] .. attachedNodes[4] inclusive, which equals 5 Nodes not 4.

I can see a number of issues with the code as it stands, but the most serious one is that you are repeatedly walking off the end of the attachedNodes array; for an array of 4 items, the range of indices is 0, 1, 2, and 3, not 0, 1, 2, 3, 4. So, this line, and others like it:

attachedNodes[4] = NULL;

are incorrect, and will lead to a segmentation fault. I don't know if this is the source of the problem you'e now having, but it is the first place to look.

Also, I would recommend against mixing C-strings and the <cstring> functions with C++ string objects. With C++ strings, the equality operator is overloaded, so you can simply compare them, without having to resort to strcmp().

BTW, what compiler and editor are you using? I have an important piece of advice, but how you would apply it depends on how you are compiling the program. The advice is to break the program up into separate files, with a header file and a code file for each of the classes and a code file for the main() function. Thus, your code would look like so (with a few corrections added):
node.h

#ifndef NODE_H
#define NODE_H 1

#include <string>

class Node
{
private:
    std::string name;
    Node *attachedNodes[4];
    Node *ladderOrChuteNodes;

public:
    // c'tors
    Node(std::string newname = "aNode");
    Node(const Node& other);

    Node& operator=(const Node& other);

    void attachLadderChuteNode(Node *newNode);
    Node *getLadderChuteNode();
    std::string move_options();

    Node *getAttachedNode(int direction)
    {
        return attachedNodes[direction];
    };

    void setNodeName(std::string newname)
    {
        name = newname;
    };

    std::string getNodeName()
    {
        return name;
    };

    void attachNewNode(Node *newNode, int direction);
};

#endif

node.cpp

#include <string>

#include "node.h"

Node::Node(std::string newname): name(newname), ladderOrChuteNodes(0)
{
    for (int i = 0; i < 4; i++)
    {
        attachedNodes[i] = 0;
    }
}

Node::Node(const Node& other): name(other.name), ladderOrChuteNodes(other.ladderOrChuteNodes)
{
    for (int i = 0; i < 4; i++)
    {
        attachedNodes[i] = other.attachedNodes[i];
    }
}


Node& Node::operator=(const Node& other)
{
    name = other.name;
    ladderOrChuteNodes = other.ladderOrChuteNodes;

    for (int i = 0; i < 4; i++)
    {
        attachedNodes[i] = other.attachedNodes[i];
    }

    return *this;
}


void Node::attachLadderChuteNode(Node *newNode)
{
    return;   // dummy function to prevent a linker error
}

void Node::attachNewNode(Node *newNode, int direction)
{
    if (direction >= 0 and direction < 4)
    {
        attachedNodes[direction] = newNode;
    }
}

std::string Node::move_options()
{
    const std::string DIRECTIONS[] = {"North", "South", "East", "West"};

    std::string options = "";

    for (int i = 0; i < 4; i++)
    {
        if (attachedNodes[i] != 0)
        {
            options += DIRECTIONS[i] + '\n';
        }
    }

    if(options.empty())
    {
        options = "There is no where for you to move, sorry. Please choose another maze!";
    }
    return options;
}

Maze.h

#ifndef MAZE_H
#define MAZE_H

#include <string>
#include <vector>
#include "node.h"

class MazeMovement
{
private:
    int StepsTaken;
    bool MazeDone;
    std::string Name;
    std::vector<Node> Rooms;
    Node *CurrentRoom;

public:
    // constructors
    MazeMovement(): StepsTaken(0), MazeDone(false), Name(""), Rooms(), CurrentRoom(0)
    {
        return;
    };

    MazeMovement(const MazeMovement& other);

    ~MazeMovement()
    {
        return;
    }

    MazeMovement& operator=(const MazeMovement& other);

    void addRoom(Node roomIn) //Adds the nodes to a vector
    {
        Rooms.push_back(roomIn);
    };
    Node getAt(int i)
    {
        return Rooms[i];
    };
    Node* get_CurrentRoom()
    {
        return CurrentRoom;
    };
    Node get_CurrentNode()
    {
        return *CurrentRoom;
    };

    int get_StepsTaken()
    {
        return StepsTaken;
    }

    void set_CurrentRoom(Node *n)
    {
        CurrentRoom = n;
    };

    Node find_Node(std::string nodeName);
    void updateNode(Node n);
    bool is_MazeDone();
    void Movement(char Direction);
    void Read_Maze(std::string file);
    std::string getName();
};

#endif

maze.cpp

#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <cstdlib>

#include "node.h"
#include "maze.h"


MazeMovement::MazeMovement(const MazeMovement& other): StepsTaken(other.StepsTaken), MazeDone(other.MazeDone), Name(other.Name), Rooms(other.Rooms), CurrentRoom(other.CurrentRoom)
{
    return;
}


MazeMovement& MazeMovement::operator=(const MazeMovement& other)
{
    StepsTaken = other.StepsTaken;
    MazeDone = other.MazeDone;
    Name = other.Name;
    Rooms = other.Rooms;
    CurrentRoom = other.CurrentRoom;
    return *this;
}

bool MazeMovement:: is_MazeDone()
{
    if(get_CurrentRoom()->getNodeName() == "H6")
    {
        MazeDone = true;
    }
    return MazeDone;
}

void MazeMovement:: Movement(char Direction)
{
    std::string Moves = get_CurrentRoom()->move_options();
    switch(Direction)
    {
    case 'N':
    case 'n':
        std::size_t nfound;
        nfound = Moves.find("North");
        if(int(nfound) >= 0)
        {
            CurrentRoom = CurrentRoom ->getAttachedNode(0); //suppposed to set the current room to the node from the chosen pointer
            StepsTaken++;
        }
        else
        {
            std::cout << "Invalid selection. Please try again. \n";
        }
        break;
    case 'S':
    case 's':
        std::size_t sfound;
        sfound = Moves.find("South");
        if(int(sfound) >= 0)
        {
            CurrentRoom = CurrentRoom -> getAttachedNode(1);
            StepsTaken++;
        }
        else
        {
            std::cout << "Invalid selection. Please try again. \n";
        }
        break;
    case 'E':
    case 'e':
        std::size_t efound;
        efound = Moves.find("East");
        if(int(efound) >= 0)
        {
            CurrentRoom = CurrentRoom -> getAttachedNode(2);
            StepsTaken++;
        }
        else
        {
            std::cout << "Invalid selection. Please try again. \n";
        }
        break;
    case 'W':
    case 'w':
        std::size_t wfound;
        wfound = Moves.find("West");
        if(int(wfound) >= 0)
        {
            CurrentRoom = CurrentRoom -> getAttachedNode(3);
            StepsTaken++;
        }
        else
        {
            std::cout << "Invalid selection. Please try again. \n";
        }
        break;
    default:
        std::cout << "Invalid selection. Please try again. \n";
    }
}

void MazeMovement::Read_Maze(std::string FileName)
{
    std::string line;
    std::ifstream inStream;

    inStream.open(FileName.c_str(), std::ios::in);
    // int test = inStream.peek();
    // int i = 0;
    if (!(inStream.fail()))
    {
        while(getline(inStream, line))
        {
            Node nodeIn;
            std::string north, south, east, west;
            int position1, position2;
            position1 = line.find(' ');
            nodeIn = find_Node(line.substr(0, position1));
            position2 = line.find(' ', position1 + 1);
            north = line.substr(position1 + 1, (position2 - position1) - 1); //Get the north node
            position1 = line.find(' ', position2);
            position2 = line.find(' ', position1 + 1);
            east = line.substr(position1 + 1, (position2 - position1) - 1);//Get the east node
            position1 = line.find(' ', position2);
            position2 = line.find(' ', position1 + 1);
            south = line.substr(position1 + 1, (position2 - position1) - 1);//Get the south node
            position1 = line.find(' ', position2);
            position2 = line.find(' ', position1 + 1);
            west = line.substr(position1 + 1, (position2 - position1) - 1);//Get the west node


            if(!(north[0] == '*'))          //Checks to see if the node is valid, and if it is sets them
            {
                Node North = find_Node(north);
                nodeIn.attachNewNode(&North, 0);
            }
            if(!(east[0] == '*'))
            {
                Node East = find_Node(east);
                nodeIn.attachNewNode(&East, 1);
            }
            if(!(south[0] == '*'))
            {
                Node South = find_Node(south);
                nodeIn.attachNewNode(&South, 2);
            }
            if(!(west[0] == '*'))
            {
                Node West = find_Node(west);
                nodeIn.attachNewNode(&West, 3);
            }
            updateNode(nodeIn);
            CurrentRoom = &Rooms[0];

        }
    }
    else
    {
        std::cerr << "Could not open the file name entered!" << std::endl;
        exit(1);
    }
}


Node MazeMovement::find_Node(std::string nodeName)
{
    Node found;
    for(std::size_t i=0; i < Rooms.size(); i++)
    {

        std::string current = Rooms[i].getNodeName();
        if(current == nodeName)
        {
            found = Rooms[i];
            return found;
        }
    }
    return found;
}

void MazeMovement:: updateNode(Node n)
{
    for(std::size_t i=0; i < Rooms.size(); i++)
    {
        std::string updatedNode = n.getNodeName();
        std::string current = Rooms[i].getNodeName();
        if(current == updatedNode)
        {
            Rooms[i] = n;
        }
    }
}

std::string MazeMovement:: getName()
{
    std::string name = get_CurrentRoom()->getNodeName();
    return name;
}

main.cpp

#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstdlib>


#include "node.h"
#include "maze.h"

using namespace std;


int main()
{
    string FileName;
    MazeMovement myMaze;
    ostringstream in;

    for (int i = 1; i < 7; i++)    //This creates all 48 nodes and puts them in a vector
    {
        char nodeChar = 'A';
        for (int j = 0; j < 8; j++)
        {
            in << i;

            string nodeName = nodeChar + in.str();
            in.str("");
            Node node(nodeName);
            myMaze.addRoom(nodeName);
            nodeChar++;
        }
    }
    cout << "Please enter the name of the file: ";
    getline(cin,FileName);
    FileName += ".txt";
    myMaze.Read_Maze(FileName);
    cout << "========================================================================= " << endl;
    cout << " Welcome to the Ladder and Chute Maze! " << endl;
    cout << "========================================================================= " << endl;
    do
    {
        string SelectedDirection;
        char selection;
        cout << "You are currently in Room ";
        cout << myMaze.get_CurrentRoom()->getNodeName();
        cout << " of the Ladder and Chute Maze, you can go " +
             myMaze.get_CurrentRoom()->move_options() +".\n What is your choice?";
        getline(cin, SelectedDirection);
        selection = SelectedDirection[0];
        myMaze.Movement(selection);
    }
    while(!myMaze.is_MazeDone());
    {
        cout << "Congratulations! You have reached the finish point. \nYou took ";
        cout << myMaze.get_StepsTaken();
        cout << " steps." << endl;
        exit(1);
    }


    return 0;
}

The reason why I'd need to know your compiler and how you build the project is because different compilers and IDEs handle them differently.

I am using JGrasp with g++ compiler and Thank You so much for the insight. This should help me a ton!

Hi again I tried your code and there is still a segment fault but I believe it is happens when the user is prompted for a direction and makes a move, I don't think the pointer is going to the correct node because while debugging I try to print CurrentRoom -> getNodeName() after choosing a direction and it is giving the me the value "\034\252\"" instead of "A2" any ideas?

Would you mind posting or attaching a copy of the data file? It is hard to test the program without it.

yeah no problem. The file I have only links the first Row at the moment but that shouldnt be an issue cause I cant get past getting to the second node

OK, I'm looking at the code, but I can't find any point in it where you allocate any Node objects. I suspect that Rooms needs to be vector<Node*>, not vector<Node>.

I've taken the liberty of updating the program so that it uses Node pointers, and added a method that prints the whole maze (useful for debugging). I've also added an enumeration for the cardinal directions, and a matching array of direction names. The current versions of the files are as follows:

node.h

#ifndef NODE_H
#define NODE_H 1

#include <string>

enum CARDINAL {NORTH, EAST, SOUTH, WEST};
const std::string DIRECTIONS[] = {"North", "East", "South", "West"};

class Node
{
private:
    std::string name;
    Node *attachedNodes[4];
    Node *ladderOrChuteNodes;

public:
    // c'tors
    Node(std::string newname = "aNode");
    Node(const Node& other);

    ~Node()
    {
        return;
    }


    Node& operator=(const Node& other);

    void attachLadderChuteNode(Node *newNode);
    Node *getLadderChuteNode();
    std::string move_options();

    Node *getAttachedNode(CARDINAL direction)
    {
        return attachedNodes[direction];
    };

    void setNodeName(std::string newname)
    {
        name = newname;
    };

    std::string getNodeName()
    {
        return name;
    };

    void attachNewNode(Node *newNode, CARDINAL direction);
};

#endif

node.cpp

#include <string>

#include "node.h"

Node::Node(std::string newname): name(newname), ladderOrChuteNodes(0)
{
    for (int i = 0; i < 4; i++)
    {
        attachedNodes[i] = 0;
    }
}

Node::Node(const Node& other): name(other.name), ladderOrChuteNodes(other.ladderOrChuteNodes)
{
    for (int i = 0; i < 4; i++)
    {
        attachedNodes[i] = other.attachedNodes[i];
    }
}


Node& Node::operator=(const Node& other)
{
    name = other.name;
    ladderOrChuteNodes = other.ladderOrChuteNodes;

    for (int i = 0; i < 4; i++)
    {
        attachedNodes[i] = other.attachedNodes[i];
    }

    return *this;
}


void Node::attachLadderChuteNode(Node *newNode)
{
    return;   // dummy function to prevent a linker error
}

void Node::attachNewNode(Node *newNode, CARDINAL direction)
{
    if (direction >= 0 and direction < 4)
    {
        attachedNodes[direction] = newNode;
    }
}

std::string Node::move_options()
{


    std::string options = "";

    for (int i = 0; i < 4; i++)
    {
        if (attachedNodes[i] != 0)
        {
            options += DIRECTIONS[i] + '\n';
        }
    }

    if(options.empty())
    {
        options = "There is no where for you to move, sorry. Please choose another maze!";
    }
    return options;
}

maze.h

#ifndef MAZE_H
#define MAZE_H

#include <string>
#include <vector>
#include "node.h"

class MazeMovement
{
private:
    bool MazeDone;
    std::string Name;
    std::vector<Node*> Rooms, visitedRooms;
    Node *CurrentRoom;

public:
    // constructors
    MazeMovement(): MazeDone(false), Name(""), Rooms(), visitedRooms(), CurrentRoom(0)
    {
        return;
    };

    MazeMovement(const MazeMovement& other);

    ~MazeMovement()
    {
        return;
    }

    MazeMovement& operator=(const MazeMovement& other);

    void addRoom(Node* roomIn) //Adds the nodes to a vector
    {
        Rooms.push_back(roomIn);
    };
    Node* getAt(int i)
    {
        return Rooms[i];
    };
    Node* get_CurrentRoom()
    {
        return CurrentRoom;
    };
    Node get_CurrentNode()
    {
        return *CurrentRoom;
    };

    int get_StepsTaken()
    {
        return visitedRooms.size();
    }

    void set_CurrentRoom(Node *n)
    {
        CurrentRoom = n;
    };

    Node* find_Node(std::string nodeName);
    void updateNode(Node* n);
    bool is_MazeDone();
    void Movement(char Direction);
    void Read_Maze(std::string file);
    std::string getName();
    void printAll();
    void printVisited();
};

#endif

maze.cpp

#include <string>
#include <vector>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstdlib>

#include "node.h"
#include "maze.h"


MazeMovement::MazeMovement(const MazeMovement& other): MazeDone(other.MazeDone), Name(other.Name), Rooms(other.Rooms), visitedRooms(), CurrentRoom(other.CurrentRoom)
{
    return;
}


MazeMovement& MazeMovement::operator=(const MazeMovement& other)
{
    MazeDone = other.MazeDone;
    Name = other.Name;
    Rooms = other.Rooms;
    visitedRooms = other.visitedRooms;
    CurrentRoom = other.CurrentRoom;
    return *this;
}

bool MazeMovement:: is_MazeDone()
{
    if(get_CurrentRoom()->getNodeName() == "H6")
    {
        MazeDone = true;
    }
    return MazeDone;
}

void MazeMovement:: Movement(char Direction)
{
    std::string Moves = get_CurrentRoom()->move_options();
    switch(std::toupper(Direction))
    {
    case 'N':
        std::size_t nfound;
        nfound = Moves.find("North");
        if(int(nfound) >= 0)
        {
            CurrentRoom = CurrentRoom ->getAttachedNode(NORTH); //suppposed to set the current room to the node from the chosen pointer
            visitedRooms.push_back(CurrentRoom);
        }
        else
        {
            std::cout << "Invalid selection. Please try again. \n";
        }
        break;
    case 'S':
        std::size_t sfound;
        sfound = Moves.find("South");
        if(int(sfound) >= 0)
        {
            CurrentRoom = CurrentRoom -> getAttachedNode(SOUTH);
            visitedRooms.push_back(CurrentRoom);
        }
        else
        {
            std::cout << "Invalid selection. Please try again. \n";
        }
        break;
    case 'E':
        std::size_t efound;
        efound = Moves.find("East");
        if(int(efound) >= 0)
        {
            CurrentRoom = CurrentRoom -> getAttachedNode(EAST);
            visitedRooms.push_back(CurrentRoom);
        }
        else
        {
            std::cout << "Invalid selection. Please try again. \n";
        }
        break;
    case 'W':
        std::size_t wfound;
        wfound = Moves.find("West");
        if(int(wfound) >= 0)
        {
            CurrentRoom = CurrentRoom -> getAttachedNode(WEST);
            visitedRooms.push_back(CurrentRoom);
        }
        else
        {
            std::cout << "Invalid selection. Please try again. \n";
        }
        break;
    default:
        std::cout << "Invalid selection. Please try again. \n";
    }
}

void MazeMovement::Read_Maze(std::string FileName)
{
    std::string line;
    std::ifstream inStream;

    inStream.open(FileName.c_str(), std::ios::in);
    // int test = inStream.peek();
    // int i = 0;
    if (!(inStream.fail()))
    {
        while(getline(inStream, line))
        {
            Node* nodeIn;
            std::string north, south, east, west;
            int position1, position2;
            position1 = line.find(' ');
            nodeIn = find_Node(line.substr(0, position1));
            position2 = line.find(' ', position1 + 1);
            north = line.substr(position1 + 1, (position2 - position1) - 1); //Get the north node
            position1 = line.find(' ', position2);
            position2 = line.find(' ', position1 + 1);
            east = line.substr(position1 + 1, (position2 - position1) - 1);//Get the east node
            position1 = line.find(' ', position2);
            position2 = line.find(' ', position1 + 1);
            south = line.substr(position1 + 1, (position2 - position1) - 1);//Get the south node
            position1 = line.find(' ', position2);
            position2 = line.find(' ', position1 + 1);
            west = line.substr(position1 + 1, (position2 - position1) - 1);//Get the west node


            if(north != "*")          //Checks to see if the node is valid, and if it is sets them
            {
                Node* North = find_Node(north);
                nodeIn->attachNewNode(North, NORTH);
            }
            if(east != "*")
            {
                Node* East = find_Node(east);
                nodeIn->attachNewNode(East, EAST);
            }
            if(south != "*")
            {
                Node* South = find_Node(south);
                nodeIn->attachNewNode(South, SOUTH);
            }
            if(west != "*")
            {
                Node* West = find_Node(west);
                nodeIn->attachNewNode(West, WEST);
            }
            updateNode(nodeIn);
            CurrentRoom = Rooms[0];

        }
        inStream.close();
    }
    else
    {
        std::cerr << "Could not open the file name entered!" << std::endl;
        std::exit(1);
    }
}


Node* MazeMovement::find_Node(std::string nodeName)
{
    Node* found = new Node();
    for(std::size_t i=0; i < Rooms.size(); i++)
    {

        std::string current = Rooms[i]->getNodeName();
        if(current == nodeName)
        {
            found = Rooms[i];
            return found;
        }
    }
    return found;
}

void MazeMovement:: updateNode(Node* n)
{
    for(std::size_t i=0; i < Rooms.size(); i++)
    {
        std::string updatedNode = n->getNodeName();
        std::string current = Rooms[i]->getNodeName();
        if(current == updatedNode)
        {
            Rooms[i] = n;
        }
    }
}

std::string MazeMovement:: getName()
{
    std::string name = get_CurrentRoom()->getNodeName();
    return name;
}

void MazeMovement::printAll()
{
    if (!Rooms.empty())
    {
        Node* southRoom = Rooms[0];


        do
        {
            Node* eastRoom = southRoom;
            do
            {
                std::cout << std::setw(4) << eastRoom ->getNodeName() << " ";
                eastRoom = eastRoom ->getAttachedNode(EAST);
            }
            while (eastRoom != 0);

            std::cout << std::endl;

            southRoom = southRoom ->getAttachedNode(SOUTH);
        } while (southRoom != 0);

    }
}

This version does not crash, but highlights some other problems with the program which you'll want to address.

The nodes are created in the main. There are 2 for loops to create the 48 nodes. And Thank you so much for your help. I will have to take a look when I get home. I will keep this updated with any questions arise.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.