Linked List not appending?
May 1, 2015 at 11:51pm UTC
My professor is having us work with linked lists and create a "shopping list" with two different types of items (price by the pound, and each item). I'm able to get my file to read into a class. It appears to be appending to the next node, but for the life of me I can't get the showInfo to work. Here is my main(I can't find where it would be screwy), everything else is verbatim from my professor, include my linkedList, node, products, byTheItem:products, byThePound:products classes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
int main(){
string name;
cout << "Please enter file location: " ;
cin >> name;
ifstream fname(name);
if (!(fname.is_open())){
cout << "File not found" ;
return -1;
}
int pluCode;
char str[100];
int type;
double price;
double inventory;
linkedList * list = new linkedList();
while (!fname.eof()){
fname >> pluCode >> str >> type >> price >> inventory;
//cout << pluCode << str << type << price << inventory << endl;
if (type == -1 || type == 2){
cout << "File corrupt at ITEM_TYPE." << endl
<< "Please check file." << endl;
return -1;
}
else if (type == 0) {
byTheItem * items = new byTheItem();
items->setPLU(pluCode);
items->setProduct(str);
items->setItem(type);
items->setPrice(price);
items->setInven(inventory);
cout << endl << "Type is " << items->getItem() << "!" << endl;
node * n = new node();
n->setItem(items);
list->appendNode(n);
}
else if (type == 1){
byThePound * pounds = new byThePound();
pounds->setPLU(pluCode);
pounds->setProduct(str);
pounds->setType(type);
pounds->setPrice(price);
pounds->setInven(inventory);
cout << endl << "Type is " << pounds->getType() << "!" << endl;
node * n = new node();
n->setPound(pounds);
list->appendNode(n);
}
list->showInfoP();
}
return 0;
}
and my appendNode
1 2 3 4 5 6 7 8 9 10 11 12 13
void appendNode(node * n){
if (head == nullptr ){
head = n;
}
else {
node * iter = head;
while (iter->getNext()){
iter = iter->getNext();
}
iter->setNext(n);
}
size++;
}
May 2, 2015 at 11:51am UTC
can you show us the showInfoP() function so we can see what it is expecting to do?
Topic archived. No new replies allowed.