title: c++ linked list storing strings
body: <p>I am creating a custom linked list class to store strings from a program I created for an assignment. We were given a linked list handout that works for ints and were told to retool it for string storage, however I am running into an error when trying to run it.</p>
  
  <p>I'm getting the error ""terminate called after throwing an instance of 'std::logic_error'
  what(): basic_string::_S_construct null not valid"" (which I searched around and found it was because of a string being set to null, however I do not know how to fix the error, I'm guessing it is with line 8 but I've toyed around with it to no success.) I've searched around and looked through the similar questions but could not find anything that helped.</p>
  
  <pre><code>    #include &lt;cstdlib&gt;
      #include &lt;iostream&gt;
      #include &lt;string&gt;
      #include &lt;cstdio&gt;
      #include &lt;iomanip&gt;
      using namespace std;
  
     struct node {
     node(string current) { data=current; next=NULL; }
     string data;
     node *next;
      };
  
      class list {
      public:
        list(int N=0, string current);
        ~list();
        bool empty() const { return N == 0; }
        void clear();
  
        void insert(int, const string &amp;);
  
        void push_front(const string &amp;current);
  
        friend ostream &amp; operator&lt;&lt;(ostream &amp;out, const list &amp;current);
  
      private:
        int N;
        node *head;
        node *findnode(int);
        };
  
      list::list(int M, string current) {
        N = M;
       head = new node;
       for (int i=0; i&lt;N; i++)
         insert(0, current);
      }
  
     list::~list() {
        clear();
       delete head;
      }
  
  
      void list::clear() {
        while (!empty()) remove(0);
      }
  
      void list::insert(int i, const string &amp;din) {
        node *p = new node(din);
        node *pp = findnode(i-1);
        p-&gt;next = pp-&gt;next;
        pp-&gt;next = p;
        N++;
      }
  
      inline
      node *list::findnode(int i) {
        if (i == -1)
          return head;
        node *p = head-&gt;next; 
        while (i--)
          p = p-&gt;next;
        return p;
      }
  
  
      void list::push_front(const string &amp;current) {
        head = new node;
        head-&gt;next;
      }
  
      ostream&amp; operator&lt;&lt;(ostream&amp; out, const list&amp; current)
      {
       out &lt;&lt; current;
       return out;
     }
  
  
      const string rank[] = { "Ace", "2", "3", "4", "5", "6", "7",
                              "8", "9", "10", "Jack", "Queen", "King" }; 
      const string suit[] = { "Clubs", "Diamonds", "Hearts", "Spades" };
  
      string random_card(bool verbose=false) {
          string card;
  
          card = rank[ rand()%13 ];
          card += " of ";
          card += suit[ rand()%4 ];
  
          if (verbose)
            cout &lt;&lt; card &lt;&lt; "\n";
  
          return card;
      }
  
      int main(int argc, char *argv[])
      {
          bool verbose = false;
          int seedvalue = 0;
          string stop_card = "Queen of Hearts";
  
          for (int i=1; i&lt;argc; i++) {
            string option = argv[i];
            if (option.compare(0,6,"-seed=") == 0) {
              seedvalue = atoi(&amp;argv[i][6]);
            } else if (option.compare(0,6,"-stop=") == 0) {
              stop_card = &amp;argv[i][6];
            } else if (option.compare("-verbose") == 0) {
              verbose = true;
            } else 
              cout &lt;&lt; "option " &lt;&lt; argv[i] &lt;&lt; " ignored\n";
          }
  
          srand(seedvalue);
  
  
          list deck[4];
  
  
          while (1) {
            string card = random_card(verbose);
            char first[10];
            char second[10];
            sscanf(card.c_str(), "%s of %s", first,second);
  
          // reverse engineer card suit and rank
  
            int index2;
  
            //suit index
            for(int i=0; i&lt;4; i++){
              if(suit[i]==second){       
                index2=i;
              break;
            }
            }
  
            deck[index2].push_front(first);
  
          if (card.compare(stop_card)==0){
            break;
          }
  
          }
  
  
       // print formatted table contents to stdout 
      cout &lt;&lt; "Clubs : "; 
         cout &lt;&lt; setw(3) &lt;&lt;  deck[0];
       cout &lt;&lt; endl;
  
       cout &lt;&lt; "Diamonds : ";
         cout &lt;&lt; setw(3) &lt;&lt;  deck[1];
       cout &lt;&lt; endl;
  
       cout &lt;&lt; "Hearts : ";
         cout &lt;&lt; setw(3) &lt;&lt; deck[2];
       cout &lt;&lt; endl;
  
       cout &lt;&lt; "Spades :  ";
       cout &lt;&lt; setw(3) &lt;&lt; deck[3];
       cout &lt;&lt; endl;
  
      }
  </code></pre>