print database

have a program that is pretty much complete but i cant figure out how to print the blank course list before prompting the user if they would like to add to it... please help this is what i have so far
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
#include <iostream>
using std::cin;
using std::cout;
using std::endl;

#include <iomanip>
using std::setw;
using std::left;

#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 myStrCpy(const char*, const char*);
int myStrCmp(const char*, const char*, size_t count);
int courseCmp(const course*, const course*);

int main()
{
	
	// Declarations
	char classEntry;
	
	// Initially empty list
	course* head = 0;
	course* cEntry = 0;
	
	while(1)
	{
		// Prompt user to enter class
		printCourse(head);
		cout << "Would you like to enter another class? [Y/N]: "; 
		cin >> classEntry;
		cin.ignore(1000, 10);

		// Break if answer is "no"
		if (classEntry == 'N' || classEntry == 'n')
		{
			printCourse(head);
			break;
		}

		// Otherwise enter all data
		else
		{
			cEntry = new course;
			cout << "Enter course name (10 characters max): ";
			cin.getline(cEntry->className, 11);

			cout << "Enter term (6 characters max): ";
			cin.getline(cEntry->term, 7);

			cout << "Enter units: ";
			cin >> cEntry->units;
			cin.ignore(1000, 10);

			cout << "Enter grade (letter grade or X if incomplete): ";
			cin >> cEntry->grade;
			cin.ignore(1000, 10);
			
			// If head is empty, create list
			if(head == 0) 
			{
					head = cEntry;
					head->next = 0;
			}

			// Otherwise compare classes and arrange in proper order
			else
			{
				course* x = head;
				if (courseCmp(x, cEntry) == 1)
				{
					while (courseCmp(x, cEntry) == 1)
					{
						if (x->next != 0 && courseCmp(x->next, cEntry) != -1)
							x = x->next;

						else
							break;
					} // End else

					cEntry->next = x->next;
					x->next = cEntry;
				 } // End if

				else
				{
					cEntry->next = x;
					head = cEntry;
				}
			} // End else
		} // End outer else
	} // End while

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

void printCourse(course* p)
{
	if (p)
	{
    cout << endl;
    cout << "*Current class history*\n" << endl;
	cout << left << setw(10) << "COURSE" << setw(10) << "TERM" << setw(10) << "UNITS" << setw(10) << "GRADE" << endl;
	cout << "-------   ------    ------    ------" << endl; 

	for(course* d = p; d;d = d->next)
	{
		cout << left << setw(10) << d->className;
		cout << left << setw(10) << d->term;
		cout << left << setw(10) << d->units;
		cout << left << setw(10) << d->grade << endl;
    } 
	} // end if (p)

}

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 myStrCpy(a->className, b->className);
	
	// handle ties here
	if (myStrCpy(a->term, b->term) == 0)
		return myStrCpy(a->className, b->className);

	// compare the year
	int yearA = atoi(a->term + 2); // atoi is found
	int yearB = atoi(b->term + 2); // in cstdlib
	if (yearA < yearB)
		return -1; // termA comes first

	if (yearA > yearB)
		return 1; // termB comes first

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

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


int myStrCpy(const char* dst, const char* src)
{
	int f, l;
	do
	{
		if (((f = (unsigned char)(*(dst++))) >= 'A') && (f <= 'Z')) f -= 'A' - 'a';
		if (((l = (unsigned char)(*(src++))) >= 'A') && (l <= 'Z')) l -= 'A' - 'a';
	} while (f && (f == l));
  return(f - l);
}

int myStrCmp(const char* dst, const char* src, size_t count)
{
	int f, l;
	do
	{
		if (((f = (unsigned char)(*(dst++))) >= 'A') && (f <= 'Z')) f -= 'A' - 'a';
		if (((l = (unsigned char)(*(src++))) >= 'A') && (l <= 'Z')) l -= 'A' - 'a';
    } while (--count && f && (f == l));
  return (f - l);
}
Topic archived. No new replies allowed.