linked list printing problem

This is a discussion on linked list printing problem within the C++ Programming forums, part of the General Programming Boards category; I have a data file, which reads the data in to a linked list using a for loop; Code: ifstream ...

  1. #1
    Registered User
    Join Date
    Apr 2005
    Posts
    1

    linked list printing problem

    I have a data file, which reads the data in to a linked list using a for loop;

    Code:
    ifstream datafile ("max.txt", ios :: in);
    
    		if (!datafile)
    		{
    			cout<<"Unable to open the file"<<endl<<endl;
    		}
    
    else	
    {	
    			datafile>>currentp->name;
    			datafile>>currentp->itemnumber;
    			datafile>>currentp->price;
    
    			for(int i=0; i<2; i++)
    			{
    			
    			currentp->nextp = new furniture;
    			currentp = currentp->nextp;
    			
    			datafile>>currentp->name;
    			datafile>>currentp->itemnumber;
    			datafile>>currentp->price;
    			}
    when i try to use a function to print out the data in the linked list the program crashes.

    Code:
    void printList(furniture* startp) 
    {
        furniture* currentp;
        
          currentp = startp;  
          while(currentp ->nextp !=NULL) 
    	  {
      	    cout << currentp->name<<"\t";
    		cout << currentp->itemnumber<<"\t";
    		cout << currentp->price<<endl;
            currentp = currentp->nextp;
    	  
        }
    	
        cout << endl;
    }
    any ideas.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It crashes because when you make your new nodes, you never set "thisnode->next" to NULL.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Code:
    while(currentp ->nextp !=NULL) 
    {
       ...
    
       currentp = currentp->nextp;
    }
    The last node has nextp = NULL which is assigned to currentp. What do you think happens when the while-loop tests its condition?

    I suggest changing the while condition to just currentp, not currentp->nextp.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Linked list program need help !!!
    By dcoll025 in forum C++ Programming
    Replies: 1
    Last Post: 04-20-2009, 10:03 AM
  2. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 10:10 AM
  3. singly linked circular list
    By DarkDot in forum C++ Programming
    Replies: 0
    Last Post: 04-24-2007, 08:55 PM
  4. How to use Linked List?
    By MKashlev in forum C++ Programming
    Replies: 4
    Last Post: 08-06-2002, 07:11 AM
  5. singly linked list
    By clarinetster in forum C Programming
    Replies: 2
    Last Post: 08-26-2001, 10:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21