Could use help initializing an empty list and head. So close to working 100% correctly.

Hi,

I'm working on my project and I could use a little help. When I compile and execute my program I am getting this funky junk text that says something like "x86\Microsoft Vi71856859 t". I can't figure out what is causing this message or how to get rid of it. I did notice however that when I change my initially empty list to "course* head, *temp = 0;" instead of
1
2
3
4
course* temp,*head = 0; //initially empty list 
head = new course;
head -> next = 0;  
temp = head;


The strange microsoft text goes away. However when I submitted my code like that my professor told me "that's actually NOT an initially empty list head is not initialized at all and remember that you cannot count on uninitialized values being zero."

If anyone knows how to fix this I would greatly appreciate any help.

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
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::ios;

#include <iomanip>
using std::setw;


#include <cstring>

#include <cstdlib>

//create class course
class course //create a class named course with 4 elements
{
public:
  char classDes[11]; // course designation ex. COMSC-165, accomodate 10 characters
  char term[7]; // term ex. FA2010
  int units; //units
  char grade; //grade
  course* next; //prepare your class to be used in a linked list by adding a next pointer
};

//prototypes
void dispCourse(course*);
int myStricmp(const char*, const char*);
int myStrnicmp(const char*, const char*, size_t count);
int courseCmp(const course*, const course*);

int main()
{  
	//variables	
	char classPrompt;
	char buf[100];

	course* head, *temp = 0; //initially empty list

	/*
	course* temp, *head = 0; //initially empty list
	head = new course;
	head -> next = 0;  
	temp = head;
	*/

	while(true)
	{
		//Prompt user to enter class
		dispCourse(head);

		cout << "Would you like to add a class to the course list? (Y/N): ";
		cin >> classPrompt;
		cin.ignore(1000, 10);

		if (classPrompt == 'n' || classPrompt == 'N') //if user input N break
		{
			cout << endl << "Thank you for entering your courses" << endl;
			break;
		}

		else //if user doesn't break..
		{
			//prompt user
			temp = new course;
			cout << "Please enter the course name: ";
			cin.getline(temp -> classDes, 11);

			cout << "Please enter the course term: ";
			cin.getline(temp -> term, 7);

			cout << "Please enter # of units: ";
			cin >> buf; (temp -> units) = atoi(buf);
			cin.ignore(1000, 10);

			cout << "Please enter your grade: ";
			cin >> temp -> grade;
			cin.ignore(1000, 10);
			cout << endl;
			
			//If head is empty create list
			if(head == 0) 
			{
					head = temp;
					head->next = 0;
			}

			//compare/arange in order
			else
			{
				course* x = head;
				if (courseCmp(x, temp) == 1)
				{
					while (courseCmp(x, temp) == 1)
					{
						if (x->next != 0 && courseCmp(x->next, temp) != -1)
							x = x->next;
						else
							break;
					} 

					temp->next = x->next;
					x->next = temp;
				 } 

				else
				{
					temp->next = x;
					head = temp;
				}
			} 
		} 
	} 

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

void dispCourse(course* p)
{
	cout.setf(ios::right, ios::adjustfield);
	cout << setw(10) << "Course" << setw(10) << "Term" << setw(10) << "Units" << setw(10) <<  "Grade" << endl;
	cout << setw(10) << "-----" << setw(10) << "-----" << setw(10) << "-----" << setw(10) << "-----" << endl;
	
  for(course* i = p; i;i = i->next)
  {

	cout.setf(ios::right, ios::adjustfield);
    cout << setw(10) <<*i->classDes <<
    setw(10) << i->term <<
	setw(10) << i->units <<
	setw(10) << i->grade <<
    endl;

  }
}

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

	//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 (myStrnicmp(a->term, "SP", 2) == 0)
		return -1; // termA comes first

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


int myStricmp(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 myStrnicmp(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);
}

course* head still has garbage.
1
2
course* head = 0;
course* temp = 0;


consider creating a constructor that initializes all its member variables to at least zero. Makes life easier.
How come course* head still has garbage even though I initialized it to 0 in the beginning?

Could you please elaborate on how you would go about initializing all the head member variables to 0?
ovaltine99 wrote:
course* head, *temp = 0; //initially empty list
This initializes 'temp' to 0, it does not initialize 'head' to anything.
Ah! I see I see my problem so of course I need to initialize both head and temp to 0 by what pogrady posted above. I was a little confused I thought what I was doing was initializing both head and temp to 0 but I guess it was just initializing temp to 0 and not head.

1
2
course* head = 0;
course* temp = 0;


This is all I need to change correct?
Test it and find out ;)

Also, this syntax would work as well:

course *head = 0, *temp = 0;
Topic archived. No new replies allowed.