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

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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
#include <iostream>
#include <conio.h>
#include <windows.h>
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.