title: terminate called after throwing an instance of 'std::logic_error' what(): basic_string::_S_construct null not valid
body: <p>I have been trying to read cfg into a multilinklist but im getting this error please someone help. It keeps on giving this error. I dont think there's any error. Please check this code and tell me where is the error which causes the program to crash with the exception attached in the picture</p>
  
  <hr>
  
  <blockquote>
  <pre><code> #include &lt;iostream&gt;
      #include &lt;fstream&gt;
      #include &lt;cstring&gt;
  
      using namespace std;
  
      struct prod_list{
  
         string production;
       struct prod_list *next;
  
      }*head_p=NULL;
  
      //Node for linked list to store Non terminals
  
      struct rule_list{
  
        char non_terminal;
        struct prod_list *first;
        struct rule_list *next;
  
      }*head_r=NULL;
  
      // Global variable
         struct prod_list *temp_p,*prev_p=NULL,*first_p=NULL,*temp1_p=NULL,*temp2_p,*newTemp;
         struct rule_list *temp_r,*prev_r=NULL,*first_r=NULL ,*temp1_r=NULL;
  
  
  
      prod_list *adding_productions_of_the_rule(string line);
      prod_list *add_node(prod_list *root, string production);
  
  
      int main()
      {
  
      string line;
  
      ifstream file ("cfg.txt");
      if (file.is_open())
      {
        while ( getline (file,line) )
        {
              head_p = NULL;
  
              // Creating rule_list node
              temp_r = new rule_list;
              temp_r-&gt;non_terminal = line[0];
              head_p = adding_productions_of_the_rule(line);
              temp_r-&gt;first = head_p;
              temp_r-&gt;next = NULL;
  
              if(head_r == NULL)
              {
                  head_r = temp_r;
                  prev_r  = temp_r;
              }
              else
              {
                  prev_r-&gt;next = temp_r;
                  prev_r = temp_r;
              }
  
  
        }
        file.close();
      }
      else
      {
          cout &lt;&lt; "Unable to open file";
      }
  
      return 0;
      }
  
  
  
  
  
  
  
      prod_list *adding_productions_of_the_rule(string line)
      {
  
              string prod = NULL;
  
              // Splitting line after arrow in cfg rule line.
              line = line.substr(3,line.length());
              // cout &lt;&lt; "The cfg is "+line &lt;&lt; endl;
  
              // declaring character array of line's length
              char ch[line.length()]={0};
              // memset(ch,'0',line.length());
              // cout &lt;&lt; sizeof(ch) &lt;&lt; endl;
  
  
              // storing line in character array and printing character array
              for(int i=0; i&lt;line.length(); i++){
                  ch[i] = line[i];
                  // Displaying the characters stored in character array from string line.
                  // cout &lt;&lt; ch[i];
              }
              // cout &lt;&lt; endl;
  
              char *point;
              point = strtok(ch, "|");
              while(point != NULL){
  
                   // cout &lt;&lt; point &lt;&lt; endl;
  
                  // Assigning each production from the cfg rule to an array of strings
                  // production[i] = point;
                  // cout &lt;&lt; production[i] &lt;&lt; endl;
  
  
                  // Creating prod_list node
                  prod = point;
                  head_p = add_node( head_p, prod );
  
  
                  point = strtok(NULL, "|");
                  // i++;
  
              }
  
  
              return head_p;
      }
  
  
  
  
  
  
      prod_list *add_node(prod_list *root, string production){
  
  
      prod_list *next_ptr = NULL;
      prod_list *prev_ptr = NULL;
  
      if(root == NULL){
          //list is empty
          if( (root = new prod_list) != NULL){
              next_ptr = root;
              // cin &gt;&gt; next_ptr-&gt;title;
              next_ptr-&gt;production = production;
              next_ptr-&gt;next = NULL;
  
          }else{
              cout &lt;&lt; "Error: Unable to allocate memory. " &lt;&lt; endl;
              return NULL;
          }
          return next_ptr;
      }else{
          //list has members.
          next_ptr = root;
          while(next_ptr-&gt;next != NULL){
              next_ptr = next_ptr-&gt;next;
          }
          prev_ptr = next_ptr;
  
          if((next_ptr = new prod_list)!= NULL){
              prev_ptr-&gt;next = next_ptr;
              next_ptr-&gt;production = production;
              next_ptr-&gt;next = NULL;
          }else{
              cout &lt;&lt; "Error: Unable to allocate memory " &lt;&lt; endl;
          }
  
      }
      return root;
      }
  </code></pre>
    
    <p>**I have been trying to read cfg into a multilinklist but im getting this error please someone help **</p>
  </blockquote>
  
  <p><a href="https://i.stack.imgur.com/AgNXB.png" rel="nofollow noreferrer">https://i.stack.imgur.com/AgNXB.png</a></p>