ofstream Assistance

Hello guys,

so my ofstream is doing something very strange, it is spitting out junk to the file when I am writing out, i am using << which has worked for me in the past. right now it is outputting 3 records, then junk, then the record i just added. to be blunt i am thoroughly confused. so please any help would be great.
NOTE: this is an Assignment so please no code just thrown at me, im trying to learn so the more info you can give the better, also what you see is what I can use, so pointer classes etc are not allowed :( life would be easier hey?

the problem I believe starts in ass1.cpp right at the bottom :)

main.cpp
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
  #include <iostream>
#include "ass1.h"
#include <fstream>

using namespace std;

char Menu();

int main()
{
	int recordCount = 0;
	ifstream fileIn;
	ofstream fileOut;
	animal records[totalRecords];

	while (true)
	{
		switch(Menu())
		{
			case 'l':
				loadData(fileIn, records, recordCount);
				fileIn.close();
				break;
			case 'd':
				displayRecords(records);
				break;
			case 'a':
				addRecord(records, recordCount, totalRecords);
				updateFile(fileOut, records, recordCount);
				break;
			case 's':
				searchRecords();
				break;
			case 'e':
				eraseRecord();
				break;
			case 'u':
				undoErase();
				break;
			case 'q':
				cout << "Thank you for using the Lost and Found Pet Directory\n";
				return 1;
			default:
				cout << "Invalid command!\n";
		}
	}
	return 0;
}

char Menu()
{
	char cmd;
	cout << "*********************************"<< endl;
	cout << "*  Lost and Found Pet Directory *"<< endl;
	cout << "*     (l)oad data file          *"<< endl;
	cout << "*     (d)isplay records         *"<< endl;
	cout << "*     (a)dd record to DB        *"<< endl;
	cout << "*     (s)earch records          *"<< endl;
	cout << "*     (e)rase last record       *"<< endl;
	cout << "*     (u)ndo erase              *"<< endl;
	cout << "*     (q)uit                    *"<< endl;
	cout << "*********************************"<< endl;
	cout << "Command: ";
	cin >> cmd;
	cin.ignore();
	return cmd;
}


ass1.h
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
  #ifndef ASS1_H
#define ASS1_H
#include <fstream>
using namespace std;

const int maxStringLength = 128;
const int totalRecords = 50;

struct animal{
	char status[maxStringLength];
	char type[maxStringLength];
	char gender[maxStringLength];
	char breed[maxStringLength];
	short int ageYears;
	short int ageMonths;
	char colour[maxStringLength];
	char chipNum[maxStringLength];
	char location[maxStringLength];
	int phoneNum;
};

bool loadData(ifstream&, struct animal records[totalRecords], int&);
void displayRecords(struct animal records[totalRecords]);
bool addRecord(struct animal records[totalRecords], int&, int);
bool searchRecords();
bool eraseRecord();
bool undoErase();
char recordDisplay(struct animal records[totalRecords], int, char&);
bool updateFile(ofstream&, struct animal records[totalRecords], int);



#endif. 


ass.cpp
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
  #include <iostream>
#include "ass1.h"
using namespace std;

bool loadData(ifstream& fileLoad, struct animal records[totalRecords], int& recordCount)
{
	cout << "in loadData" << endl;
	int i = 0;
	fileLoad.open("pets2.dat");

	if(!fileLoad.good())
	{
		cerr << "file cant be opened" << endl;
		return false;
	}
	else
	{
		while(fileLoad.getline(records[i].status, maxStringLength, '\n'))
		{
			recordCount++;
			fileLoad >> records[i].type >> records[i].gender;
			fileLoad.ignore();
			fileLoad.getline(records[i].breed, maxStringLength, '\n');
			fileLoad >> records[i].ageYears >> records[i].ageMonths >> records[i].colour >> records[i].chipNum;
			fileLoad.ignore();
			fileLoad.getline(records[i].location, maxStringLength, '\n');
			fileLoad >> records[i].phoneNum;
			fileLoad.ignore();
			//cout << records[i].status << " " << records[i].type << records[i].gender << records[i].breed << records[i].ageYears << records[i].ageMonths << records[i].colour << records[i].chipNum << records[i].location << records[i].phoneNum << endl;
			i++;
		}

		cout << "There are " << recordCount << " records in the directory" << endl;
		return true;
	}

	
	return true;
}

void displayRecords(struct animal records[totalRecords])
{
	//display first record
	//offer option to display next or quit
	//if quit close
	//if display next show next
	//continue until no is chosen

	int i = 0;
	char cmd;
	
	while(cmd != 'n')
	{
		switch(recordDisplay(records, i, cmd))
		{
			case 'y':
				i++;
				recordDisplay(records, i, cmd);
				break;
			case 'n':
				cout << "Returning to Main Menu" << endl;
				break;
		}
	}
}

char recordDisplay(struct animal records[totalRecords], int i, char& cmd)
{
	cout << "in displayRecords" << endl;
	cout << "Status: " << records[i].status << endl;
	cout << "Type: " << records[i].type << endl;
	cout << "Gender: " << records[i].gender << endl;
	cout << "Breed: " << records[i].breed << endl;
	cout << "Age: " << records[i].ageYears << " years " << records[i].ageMonths << " months" << endl;
	cout << "Colour: " << records[i].colour << endl;
	cout << "Microchip: " << records[i].chipNum << endl;
	cout << "Location: " << records[i].location << endl;
	cout << "Telephone: " << records[i].phoneNum << endl;
	cout << "Would you like to display another record? (y/n): ";
	cin >> cmd;
	cin.ignore();

	return cmd;
}

bool addRecord(struct animal records[totalRecords], int& recordCount, int totalRecords)
{
	cout << "in addRecord" << endl;
	recordCount++;
	//check to see if index > totalrecords
	//if yes return false
	//if no input data
	//update file
	if(recordCount > totalRecords)
	{
		cerr << "Sorry the directory is full" << endl;
		recordCount--;
		return false;
	}
	else
	{
		cout << "Status: ";
		cin >> records[recordCount].status;
		cout << "Type: ";
		cin >> records[recordCount].type;
		cout << "Gender: ";
		cin >> records[recordCount].gender;
		cout << "Breed: ";
		cin >> records[recordCount].breed;
		cout << "Age (YY MM): ";
		cin >> records[recordCount].ageYears >> records[recordCount].ageMonths;
		cout << "Colour: ";
		cin >> records[recordCount].colour;
		cout << "Microchip: ";
		cin >> records[recordCount].chipNum;
		cout << "Location: ";
		cin >> records[recordCount].location;
		cout << "Telephone: ";
		cin >> records[recordCount].phoneNum; 
	}
	cout << records[recordCount].status << " " << records[recordCount].type << records[recordCount].gender << records[recordCount].breed << records[recordCount].ageYears << records[recordCount].ageMonths << records[recordCount].colour << records[recordCount].chipNum << records[recordCount].location << records[recordCount].phoneNum << endl;
	return true;
}

bool searchRecords()
{
	cout << "in searchRecords" << endl;
	return true;
}

bool eraseRecord()
{
	cout << "in removeRecord" << endl;
	return true;
}

bool undoErase()
{
	cout << "in undoRemove" << endl;
	return true;
}

bool updateFile(ofstream& fileOut, struct animal records[totalRecords], int recordCount)
{
	//open file
	//check to see if file open is good
	//if yes
	//loop through struct array outputting data
	//return true
	//if no return false
	fileOut.open("pets2.dat", ios::out | ios::trunc);

	if(!fileOut.good())
	{
		cerr << "Cannot Open File" << endl;
		return false;
	}
	else
	{
		for(int i = 0; i <= recordCount; i++)
		{
			fileOut << records[i].status << '\n' << records[i].type << '\n' << records[i].gender << '\n' << records[i].breed << '\n' << records[i].ageYears << " " << records[i].ageMonths << '\n' << records[i].colour << '\n' << records[i].chipNum << '\n' << records[i].location << '\n' << records[i].phoneNum << endl;
			cout << records[i].status << " " << records[i].type << records[i].gender << records[i].breed << records[i].ageYears << records[i].ageMonths << records[i].colour << records[i].chipNum << records[i].location << records[i].phoneNum << endl;
			fileOut.clear();
		}
	}
	return true;
}
Topic archived. No new replies allowed.