### One loop in the link list is having some logical problem

At line 148:
 for (p = head; (strcmp(p->color,color)) != 0 && (p!=NULL); p = p->link);
My program crashes

First time pen is inserted. Second Time this loop starts having problem.

Question statement: https://www.box.com/s/kx16fae6276wnjb5rhx9

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248`` ``````#include #include #include using namespace std; //////////////////////////////////////////////////////////////////////////////////////////////////////// struct pen{ char color[20]; int qty; pen* link; }; //////////////////////////////////////////////////////////////////////////////////////////////////////// class ll{ public: ll(); void insert(char color[]); void sell(char color[]); void print(); private: pen *head, *tail; }; //////////////////////////////////////////////////////////////////////////////////////////////////////// int main(){ ll o; while(1){ cout << "Select your choice" << "\n1. Add Pen" << "\n2. Sell Pen" << "\n3. Display Stock" << "\n4. Exit\n"; char opt = getch(); switch(opt){ case '1': cout << "\nSelect Choice" << "\n1. Add Red" << "\n2. Add Green" << "\n3. Add Blue" << "\n4. Add Yellow" << "\n5. Add Aqua\n"; opt = getch(); switch(opt){ case '1': o.insert("Red"); break; case '2': o.insert("Green"); break; case '3': o.insert("Blue"); break; case '4': o.insert("Yellow"); break; case '5': o.insert("Aqua"); break; } break; case '2': cout << "\nSelect Choice" << "\n1. Sell Red" << "\n2. Sell Green" << "\n3. Sell Blue" << "\n4. Sell Yellow" << "\n5. Sell Aqua\n"; opt = getch(); switch(opt){ case '1': o.sell("Red"); break; case '2': o.sell("Green"); break; case '3': o.sell("Blue"); break; case '4': o.sell("Yellow"); break; case '5': o.sell("Aqua"); break; } case '3': o.print(); break; case '4': return 0; } system("cls"); } return 0; } //////////////////////////////////////////////////////////////////////////////////////////////////////// ll::ll(){ head = tail = 0; } //////////////////////////////////////////////////////////////////////////////////////////////////////// void ll::insert(char color[]){ cout << "Quantity: "; int quantity; cin >> quantity; //find if pen already exists? pen* p = 0; bool pen_exists = 0; if (head != NULL && tail != NULL){ for (p = head; (strcmp(p->color,color)) != 0 && (p!=NULL); p = p->link); } if (p!=NULL){ pen_exists = 1; } if (pen_exists){ p->qty+=quantity; //Pen added. } else{ //create new node for this color pen and insert it. pen *t = new pen; t->link = NULL; strcpy(t->color , color); t->qty = quantity; if (head == NULL && tail == NULL){ head = t; tail = t; } else if (color <= (head->color)){//insert at head t->link = head; head = t; } else if ( color >= (tail->color) ){ //insert at tail tail->link = t; tail = t; } else{ //insert somewhere in the middle. for (p = head; color < p->color && p!=NULL; p = p->link); //p points at node before which we've to insert. //so for (pen *b = head; b->link != p; b = b->link);// b points before p and at node after which we'll insert. //isert t->link = p; b->link = t; } } } //////////////////////////////////////////////////////////////////////////////////////////////////////// void ll::sell(char color[]){ //whether color exists? for (pen *p = head; p!=NULL && color != p->color; p = p->link); if (p!=NULL){ if (p->qty-1 != 0){ p->qty--; } else{//delete that node. for (pen *b = head; b->link != p; b = b->link);//b points just before p. //we've to delete p. b->link = p->link; delete p; p = NULL; } } else{ cout << color << " does not exists" << endl; } } //////////////////////////////////////////////////////////////////////////////////////////////////////// void ll::print(){ if (head == NULL && tail == NULL){ cout << "No Record." << endl; } else{ cout << "Pen Color \t\t Quantity" << endl; for (pen *p = head; p!=NULL; p = p->link){ cout <<" " << p->color <<" " << p->qty << endl; } } getch(); }``````
Last edited on
Topic archived. No new replies allowed.