title: Extra Output into Text File
body: <p>For some reason, when I am outputting my list to a text file, I am getting all of the nodes, except there is also an extra blank node being added to the beginning of the list like this:</p>
  
  <pre><code>(blank), (blank) $(blank)
  Ball, 20 $9.99
  </code></pre>
  
  <p>How can I fix this? I have tried deleting head after the while loop and incrementing nodePtr/head to --, but to no avail.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #define nullptr 0
  #include &lt;cstdlib&gt;
  #include &lt;algorithm&gt;
  #include &lt;string&gt;
  #include &lt;conio.h&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  
  
  class ItemList {
      private:
          struct ListNode{
              string IName;
              string QQuantity;
              string PPrice;
              struct ListNode * next;
                  };
  ListNode *head;
      public:
          ItemList()
              {
                  head = new ListNode;
                  head-&gt;next=nullptr;
              }
              ~ItemList();
  
          void insertNode(string Item, string Quantity, string Price)
              {
                  ListNode *newNode;
                  ListNode *nodePtr;
                  ListNode *previousNode=nullptr;
  
                  newNode=new ListNode;
                  newNode-&gt;IName=Item;
                  newNode-&gt;QQuantity=Quantity;
                  newNode-&gt;PPrice=Price;
  
                  if(!head)
                  {
                      head=newNode;
                      newNode-&gt;next=nullptr;
                  }
                  else
                  {
                      nodePtr=head;
                      previousNode=nullptr;
  
                      while(nodePtr != nullptr &amp;&amp; nodePtr-&gt;IName &lt; Item)
                      {
                          previousNode=nodePtr;
                          nodePtr=nodePtr-&gt;next;
                      }
                      if(previousNode==nullptr)
                      {
                          head=newNode;
                          newNode-&gt;next=nodePtr;
                      }
                      else
                      {
                          previousNode-&gt;next=newNode;
                          newNode-&gt;next=nodePtr;
                      }
                  }
              }
          void displayNode()
              {
                  ListNode *nodePtr;
                  nodePtr=head-&gt;next;
  
                  int i=0;
                  if(head-&gt;next==nullptr)
                  {
                      cout &lt;&lt; "The store is empty." &lt;&lt; endl;
                  }
                  while(nodePtr)
                  {
                      i++;
                      cout &lt;&lt; i &lt;&lt; ". " &lt;&lt; nodePtr-&gt;IName &lt;&lt; ", ";
                      cout &lt;&lt; nodePtr-&gt;QQuantity &lt;&lt; "  ";
                      cout &lt;&lt; "$" &lt;&lt; nodePtr-&gt;PPrice &lt;&lt; "\n" &lt;&lt; endl;
                      nodePtr=nodePtr-&gt;next;
                  }
  
              }
  
          void modifyNode(string Item)
              {
              ListNode *nodePtr;
              ListNode *nodePrev;
              string newName, newQuantity, newPrice;
              int modify;
              if (head==nullptr)
              {
  
                  return;
              }
              else
              {
                  nodePtr = head;
                  if (head-&gt;IName==Item)
                      nodePtr = head-&gt;next;
                  else
                  {
                      while (nodePtr != nullptr &amp;&amp; nodePtr-&gt;IName != Item)
                      {
                          nodePrev = nodePtr;
                          nodePtr = nodePtr-&gt;next;
                      }
                  }
                  if (nodePtr)
                  {
                      system("cls");
                      cout &lt;&lt; nodePtr-&gt;IName &lt;&lt; "\t" &lt;&lt; nodePtr-&gt;QQuantity &lt;&lt; "\t" &lt;&lt; nodePtr-&gt;PPrice &lt;&lt; endl;
                      cout &lt;&lt; "What would you like to change?\n";
                      cout &lt;&lt; "1. Item" &lt;&lt; endl;
                      cout &lt;&lt; "2. Quantity" &lt;&lt; endl;
                      cout &lt;&lt; "3. Price" &lt;&lt; endl;
                      cout &lt;&lt; "4. Whole Entry" &lt;&lt; endl;
                      cin &gt;&gt; modify;
                      switch (modify)
                      {
                          case 1:
                              cout &lt;&lt; "Change to what?\n";
                              cin.sync();
                              getline(cin,newName);
                              transform(newName.begin(), newName.end(), newName.begin(), ::toupper);
                              nodePtr-&gt;IName = newName;
                              break;
                          case 2:
                              cout &lt;&lt; "Change to what?\n";
                              cin &gt;&gt; newQuantity;
                              nodePtr-&gt;QQuantity = newQuantity;
                              break;
                          case 3:
                              cout &lt;&lt; "Change to what?\n";
                              cin &gt;&gt; newPrice;
                              nodePtr-&gt;PPrice = newPrice;
                              break;
                          case 4:
                              cout &lt;&lt; "What is the product called?\n";
                              cin.sync();
                              getline(cin,newName);
                              transform(newName.begin(), newName.end(), newName.begin(), ::toupper);
                              nodePtr-&gt;IName = newName;
                              cout &lt;&lt; "How many are there really?\n";
                              cin &gt;&gt; newQuantity;
                              nodePtr-&gt;QQuantity = newQuantity;
                              cout &lt;&lt; "What is the actual price?\n";
                              cin &gt;&gt; newPrice;
                              nodePtr-&gt;PPrice = newPrice;
                      }
                      newName=nodePtr-&gt;IName;
                      newQuantity=nodePtr-&gt;QQuantity;
                      newPrice=nodePtr-&gt;PPrice;
                      deleteNode(newName);
                      insertNode(newName, newQuantity, newPrice);
                      cout &lt;&lt; "Entry successfully changed." &lt;&lt; endl;
                  }
                  else
                      cout &lt;&lt; "Product not found\n";
              }
          }
  
          void deleteNode(string Item)
              {
                  ListNode *nodePtr;
                  ListNode *previousNode;
  
                  if(!head)
                      return;
                  if(head-&gt;IName==Item)
                  {
                      nodePtr=head-&gt;next;
                      delete head;
                      head=nodePtr;
                  }
                  else
                  {
                      nodePtr=head;
                      while(nodePtr!=nullptr &amp;&amp; nodePtr-&gt;IName!=Item)
                      {
                          previousNode=nodePtr;
                          nodePtr=nodePtr-&gt;next;
                      }
                      if(nodePtr)
                      {
                          previousNode-&gt;next=nodePtr-&gt;next;
                          delete nodePtr;
                      }
                      else
                      {
                          cout &lt;&lt; "Nothing to delete." &lt;&lt; endl;
                      }
                  }
              }
  
          void deleteRangeNode(int start, int stop)
              {
                  ListNode *nodePtr;
                  ListNode *newNode;
                  nodePtr = head;
                  int i=-1;
                  cin.sync();
  
                  while(nodePtr!=nullptr)
                  {
                      i++;
  
                      if((i&gt;=start)&amp;&amp;(i&lt;=stop))
                      {
                          newNode-&gt;next = nodePtr -&gt; next;
                          cout &lt;&lt; "Deleted Product: " &lt;&lt; nodePtr-&gt;IName &lt;&lt; endl;
                          delete nodePtr;
                          nodePtr=newNode;
                      }
                      newNode=nodePtr;
                      nodePtr=nodePtr-&gt;next;
  
  
                  }
  
              }
  
  
  
  
          void displayRange()
              {
                  ListNode * nodePtr;
                  nodePtr=head;
                  int i=-1;
                  int start, stop;
  
                  if(head-&gt;next==nullptr)
                  {
                      cout &lt;&lt; "The store is empty." &lt;&lt; endl;
                  }
                  else
                  {
                      cout &lt;&lt; "The display should START at: ";
                  cin &gt;&gt; start;
                  cout &lt;&lt; "The display should END at: ";
                  cin &gt;&gt; stop;
                  system("cls");
                  while(nodePtr!=nullptr)
                  {
                      i++;
                      if((i&gt;=start &amp;&amp; i&lt;=stop))
                      {
                          cout &lt;&lt; i &lt;&lt; ". " &lt;&lt; nodePtr-&gt;IName &lt;&lt; ",  ";
                          cout &lt;&lt; nodePtr-&gt;QQuantity &lt;&lt; "  ";
                          cout &lt;&lt; "$" &lt;&lt; nodePtr-&gt;PPrice &lt;&lt; "\n" &lt;&lt; endl;
                      }
                      nodePtr=nodePtr-&gt;next;
                  }
                  }
  
  
              }
  
          void deleteAllNodes()
              {
                  ListNode *temp = head;
                  while(head-&gt;next!=nullptr)
                  {
                      temp=head-&gt;next;
                      free(head);
                      head=temp;
                  }
              }
  
          void keepNodes()
          {
              ofstream FinalInventory;
              FinalInventory.open("store.txt");
              ListNode *nodePtr;
              nodePtr=head;
  
              while(nodePtr!=nullptr)
              {
                  FinalInventory &lt;&lt; nodePtr-&gt;IName &lt;&lt; ", " &lt;&lt; nodePtr-&gt;QQuantity &lt;&lt; "  $" &lt;&lt; nodePtr-&gt;PPrice &lt;&lt; endl;
                  nodePtr=nodePtr-&gt;next;
              }
              delete head++;
              FinalInventory.close();
          }
  };
  
  ItemList::~ItemList()
  {
      ListNode *nodePtr;
      ListNode *nextNode;
  
      nodePtr=head;
      while(nodePtr!=nullptr)
      {
          nextNode=nodePtr-&gt;next;
          delete nodePtr;
          nodePtr=nextNode;
      }
  }
  
  int main()
  {
      int menu();
      ItemList pro;
      int method;
      while(method!=0)
      {
      int method=menu();
      system("cls");
      string It, Q, P;
      int begin, end;
      switch(method)
      {
      case 1:
          int count;
          cout &lt;&lt; "How many products would you like to put in?" &lt;&lt; endl;
          cin &gt;&gt; count;
          system("cls");
          for(int i=0; i&lt;count; i++)
          {
              cout &lt;&lt; "Product #" &lt;&lt; i + 1 &lt;&lt; endl;
              cout &lt;&lt; "Enter the item name: ";
              cin.sync();
              getline(cin,It);
              transform(It.begin(), It.end(), It.begin(), ::toupper);
              cout &lt;&lt; "Enter the Quantity: ";
              cin &gt;&gt; Q;
              transform(Q.begin(), Q.end(), Q.begin(), ::toupper);
              cout &lt;&lt; "Enter the Price: ";
              cin &gt;&gt; P;
              pro.insertNode(It, Q, P);
              cout &lt;&lt; "\n";
          }
          break;
  
      case 2:
          int dis;
          cout &lt;&lt; "How many products would you like to display?" &lt;&lt; endl;
          cout &lt;&lt; "1. Entire Store" &lt;&lt; endl;
          cout &lt;&lt; "2. Range of Products" &lt;&lt; endl;
          cin &gt;&gt; dis;
          system("cls");
          switch(dis)
          {
              case 1:
                  pro.displayNode();
                  break;
              case 2:
                  system("cls");
                  pro.displayRange();
                  break;
          }
          break;
  
      case 3:
          pro.displayNode();
          cout &lt;&lt; "What product do you wish to modify? (by item name)" &lt;&lt; endl;
          cin.sync();
          getline(cin, It);
          transform(It.begin(), It.end(), It.begin(), ::toupper);
          pro.modifyNode(It);
          break;
  
      case 4:
          int del;
          cout &lt;&lt; "Do you wish to delete one product or more?" &lt;&lt; endl;
          cout &lt;&lt; "1. One" &lt;&lt; endl;
          cout &lt;&lt; "2. Range of Products" &lt;&lt; endl;
          cout &lt;&lt; "3. Entire Store" &lt;&lt; endl;
              cin &gt;&gt; del;
              system("cls");
              switch(del)
              {
                  case 1:
                      cout &lt;&lt; "What product do you wish to delete? (by item name)" &lt;&lt; endl;
                      pro.displayNode();
                      cin.sync();
                      getline(cin,It);
                      transform(It.begin(), It.end(), It.begin(), ::toupper);
                      pro.deleteNode(It);
                      cout &lt;&lt; "\nItem successfully deleted." &lt;&lt; endl;
                      cout &lt;&lt; "\n";
                      break;
                  case 2:
                      pro.displayNode();
                      cout &lt;&lt; "What range of items do you wish to delete? (by number)" &lt;&lt; endl;
                      cout &lt;&lt; "START: ";
                      cin &gt;&gt; begin;
                      cout &lt;&lt; "END: ";
                      cin &gt;&gt; end;
                      pro.deleteRangeNode(begin, end);
                      break;
                  case 3:
                      pro.deleteAllNodes();
                      cout &lt;&lt; "All items deleted." &lt;&lt; endl;
                      break;
              }
          break;
  
      case 5:
          pro.keepNodes();
          cout &lt;&lt; "The inventory has been saved." &lt;&lt; endl;
          break;
  
      case 0:
          cout &lt;&lt; "Exiting the program." &lt;&lt; endl;
          return 0;
      }
      system("pause");
      system("cls");
      }
      return 0;
  }
  
  int menu()
  {
      string space1= "                  ";
      string space2= "                                  ";
      int method;
      cout &lt;&lt; space1 &lt;&lt; "What would you like to do to the store's inventory?" &lt;&lt; endl;
      cout &lt;&lt; space2 &lt;&lt; "1. Add Product" &lt;&lt; endl;
      cout &lt;&lt; space2 &lt;&lt; "2. Display Products" &lt;&lt; endl;
      cout &lt;&lt; space2 &lt;&lt; "3. Modify Product" &lt;&lt; endl;
      cout &lt;&lt; space2 &lt;&lt; "4. Delete Product" &lt;&lt; endl;
      cout &lt;&lt; space2 &lt;&lt; "5. Save Inventory" &lt;&lt; endl;
      cout &lt;&lt; space2 &lt;&lt; "0. Exit\n" &lt;&lt; endl;
      cout &lt;&lt; space2;
      cin &gt;&gt; method;
      return(method);
  }
  </code></pre>