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;
}