Run time error in linkList attemp

This is a discussion on Run time error in linkList attemp within the C++ Programming forums, part of the General Programming Boards category; there is this run time error that i just don't know how to fix. basically, i'm trying to make a ...

  1. #1
    Registered User
    Join Date
    Sep 2008
    Location
    Jakarta
    Posts
    18

    Run time error in linkList attemp

    there is this run time error that i just don't know how to fix. basically, i'm trying to make a shopping list using linkList. but a run time error has occur in my program. can someone please spot the mistake that i made. thx !

    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    class shopChart
    {
    	public:
    	
    		shopChart();
    		void addItem (int itemID, string itemName, string itemPrice);
    		void display ();
    		~shopChart ();
    	
    	private:
    	
    	struct shopList
    	{
    		int ID;
    		string price;
    		string name;
    		shopList *link;
    	
    	};
    
    	shopList *list;
    	
    };
    
    shopChart::shopChart ()
    {
    	list = NULL;
    }
    
    void shopChart::addItem (int itemID, string itemName, string itemPrice)
    {
    	shopList *item, *temp;
    	
    	if ( list = NULL )
    	{
    		list = new shopList;
    		list -> ID = itemID;
    		list -> price = itemPrice;
    		list -> name = itemName;
    		list -> link = NULL;
    	}
    	
    	else
    	{
    		item = list;
    		
    		while (item -> link != NULL){
    		
    			item = item -> link;
    			}
    			
    			
    			temp = new shopList;
    			temp -> ID = itemID;
    			temp -> name = itemName;
    			temp -> price = itemPrice;
    			temp -> link = NULL;
    			item -> link = temp;
    	}
    }
    
    void shopChart::display()
    	
    {
    
    	shopList *x;
    	cout<<endl;
    	
    	for (x = list; x != NULL; x = x -> link)
    	{
    		cout<< "Item ID : \t"<< x->ID <<endl;
    		cout<< "item Name : \t"<< x->name <<endl;
    		cout<< "item price : \t"<< x->price <<endl;
    	}
    }
    
    shopChart::~shopChart()
    {
    
    	shopList *x;
    	
    	if ( list == NULL )
    	{
    		return;
    	}
    	
    	while ( list != NULL )
    	{
    	
    		x = list -> link;
    		delete list;
    		list = x;
    	}
    
    }
    
    int main ()
    {
    
    	shopChart chart;
    	chart.addItem(1, "meja", "100.000");
    	chart.addItem(2, "kursi", "1000.000");
    	
    	chart.display();
    	
    	getchar();
    	return 0;
    
    }

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    if ( list = NULL )
    This looks rather suspect to me.

    Also, unless it is an exercise, there's no reason to code linked lists yourself, since the standard library has a selection of container available.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-26-2007, 01:11 PM
  2. Is this really true or it's just science fiction?
    By Nutshell in forum A Brief History of Cprogramming.com
    Replies: 145
    Last Post: 04-09-2002, 07:17 PM
  3. Replies: 2
    Last Post: 02-28-2002, 12:40 PM
  4. time class
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-11-2001, 10:12 PM
  5. run time error?
    By gogo in forum C++ Programming
    Replies: 5
    Last Post: 11-17-2001, 02:24 PM

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