-
Notifications
You must be signed in to change notification settings - Fork 0
/
project12.cpp
230 lines (194 loc) · 7.61 KB
/
project12.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
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
/* Name: Keita Nonaka
Project 12
This program is for using files.
There are 5 commands to handle person data
*/
#include <iostream>
#include <iomanip>
#include <fstream>
#include "Person.h"
using namespace std;
char enterChoice();
void createTextFile(fstream&);
void updateRecord(fstream&);
void newRecord(fstream&);
void deleteRecord(fstream&);
void outputLine(ostream&, const Person &);
int getidNumber(const char * const);
int main() {
char ans;
cout<<"Do you want to create an empty file (y/n)? ";
cin >> ans;
if (ans=='y' || ans=='Y'){
//create nameage.dat to hold 100 possible person objects
ofstream outPerson("nameage.dat", ios::out | ios::binary);
// exit program if ofstream could not open file
if (!outPerson){
cerr << "File could not be opened." << endl;
exit(1);
}
Person blankPerson; // constructor zeros out each data member
// output 100 blank records to file
for (int i = 0; i < 100; ++i) {
outPerson.write(reinterpret_cast< const char * >(&blankPerson), sizeof(Person));
}
outPerson.close();
}
fstream inOutPerson("nameage.dat", ios::in | ios::out | ios::binary);
char choice = '\0';
while ((choice = enterChoice()) != 'E' && choice != 'e') {
switch (choice) {
case 'P':
case 'p':// print/display data
createTextFile(inOutPerson);
break;
case 'U':
case 'u':// update record
updateRecord(inOutPerson);
break;
case 'A':
case 'a':// add a new record
newRecord(inOutPerson);
break;
case 'D':
case 'd':// delete existing record
deleteRecord(inOutPerson);
break;
default: // display error if user does not select valid choice
cerr << "Incorrect choice" << endl;
break;
}
inOutPerson.clear(); // reset
}
}
// enable user to input menu choice
char enterChoice() {
// display available options
cout << "\nEnter your choice" << endl
<< "P - Print/Display a list of people" << endl
<< "U - Update a person" << endl
<< "A - Add a new person" << endl
<< "D - Delete a person" << endl
<< "E - End program\n> ";
char menuChoice;
cin >> menuChoice; // input menu selection from user
return menuChoice;
}
// create formatted text file for printing (uses nameage.dat as input)
void createTextFile(fstream &readFromFile) {
// create text file
ofstream outPrintFile("print.txt", ios::out);
// exit program if ofstream cannot create file
if (!outPrintFile) {
cout << "File could not be created." << endl;
exit(1);
}
//display header information to file and to cout
cout << left << setw( 10 ) << "id" << setw( 16 ) << "Last name" << setw( 11 ) << "first name" << setw( 10 ) << right << "age" << endl;
outPrintFile << left << setw( 10 ) << "id" << setw( 16 ) << "Last name" << setw( 11 ) << "first name" << setw( 10 ) << right << "age" << endl;
// set file-position pointer to beginning of readFromFile
readFromFile.seekg(0);
// read first record from record file
Person person;
readFromFile.read(reinterpret_cast<char *>(&person), sizeof(Person));
// copy all records from record file into text file
while (!readFromFile.eof()) { // write single record to text file
if (person.getId() != 0) {
outputLine(cout,person);
outputLine(outPrintFile,person);
}
// read next record from record file
readFromFile.read(reinterpret_cast<char *>(&person), sizeof(Person));
}
}
// update age in record
void updateRecord( fstream &updateFile ) {
// obtain number of idNumber to update
int idNumber = getidNumber("Enter Id Number to update");
Person person;
// move file-position pointer to correct record in file
updateFile.seekg((idNumber - 1) * sizeof(Person));
// read first record from file
updateFile.read(reinterpret_cast<char *>(&person), sizeof(Person));
//update record
if(person.getId() != 0) {
outputLine(cout, person); // display the record
// request user to update age
cout << "\nEnter new age:" << endl;
int age;
cin >> age;
// update age
person.setAge(age);
// move file-position pointer to correct record in file
updateFile.seekp((idNumber - 1) * sizeof(Person));
// write updated record over old record in file
updateFile.write(reinterpret_cast<const char *>(&person), sizeof(Person));
} else{ // display error if idNumber does not exist
cerr << "Id # " << idNumber << " has no information." << endl;
}
}
// create and insert record
void newRecord(fstream &insertInFile) {
// obtain number of idNumber to create
int idNumber = getidNumber("Enter new Id Number");
Person person;
// move file-position pointer to correct record in file
insertInFile.seekg((idNumber-1) * sizeof(Person));
// read record from file
insertInFile.read(reinterpret_cast<char *>(&person), sizeof(Person));
// create record, if record does not previously exist
if (person.getId() == 0) {
string lastName;
string firstName;
int age;
// user enters last name, first name and age
cout << "Enter lastname, firstname, age\n> ";
cin >> setw( 15 ) >> lastName;
cin >> setw( 10 ) >> firstName;
cin >> age;
person.setId(idNumber);
person.setFirstName(firstName);
person.setLastName(lastName);
person.setAge(age);
// insert record in file
insertInFile.seekp((idNumber-1) * sizeof(Person));
insertInFile.write(reinterpret_cast<const char *>(&person), sizeof(Person));
} else { // display error if idNumber already exists
cerr << "Id # " << idNumber << " already contains information." << endl;
}
}
// delete an existing record
void deleteRecord( fstream &deleteFromFile ) {
// obtain number of idNumber to delete
int idNumber = getidNumber("Enter Id Number to delete");
Person person;
// move file-position pointer to correct record in file
deleteFromFile.seekg((idNumber-1) * sizeof(Person));
// read record from file
deleteFromFile.read(reinterpret_cast<char *>(&person), sizeof(Person));
// delete record, if record exists in file
if(person.getId() != 0) {
Person blankPerson; // create blank record
// move file-position pointer to correct record in file
deleteFromFile.seekp((idNumber-1) * sizeof(Person));
// replace existing record with blank record
deleteFromFile.write(reinterpret_cast<char *>(&blankPerson), sizeof(Person));
cout << "Id #" << idNumber << " deleted.\n";
} else{ // display error if idNumber does not exist
cerr << "Id #" << idNumber << " is empty" << endl;
}
}
// display single record
void outputLine( ostream &output, const Person &record ) {
output << left << setw( 10 ) << record.getId() << setw( 16 ) << record.getLastName()
<< setw( 11 ) << record.getFirstName() << setw( 10 ) << right << record.getAge() << endl;
}
// obtain idNumber-number value from user
int getidNumber(const char * const prompt) {
int idNumber;
do {
cout << prompt << " (1-100): ";
cin >> idNumber;
} while (idNumber < 1 || idNumber > 100);
return idNumber;
}