logic error?

so i wrote this whole program it compiles and runs on my system but was told it has a logic error can somebody help me find it and fix it? It supposedly occurs when these 2 classes are entered into the program (comsc-110 fa2013 4 a, and Math-292 SP2013 5 d)
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
	#include <iostream>
	using std::cin;
	using std::cout;
	using std::endl;
	using std::ios;
	using std::string;

	#include <iomanip>
	using std::setw;

	#include <fstream>
	using std::ofstream;
	using std::ifstream;

	#include <string>
	#include <cstring>
	#include <cstdlib>

	class course
	{
		public:
		char className[11]; // Class designation
		char term[7]; // Term
		int units; // Units
		char grade; // Grade
		course* next; // Next node
	};

	// Prototype(s)
	void printCourse(course*);
	int courseCmp(const course*, const course*);
	course* restoreList(course*);
	course* sortByUnit(course*);
	course* sortByGrade(course*);
	course* sortByDescription(course*);
	course* sortByTerm(course*);
	course* push_back(course*, course*);
	void saveList(course*);
	
	int main()
	{
		char classEntry; // Add a class?
		char load; // Load from disk?
	
		// Initially empty list
		course* head = 0;

		// prompt user and ask if they want to load previous file from disk
		cout << "Would you like to load classes from disk? [Y/N]: ";
		cin >> load;
		cout << endl;
		
		// if user selects yes restore and print
		if (load == 'Y' || load == 'y')
		{
			head = restoreList(head);
			printCourse(head);
		} // end if

		while(1)
		{
			printCourse(head);
			cout << endl;
			cout << "Would you like to enter another class? [Y/N]: "; 
			cin >> classEntry;
			cin.ignore(1000, 10);
			cout << endl;
		
			// if user enters no print class history and break
			if (classEntry == 'N' || classEntry == 'n')
			{
				printCourse(head);
				break;
			}
			
			// if yes selected begin adding to courselist
			if (classEntry == 'Y' || classEntry == 'y')
			{
				string unit;
				course* cEntry = new course;
				cout << endl;
				cout << "COURSE: Enter your course name [Ex: comsc-110, 10 Characters Max]: ";
				cin.getline(cEntry->className, 11);

				cout << "TERM: Enter the term [Ex: fa2011, 6 Characters Max]: ";
				cin.getline(cEntry->term, 7);
				
				cout << "UNITS: Enter # of units [1-5]: ";
				getline(cin, unit);
				cEntry->units = atoi(unit.c_str());
				
				// if enter a letter or non numeric prompts user to input correct info
				while (1)
				{
					if (cEntry->units == 0)
					{	cout << "Invalid number of units please enter [1-5] units: ";
						getline(cin, unit);
						cEntry->units = atoi(unit.c_str());
					}
					else
						break;
				}
			
				cout << "GRADE: Enter your grade [letter grade (A-F), or W if incomplete]: ";
				cin >> cEntry->grade;
				cin.ignore(1000, 10);
				
				head = push_back(head, cEntry);
			} 
		} 

	saveList(head);
	
	cout << endl;
	cout << "          Class History Sorted by Unit \n";
	cout << "          ---------------------------- \n";
	head = sortByUnit(head);
	printCourse(head);
	cout << endl;
			
	cout << "Press ENTER to view class history sorted by grade: " << endl;
	cin.get();

	cout << endl;
	cout << "          Class History Sorted by Grade \n";
	cout << "          ----------------------------- \n";
	head = sortByGrade(head);
	printCourse(head);
	cout << endl;

	cout << "Press ENTER to view class history sorted by term: " << endl;
	cin.get();

	cout << endl;
	cout << "          Class History Sorted by Term \n";
	cout << "          ---------------------------- \n";
	head = sortByTerm(head);
	printCourse(head);
	cout << endl;

	cout << "Press ENTER to view class history sorted by description: " << endl;
	cin.get();

	cout << endl;
	cout << "          Class History Sorted by Description \n";
	cout << "          -----------------------------------`\n";
 	head = sortByDescription(head);
	printCourse(head);
	cout << endl;
	 

	// Deallocate memory
		while (head)
		{
			course* n = head->next;
			delete head;
			head = n;
		} 

	} 
	
	// formatted function for history output
	void printCourse(course* head)
	{
	
		cout << endl;
		cout << "                CLASS HISTORY \n";
		cout << endl;
		cout.setf(ios::left, ios::adjustfield);
		cout << setw(20) << "     COURSE" << setw(10) << " TERM" << setw(10);
		cout << " UNITS" << setw(10) << " GRADE" << endl;
		cout << "     ------          ----      -----     -----" << endl; 

		for(course* i = head; i; i = i->next)
		{
			cout.setf(ios::left, ios::adjustfield);
			cout << "     " << setw(10) << i->className;
			cout << "      " << setw(10) << i->term;
			cout.setf(ios::adjustfield);
			cout << setw(3) << i->units;
			cout.setf(ios::right, ios::adjustfield);
			cout << setw(10) << i->grade;
			cout << endl;
			cout << "     ------------|----------|---------|--------" << endl;
		} 
	}

	// course comparison function
	int courseCmp(const course* a, const course* b)
	{
		// validate the length of the strings
		if ((strlen(a->term) != 6) || (strlen(b->term) != 6)) // in cstring
			return strcmp(a->className, b->className);
	
		// handle ties here
		if (stricmp(a->term, b->term) == 0)
			return stricmp(a->className, b->className);

		// compare the year
		int yearA = atoi(a->term + 2); 
		int yearB = atoi(b->term + 2);
		if (yearA < yearB)
			return -1; 

		if (yearA > yearB)
			return 1; 

		// compare semester in case of same year
		if (strnicmp(a->term, "SP", 2) == 0)
			return -1; 

		if (strnicmp(a->term, "SU", 2) == 0)
			return strnicmp(b->term, "SP", 2) ? -1 : 1;
			return 1;
	} 

	course* push_back(course* head, course* x)
	{
		course* t = head;
		if (t == 0)
		{
			x->next = 0;
			return x;

		}
		else
		{
			course* a = t;
		
			while(a->next)
			{
				a = a->next;
			} 
				a->next = x;
				return t;
		}
	} 

	course* restoreList(course* head)
	{
		ifstream fin;
		fin.open("courses.dat", ios::binary|ios::in);

		if (!fin)
		return head;

		// read the number of objects fromthe disk file
		int nRecs;
		fin.read((char*)&nRecs, sizeof(int));
		course* newHead = head;

		for (int i = 0; i < nRecs; i++)
		{
			course* x = new course;
			fin.read((char*)x, sizeof(course));
			x->next = 0;
			newHead = push_back(newHead, x);
		} 
		fin.close();
		return newHead;
	} 
	
	void saveList(course* head)
	{
		int counter = 0;
		ofstream fout;
		fout.open("courses.dat", ios::binary|ios::out|ios::trunc);
		if(!fout) cout << "Could not find and save file..." << endl;
		course* x = head;

		for(counter, x; x; ++counter, x = x->next);
		x = head;
		fout.write((char*)&counter, sizeof(int));
		for (x; x; x = x->next)
		{
			fout.write((char*)x, sizeof(course));
		}
		fout.close();
	}

	course* sortByUnit(course* head)
	{
		course* newHead = 0;
		course* oldHead = head;

		while(oldHead)
		{
			course* a = oldHead;
			oldHead = oldHead->next;

			course* x, *prev;

			for (x = newHead, prev = 0; x; prev = x, x = x->next)
			{
				if (a->units < x->units)
				break;
			}
		 
			a->next = x;

			if (prev)
				prev->next = a;
			else
				newHead = a;
		}
		return newHead;
	} 

	course* sortByGrade(course* head)
	{
		course* newHead = 0;
		course* oldHead = head;

		while (oldHead)
		{
			course* a = oldHead;
			oldHead = oldHead->next;
			course* x, *prev;

			for (x = newHead, prev = 0; x; prev = x, x = x->next)
			{
				if (tolower(a->grade) < tolower(x->grade))
				break;
			} 

			a->next = x;

			if (prev)
				prev->next = a;

			else
				newHead = a;
		} 
			return newHead;
	} 

	course* sortByDescription(course* head)
	{
		course* newHead = 0;
		course* oldHead = head;

		while (oldHead)
		{
			course* a = oldHead;
			oldHead = oldHead->next;
			course* x, *prev;

			for (x = newHead, prev = 0; x; prev = x, x = x->next)
			{
				if (strcmp(a->className, x->className) < 0)
				break;
			}

			a->next = x;
			if (prev)
				prev->next = a;
			else
				newHead = a;
		} 
		
		return newHead;
	}

	course* sortByTerm(course* head)
	{
		course* newHead = 0;
		course* oldHead = head;

		while (oldHead)
		{
			course* a = oldHead;
			oldHead = oldHead->next;
			course* x, *prev;

			for (x = newHead, prev = 0; x; prev = x, x = x->next)
			{
				if (courseCmp(a, x) < 0)
				break;
			} 

			a->next = x;
			if (prev)
				prev->next = a;
			else
				newHead = a;
		} 
			return newHead;
	} 
	
add cEntry->next = NULL; after line 80.
do the same at other instances.
Topic archived. No new replies allowed.