This is my first time with link lists. I have gotten it to pull the first bit of information into the first node, and it prints out fine after that into the console window.

When I try to add additional lists and link them, and then try to print them out, there is a blank line appearing at the top of the console window, then it prints the last data I entered. I'm not sure whats going on. My print function looks fine, as I know my first part works at least to get the first bit of data in. Could someone please point out where it is going wrong? The book we are using is really convoluted and the lecture the professor gave was really vague and had nothing similar to what we were seeing in the book, at least for a first go around with this. Code at the bottom.

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream> //for ifstream and ofstream


using namespace std;

ifstream dataIn;
ofstream dataOut;

struct LList
{
      
             string itemName;
             int itemNumber;
             int itemQuantity;
             float itemPrice;
             int itemSafeStock;
             char itemFlag;
             LList *linky;
             };

LList *head;


LList *newNode()
{
      LList *temp;
      temp = new LList();
      temp->itemName = "";
      temp->itemNumber = 0;
      temp->itemQuantity = 0;
      temp->itemPrice = 0;
      temp->itemSafeStock = 0;
      temp->itemFlag = ' ';
      temp->linky = NULL;
      return temp;
      }
             
void insert(LList *cp);
void printLL(LList *cp);

int main(int argc, char *argv[])
{
   dataIn.open("invt.txt");
    
    insert(head);
    printLL(head);
 
    system("PAUSE");
    return EXIT_SUCCESS;
}

void insert(LList *cp){
     
     for(int i = 0; i < 13; i++){ //I know I need to check for eof, this is just for testing
     LList *prev, *t;
     prev = NULL;
     
     //dataIn.open("invt.txt");
     
     char x = ' ';
     string tmpName;
     int tmpNumber;

     dataIn.get(x);
     for(int i = 0; i < 20; i++){
          tmpName = tmpName + x;
          dataIn.get(x);
     }
             
     dataIn >> tmpNumber;
  
     if(head == NULL){//checks to see if the list is empty, if it is, creates first node
             head = newNode();
             
             
             //pulls information from .txt into the node
             
             
             head->itemName = tmpName;
             head->itemNumber = tmpNumber;
             dataIn >> head->itemQuantity;
             dataIn >> head->itemPrice;
             dataIn >> head->itemSafeStock;
             
             if(head->itemQuantity < head->itemSafeStock)
             head->itemFlag = '*';
             
             head->linky = NULL;
             
             
     }
     ////////////////////////////////////////////
     
    else {
         while(cp != NULL && cp->itemNumber < tmpNumber){ //for ordered list by part number?
              
              prev = cp;
              cp = cp->linky;
              }
              if(prev != NULL){
                      t = newNode();
                      t->itemName = tmpName;
                      t->itemNumber = tmpNumber;
                      dataIn >> t->itemQuantity;
                      dataIn >> t->itemPrice;
                      dataIn >> t->itemSafeStock;
                      
                      if(t->itemQuantity > t->itemSafeStock)
                      t->itemFlag = '*';
                      
                      prev->linky = t;
                      t->linky = cp;
                      
                      
                      
                      }
                      else {
                           t=newNode();
                           t->itemName = tmpName;
                           t->itemNumber = tmpNumber;
                           dataIn >> t->itemQuantity;
                           dataIn >> t->itemPrice;
                           dataIn >> t->itemSafeStock;
                      
                           if(t->itemQuantity > t->itemSafeStock)
                           t->itemFlag = '*';
                           head = t;           
                    }
                    }
                    }
     }
void printLL(LList *cp){
     while(cp != NULL){
              cout << cp->itemName << "   " << cp->itemNumber << "   " << cp->itemQuantity << "   " <<
               cp->itemPrice << "   "<< cp->itemSafeStock << "   "<< cp->itemFlag << endl << endl;
              cp = cp-> linky;
              }
     }

I haven't looked too deeply into your code but at first glance there are a few things that are dangerous/undefined and wrong with your code. For a start: pointers in C++ (and C for that matter) are not automatically set to NULL (0) when they are declared. They have an undefined value (which might be NULL but is not neccessarily NULL). You should always explicitly set them to NULL at creation.
For a free standing variable you do this by

int* pInt = 0;

A member variable you set to 0 in the initialisation list of the constructors of your struct/class

struct Xyz
{
   Xyx() : pDbl(NULL)
   {
   }
private:
   double* pDbl;
};

Speaking of constructors: You should always provide the following 4 members of a class/struct that has a pointer variable:
1) Default constructor
2) Copy constructor
3) Assignment operator
4) destructor
If you do not, they are silently provided to you and unfortunately they will most likely not do what you would like them to do.
Once you sorted those things out, you are a huge step closer to make your program work.
Hope that helps.

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.