Please help! My program is not reading in values from a .DAT file correctly

Hey guys I'm really stumped on this problem I'm having in my lab. We are supposed to split this lab into 3 files a .h and 2 .cpp's. The code I'll be talking about is going to be directed at the main.cpp file. Basically we are supposed to be able to enter as many classes into the program and when you are done the program is supposed to save the contents into a .dat file. This lab is working off of an older lab so I already have a .dat file I am trying to test with this program by reading it into my program when I first run it I prompt if the user wants to load previously entered courses and if they reply with a 'Y' the program will load the contents of the .dat file. Problem is right now when I load from the .dat file only the first class is correct. Every other class after that is just giberish. I've tried everything but can't get the restore part working. I also cannot get the save to .dat portion of this lab working either :( If anyone could help me out I'd greatly appreciate it.

Here is my code:

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
#include "Course.h"

#include <iostream>
using std::cout;
using std::cin;
using std::endl;
using std::ios;

#include <iomanip>
using std::setw;

#include <cstdlib>

#include <cstring>

//#include <cctype>

#include <string>
using std::strcpy;

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

#include <list>
using std::list;

void restoreList (list<Course>&);
void saveList (list<Course>&);

int main()
{
  char tempA;
  char tempB[100], tempC[100];
  char load;
  
  list <Course> myCourses;
  Course c;
  
  cout << "Load previous entered classes? (Y/N): ";
	cin >> load;
  
  if (load == 'Y' || load == 'y')
	{
    restoreList(myCourses);
	}
  
  while(true) // loop to create Course objects
  {
    cout.setf(ios::left, ios::adjustfield);
    cout << endl << setw(10) << "Course" << setw(10) << "Term" << setw(10)
    << "Units" << setw(10) << "Grade" << endl;
    cout << setw(10) << "------" << setw(10) << "------" << setw(10)
    << "------" << setw(10) << "------" << endl;
    
    list<Course>::iterator p;
    for (p = myCourses.begin(); p != myCourses.end(); p++)
      
      p->printMe();
    
    cout << "Would you like to add a class to the course list? (Y/N): ";
    cin >> tempA;
    cin.ignore(1000,10);
    if(tempA == 'n' || tempA == 'N') break;
    
    cout << "Please enter the course name: ";
    cin >> tempB;
    cin.ignore(1000,10);
    for(int i = 0; i < 100; i++)
    {
      tempC[i]= toupper(tempB[i]);
    }
    c.setName(tempC);
    
    cout << "Please enter the course term: ";
    cin >> tempB;
    cin.ignore(1000,10);
    c.setTerm(tempB);
    
    cout << "Please enter # of units: ";
    cin >> tempB;
    c.setUnit(atoi(tempB));
    
    cout << "Please enter your grade: ";
    cin >> tempA;
    c.setGrade(toupper(tempA));
    
    myCourses.push_back(c);

  }
  
  //Sorted by unit*****
  cout << endl;
  cout << "*****Sorted by unit: " << endl;

  cout.setf(ios::left, ios::adjustfield);
  cout << endl << setw(10) << "Course" << setw(10) << "Term" << setw(10)
  << "Units" << setw(10) << "Grade" << endl;
  cout << setw(10) << "------" << setw(10) << "------" << setw(10)
  << "------" << setw(10) << "------" << endl;
  
  myCourses.sort(Course::unitCmp);
  
  list<Course>::iterator p;
  for (p = myCourses.begin(); p != myCourses.end(); p++)    
    p->printMe();
  

  //Sorted by grades*****
  cout << endl;
  cout << "*****Sorted by grade: " << endl;
  
  cout.setf(ios::left, ios::adjustfield);
  cout << endl << setw(10) << "Course" << setw(10) << "Term" << setw(10)
  << "Units" << setw(10) << "Grade" << endl;
  cout << setw(10) << "------" << setw(10) << "------" << setw(10)
  << "------" << setw(10) << "------" << endl;
  
  myCourses.sort(Course::gradeCmp);
  
  list<Course>::iterator a;
  for (a = myCourses.begin(); a != myCourses.end(); a++)
    a->printMe();
  
  
  //Sorted by name*****
  cout << endl;
  cout << "*****Sorted by name: " << endl;
  
  cout.setf(ios::left, ios::adjustfield);
  cout << endl << setw(10) << "Course" << setw(10) << "Term" << setw(10)
  << "Units" << setw(10) << "Grade" << endl;
  cout << setw(10) << "------" << setw(10) << "------" << setw(10)
  << "------" << setw(10) << "------" << endl;
  
  myCourses.sort(Course::nameCmp);
  
  list<Course>::iterator b;
  for (b = myCourses.begin(); b != myCourses.end(); b++)
    b->printMe();
  
  
  //Sorted by term*****
  cout << endl;
  cout << "*****Sorted by term: " << endl;
  
  cout.setf(ios::left, ios::adjustfield);
  cout << endl << setw(10) << "Course" << setw(10) << "Term" << setw(10)
  << "Units" << setw(10) << "Grade" << endl;
  cout << setw(10) << "------" << setw(10) << "------" << setw(10)
  << "------" << setw(10) << "------" << endl;
  
  myCourses.sort(Course::courseCmp);
  
  list<Course>::iterator d;
  for (d = myCourses.begin(); d != myCourses.end(); d++)
    d->printMe();
  

  //saveList(myCourses);
  
}

void restoreList(list<Course>& myCourses)
{
  fstream fin;
  fin.open("courses.dat", ios::binary|ios::in);
  if (!fin)
    return;
  
  // read the number of objects from the disk file
  int nRecs;
  fin.read((char*)&nRecs, sizeof(int));
  
  // read the objects from the disk file
  for (int i = 0; i < nRecs; i++)
  {
    Course c;
    fin.read((char*)&c, sizeof(Course));
    myCourses.push_back(c);
  }
  fin.close();
}



This is my code from the previous lab we are supposed to build off of. The save to .dat function and restore from .dat works here but I just can't get it to work in my new lab.

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
390
391
392
393
394
395
396
397
398
399
400
401
402
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
using std::ios;

#include <iomanip>
using std::setw;

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

#include <cstring>

#include <cstdlib>

class course
{
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 courseCmp(const course*, const course*);
course* restoreList(course*);
course* sortByUnit(course*);
course* sortByGrade(course*);
course* sortByDescription(course*);
course* sortByTerm(course*);
course* deleteList(course*);
void saveList(course*);
course* push_back(course*, course*);
int myStricmp(const char*, const char*);
int myStrnicmp(const char*, const char*, int);

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

	course* head = 0; //initially empty list
	
	//ask if you want to load from disk
	cout << "Load previous entered classes? (Y/N): ";
	cin >> load;

	//yes, load and print 
	if (load == 'Y' || load == 'y')
	{
		head = restoreList(head);
	} 

	
	while(true)
		{
			dispCourse(head);
			
			//prompt user to enter class
			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')
			{
				cout << endl << "Thank you for entering your courses" << endl;
				break;
			} 

			else
			{
				
				course* temp = new course;
				cout << endl;

				dispCourse(head);
				cout << "Please enter the course name: "; 
				cin >> buf;
				cin.ignore(1000,10);

				for(int i = 0; i < 100 ; i++)
				{
					buff[i] = toupper(buf[i]);
				}

				strcpy(temp->classDes,buff);
				//cin.getline(temp->classDes, 11);


				
				cout << "Please enter the course term: "; 
				cin >> buf;
				cin.ignore(1000,10);
				strcpy(temp->term,buf);
				//cin.getline(temp->term, 7);
				
				cout << "Please enter # of units: ";
				cin >> temp->units;
				cin.ignore(1000, 10);
 
				cout << "Please enter your grade: "; 
				cin >> bufff;
				temp->grade = toupper(bufff);
				cin.ignore(1000, 10); 

				head = push_back(head, temp);
			} 
		}

	saveList(head);
	
	cout << endl;
	cout << "BY UNIT-" << endl;
	head = sortByUnit(head);
	dispCourse(head);
			
	cout << endl;
	cout << "PRESS ENTER TO CONTINUE" << endl;
	cin.get();

	cout << "BY GRADE-" << endl;
	head = sortByGrade(head);
	dispCourse(head);

	cout << endl;
	cout << "PRESS ENTER TO CONTINUE" << endl;
	cin.get();

	cout << "BY DESCRIPTION-" << endl;
 	head = sortByDescription(head);
	dispCourse(head);

	cout << endl;
	cout << "PRESS ENTER TO CONTINUE" << endl;
	cin.get();

	cout << "BY TERM-" << endl;
	head = sortByTerm(head);
	dispCourse(head);

  head = deleteList(head);
} 
 
void dispCourse(course* p)
{
		
	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;
		cout << setw(10) << i->term;
		cout << setw(10) << i->units;	
		cout << 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 c string
    return myStricmp(a->classDes, b->classDes);

  //handles ties
  if (myStricmp(a->term, b->term) == 0)
    return myStricmp(a->classDes, b->classDes);

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

  // compare semesters 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;
};

course* deleteList(course* dList)
{
	while(dList)
	{
		course* x = dList->next;
		delete dList;
		dList = x;
	}
	return dList;
} 

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;
		x->next = 0;
		return t;
	} 
} 

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

	if (!fin)
		return head;

	//read the number of objects from the disk file
	int nRecs;
	fin.read((char*)&nRecs, sizeof(int));

	//read the objects from the disk file
	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);
	course* x = head;

	for(;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 (a->grade < 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->classDes, x->classDes) < 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;
} 

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, int 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.