title: Program exits unexpectedly
body: <p>I am trying to make a program that takes all the data from a file and then the user can decide to add more courses to the file, delete a specific course, or search for a specific course in the file. I am sure that there are more problems then what I am asking for.</p>
  <p>In the createNode function the user is supposed to input all the information about the course, but after the user enters the name of the course the program closes. I am relatively new to coding, and I have tried looking up solutions to this problem but could not find anything.</p>
  <pre><code>#include&lt;iostream&gt;
  #include&lt;string&gt;
  #include&lt;fstream&gt;
  #include&lt;cstdlib&gt;
  using namespace std;
  
  enum week {Mon,Tue,Wed,Thu,Fri};
  
  struct courseInfo
  {
      string courseName, CourseID, times[5], building, roomNum;
      week day[5];
  }course;
  
  struct NodeType
  {
      courseInfo *data;
      NodeType *next;
  };
  
  //Function that displays all the courses that are in the file including the courses that are added by the user
  //Pre:  User inputs the correct file name for the file that has their course information
  //Post: Outputs all of the classes in the file for the user to see
  void displayAllCourses(string fileName);
  
  //Function that takes a user input course ID and displays that course info
  //Pre:  The user inputs a valid course ID
  //Post: The class the user searched for is displayed
  void searchCourse(string fileName);
  
  //Function that allows the user to add another class to the file
  //Pre:  user adds the information in the way they are prompted
  //Post:
  void addCourse(string fileName);
  
  // Create and return a new node.
  // Pre: None
  // Post: Returns a pointer to NodeType with all of the data filled in by user
  NodeType *CreateNode();
  
  // Add a given node to a list in a sorted position.
  // Pre: listPtr set to a valid list, or nullptr
  //      newNodePtr assigned to a valid node with valid component value
  // Post: listPtr is updated with nodePtr included in the sorted position
  void AddNode(string fileName, NodeType *&amp;listPtr, NodeType *newNodePtr);
  
  int main()
  {
      NodeType *headPtr;      // Always points to the first node in the list
      NodeType *lastPtr;      // Always points to the last node in the list
      NodeType *currPtr;      // Working pointer. Points to whatever node it needs to
      NodeType *newNodePtr;   // Points to a newly created node
      string fileName;        //Variable for the name of the file the user would like to open
      cout &lt;&lt; &quot;Please enter the name of the file you would like to open: &quot; &lt;&lt; endl;//Prompts the user for the name of the file they would like to open
      getline(cin, fileName);        //User inputs the file name they want to open
      char answer;
      while(1)                //Loop That repeats until the user inputs that they want to exit
      {
          //prompt the user for what action they would like to do
          cout&lt;&lt;&quot;Type D to display all your courses&quot;&lt;&lt;endl;
          cout&lt;&lt;&quot;Type S to search for a specific course&quot;&lt;&lt;endl;
          cout&lt;&lt;&quot;Type A to add another course&quot;&lt;&lt;endl;
          cout&lt;&lt;&quot;Type E to exit the program&quot;&lt;&lt;endl;
          cout&lt;&lt;&quot;What would you like to do: &quot;;
          cin &gt;&gt; answer;
          cout&lt;&lt;endl;
  
          //Compares the users choice to the case and executes the code for that case
          switch(answer)
          {
              case 'D':
              displayAllCourses(fileName);    //Function call to display all the courses in the file
              break;
              case 'S':
              {
                  searchCourse(fileName);     //Function call to search for a user defined course by ID
                  break;
              }
              case 'A':
                  CreateNode();
                  //AddNode(fileName, headPtr, newNodePtr);            //Function call to add another course to the file
                  break;
              default:
              exit(0);
          }
      }
  }
  
  NodeType *CreateNode(){
      NodeType *newNodePtr;   // Pointer for new node
      newNodePtr = new NodeType;
      string day;
      cout &lt;&lt; &quot;Please enter the Course name: &quot; &lt;&lt; endl;
      cin &gt;&gt; newNodePtr-&gt;data-&gt;courseName;
  
      cout &lt;&lt; &quot;Please enter Course ID: &quot; &lt;&lt; endl;
      cin &gt;&gt; newNodePtr-&gt;data-&gt;CourseID;
  
      cout &lt;&lt; &quot;Please enter the day of the course: &quot; &lt;&lt; endl;
      cin &gt;&gt; day;
  
      cout &lt;&lt; &quot;Please enter the time time of the course: &quot; &lt;&lt; endl;
      getline(cin, newNodePtr-&gt;data-&gt;times[0]);
      cout &lt;&lt; &quot;Please enter the building name: &quot; &lt;&lt; endl;
      getline(cin, newNodePtr-&gt;data-&gt;building);
      cout &lt;&lt; &quot;Please enter the room number: &quot; &lt;&lt; endl;
      getline(cin, newNodePtr-&gt;data-&gt;roomNum);
  
  
      if(day==&quot;Monday&quot;)
          newNodePtr-&gt;data-&gt;day[0] = Mon;
  
          else if(day==&quot;Tuesday&quot;)
              newNodePtr-&gt;data-&gt;day[0] = Tue;
  
          else if(day==&quot;Wednesday&quot;)
              newNodePtr-&gt;data-&gt;day[0] = Wed;
  
          else if(day==&quot;Thursday&quot;)
              newNodePtr-&gt;data-&gt;day[0] = Thu;
  
          else if(day==&quot;Friday&quot;)
              newNodePtr-&gt;data-&gt;day[0] = Fri;
  
      newNodePtr-&gt;next = nullptr;
  
      return newNodePtr;
  }
  
  void AddNode(string fileName, NodeType *&amp;listPtr, NodeType *newNodePtr){
      NodeType *currPtr = listPtr;    // Points to current node in list
      NodeType *prevPtr = nullptr;    // Points to node before the currPtr
  
      // 1 - Find position in list to add newNodePtr
      // Loop while not at end of list and new node value is greater than currPtr value to find sorted location
      while ((currPtr != nullptr) &amp;&amp;
             (newNodePtr-&gt;data-&gt;courseName.at(0) &gt; currPtr-&gt;data-&gt;courseName.at(0))){
  //        cout &lt;&lt; currPtr-&gt;component &lt;&lt; &quot; &quot;;        // Echo node be traversed for debugging purposes
          prevPtr = currPtr;
          currPtr = currPtr-&gt;next;
      }
      cout &lt;&lt; endl;
  
  
      // 2 - Insert node
      if ((prevPtr == nullptr) &amp;&amp; (currPtr == nullptr)){  // Pointers never moved, so first/only node
          listPtr = newNodePtr;                           // Set listPtr to new (only) node
      }
  
      else if (prevPtr == nullptr){                       // prevPtr never moved, so need to add to beginning of list
          newNodePtr-&gt;next = listPtr;                     // Point newNodePtr-&gt;next to current beginning of list
          listPtr = newNodePtr;                           // Make newNodePtr new first node in list
      }
  
      else if (currPtr == nullptr){
          prevPtr-&gt;next = newNodePtr;                     // prevPtr already pointing at last node, so add newPointer as new last node
      }
  
      else {
          newNodePtr-&gt;next = currPtr;                     // Point newNodePtr-&gt;next to currPtr
          prevPtr-&gt;next = newNodePtr;                     // Update prevPtr-&gt;next to insert newNodePTr in list
      }
      fstream file;
      file.open(fileName += &quot;.txt&quot;, fstream::app);    //Adds the new course the the course list in the file
      if(!file)
      {
          cout&lt;&lt;&quot;Unable to open file!&quot;;
          exit(1);
      }
  
      cout&lt;&lt;newNodePtr&lt;&lt;endl;
      file&lt;&lt;newNodePtr;
      file.close();
  }
  
  void displayAllCourses(string fileName)
  {
      ifstream file;
      file.open(fileName += &quot;.txt&quot;);              //Opens the user specified file
      if(!file)
      {
          cout&lt;&lt;&quot;Unable to open file!&quot;;           //Tells the user that the file could not open
          exit(1);
      }
  
      int counter=1;
      string fileInfo;                            //String to hold the file information
      while(getline(file, fileInfo))              //Loop to display everything in the file
      {
          cout&lt;&lt;counter&lt;&lt;&quot;: &quot;&lt;&lt;fileInfo&lt;&lt;endl;
          counter++;
      }
      cout&lt;&lt;endl;
      file.close();
  }
  
  void searchCourse(string fileName)
  {
      string courseName;                          //Variable to hold the ID that the user wants to search for
  
      cout&lt;&lt;&quot;Enter the course name: &quot; &lt;&lt; endl;            //Prompt the user for the ID of the course
      cin&gt;&gt;courseName;                            //User inputs the ID for the course
  
      ifstream file;
      file.open(fileName += &quot;.txt&quot;);              //Open user specified file
      if(!file)
      {
          cout&lt;&lt;&quot;Unable to open file!&quot;;
          exit(1);
      }
  
      string line;
  
      while( getline(file, line ))
      {
          size_t pos = line.find(courseName);
          if ( pos != string::npos)
              cout &lt;&lt; line &lt;&lt; endl;
      }
      cout&lt;&lt;endl;
      file.close();
  }
  </code></pre>