-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathmain.cpp
335 lines (277 loc) · 11.2 KB
/
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
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
//*****************************************************************************************************
//
// This program reads in a file of student data and instantiates a doubly linked list to store
// the student objects. It then displays a menu of options to perform its methods.
//
// Other files required:
// 1. DLList.h - header file for the DLList class
// 2. student.h - header file for the Student struct
// 3. studentFile.txt - file containing student data of 61 students
//
//*****************************************************************************************************
#include "DLList.h"
#include "student.h"
#include <cctype>
#include <fstream>
#include <iomanip>
#include <iostream>
using namespace std;
//*****************************************************************************************************
char getChoice();
void process(DLList<Student> &studentList);
void buildList(DLList<Student> &studentList);
void displayStudent(DLList<Student> &studentList);
void displayStudentRev(DLList<Student> &studentList);
void countStudents(DLList<Student> &studentList);
void addNewStudent(DLList<Student> &studentList);
void removeStudent(DLList<Student> &studentList);
void findStudent(DLList<Student> &studentList);
//*****************************************************************************************************
int main() {
DLList<Student> studentList;
buildList(studentList);
process(studentList);
return 0;
}
//*****************************************************************************************************
char getChoice() {
char choice;
bool valid;
cout << "============= MENU =============\n"
<< "A: Add a new student\n"
<< "F: Find a student\n"
<< "R: Remove a student\n"
<< "C: Count the students\n"
<< "V: Reverse display the students\n"
<< "D: Display the students\n"
<< "Q: Quit\n"
<< "================================\n"
<< "Enter your choice: ";
do {
cin >> choice;
choice = toupper(choice);
switch (choice) {
case 'A':
case 'F':
case 'R':
case 'C':
case 'V':
case 'D':
case 'Q':
valid = true;
break;
default:
valid = false;
cerr << "\n\aInvalid choice\n"
<< "Please try again: ";
break;
}
} while (!valid);
return choice;
}
//*****************************************************************************************************
void process(DLList<Student> &studentList) {
char choice;
do {
choice = getChoice();
switch (choice) {
case 'A':
addNewStudent(studentList);
break;
case 'F':
findStudent(studentList);
break;
case 'R':
removeStudent(studentList);
break;
case 'C':
countStudents(studentList);
break;
case 'V':
displayStudentRev(studentList);
break;
case 'D':
displayStudent(studentList);
break;
case 'Q':
break;
}
} while (choice != 'Q');
}
//*****************************************************************************************************
void buildList(DLList<Student> &studentList) {
Student student;
ifstream studentData;
studentData.open("studentFile.txt");
while (studentData >> student.id) {
studentData.ignore();
studentData.getline(student.name, 50);
studentData.getline(student.cityState, 50);
studentData >> student.phone >> student.gender >>
student.year >> student.credits >> student.gpa >> student.major;
studentList.insert(student);
}
studentData.close();
}
//*****************************************************************************************************
void displayStudent(DLList<Student> &studentList) {
cout << "\n\n"
<< setw(32) << right << "STUDENT LIST\n"
<< "==================================================\n"
<< setw(11) << left << "ID" << setw(25) << "NAME" << setw(10) << "MAJOR" << "GPA\n"
<< "==================================================" << endl;
studentList.display();
}
//*****************************************************************************************************
void displayStudentRev(DLList<Student> &studentList) {
cout << "\n\n"
<< setw(36) << right << "REVERSE STUDENT LIST\n"
<< "==================================================\n"
<< setw(11) << left << "ID" << setw(25) << "NAME" << setw(10) << "MAJOR" << "GPA\n"
<< "==================================================" << endl;
studentList.displayReverse();
}
//*****************************************************************************************************
void countStudents(DLList<Student> &studentList) {
cout << "\n\nNumber of students: " << studentList.getNumValues() << "\n"
<< endl;
}
//*****************************************************************************************************
void addNewStudent(DLList<Student> &studentList) {
Student student;
cout << "\n\nID: ";
cin >> student.id;
cout << "Name (e.g. Smith, John A): ";
cin.ignore();
cin.getline(student.name, 50);
cout << "City, State (e.g. San Diego, California): ";
cin.ignore();
cin.getline(student.cityState, 50);
cout << "Phone Number (e.g. 3145551212): ";
cin >> student.phone;
cout << "Gender (M/F): ";
cin >> student.gender;
cout << "Year (1-5): ";
cin >> student.year;
cout << "Credits (0-200): ";
cin >> student.credits;
cout << "Gpa (0.00-4.00): ";
cin >> student.gpa;
cout << "Major (e.g. MATH): ";
cin >> student.major;
studentList.insert(student);
cout << "\n\n"
<< setw(31) << right << "STUDENT ADDED\n"
<< "==================================================\n"
<< setw(11) << left << "ID" << setw(25) << "NAME" << setw(10) << "MAJOR" << "GPA\n"
<< "==================================================\n"
<< student << endl;
}
//*****************************************************************************************************
void removeStudent(DLList<Student> &studentList) {
Student student;
cout << "\n\nEnter the student's ID to remove: ";
cin >> student.id;
if (studentList.remove(student))
cout << "\n\n"
<< setw(32) << right << "STUDENT REMOVED\n"
<< "==================================================\n"
<< setw(11) << left << "ID" << setw(25) << "NAME" << setw(10) << "MAJOR" << "GPA\n"
<< "==================================================\n"
<< student << endl;
else
cerr << "\n\nStudent not found\n\n";
}
//*****************************************************************************************************
void findStudent(DLList<Student> &studentList) {
Student student;
cout << "\n\nEnter the student's ID to find: ";
cin >> student.id;
if (studentList.retrieve(student))
cout << "\n\n"
<< setw(33) << right << "STUDENT FOUND\n"
<< "==================================================\n"
<< setw(11) << left << "ID" << setw(25) << "NAME" << setw(10) << "MAJOR" << "GPA\n"
<< "==================================================\n"
<< student << endl;
else
cerr << "\n\nStudent not found\n\n";
}
//*****************************************************************************************************
/*
============= MENU =============
A: Add a new student
F: Find a student
R: Remove a student
C: Count the students
V: Reverse display the students
D: Display the students
Q: Quit
================================
Enter your choice: v
REVERSE STUDENT LIST
==================================================
ID NAME MAJOR GPA
==================================================
32631 Freud, JR, Fred E PSYC 1.85
32598 Xerxes, Art I GREE 3.25
31631 Aristotle, Alice A PHIL 3.1
30878 Lewis, Clark N GEOG 3.37
30749 Mendelssohn, Mozart W MUSC 2.87
30655 Angelo, Mike L ART 3.74
30381 Elba, Able M SPEE 3.4
30280 Dewey, Johanna A EDUC 3.83
30268 Newmann, Alfred E EDUC 0.99
29583 Yewliss, Cal C MATH 2.99
28658 Cicero, Marsha LATI 2.87
27503 Fahrenheit, Felicia O CHEM 3.85
26316 Custer, General G HIST 1.95
25831 Santamaria, Nina P HIST 1.77
25377 Porgy, Bess N MUSI 2.78
24237 Euler, Lennie L MATH 3.83
23750 Vespucci, Vera D GEOG 2.29
23544 Gestalt, Gloria G PSYC 2.48
23497 Fault, Paige D CPSC 2.95
23314 Macdonald, Ronald B CPSC 2.99
22447 Zylstra, Zelda A ENGL 1.95
22277 Principal, Pamela P EDUC 1.75
21144 Pasteur, Louise A BIOL 3.1
20991 Augusta, Ada B CPSC 3.83
20454 Chicita, Juanita A BIOL 2.66
19918 Virus, Vera W CPSC 3.25
19077 Medes, Archie L ENGR 3.1
18264 Lucky, Lucy L HIST 2.29
18213 Marx, Karl Z ECON 2.75
17424 Nakamura, Toky O SOCI 1.95
17376 Scrooge, Ebenezer T SOCI 3.25
16622 Issacson, Esau B RELI 2.98
16540 Weerd, Dewey L PHIL 2.99
16183 Kuts, Cole FOOD 3.98
15889 Gazelle, Gwendolyn D PE 2.78
15802 Pascal, Blaze R CPSC 1.98
15755 VandenVander, Vanessa V HIST 3.74
15671 Rembrandt, Roberta E ART 2.2
15052 Einstein, Alfred M ENGR 2.78
14815 Tchaikovsky, Wolfgang A MUSC 2.75
14674 Rockne, Newton K PE 1.98
13511 Pitt, Stew GNED 0.21
11951 Mouse, Michael E EDUC 1.99
11749 Issacson, Jacob A RELI 2.99
11688 Kronecker, Leo P MATH 2.75
10304 Deutsch, Sprechen Z GERM 3.05
10236 Andrews, Peter J CPSC 2.78
9743 Johnson, James L ENGR 3.15
9463 Hochschule, Hortense C EDUC 2.7
7885 Fibonacci, Leonard O MATH 3.25
7844 Aardvark, Anthony A ENGR 2.79
7448 Roosevelt, Rose Y POLS 2.95
7107 Shoemaker, Imelda M POLS 3.15
5873 Psycho, II, Prunella E PSYC 2.99
5710 Busch, Arch E ENGR 2.75
5430 Nightingale, Florence K NURS 3.15
5316 GotoDijkstra, Edgar G CPSC 4
4777 Gauss, Carl F MATH 4
4559 Shyster, Samuel D SOCI 1.95
4454 Atanasoff, Eniac C CPSC 1.88
3930 Leibniz, Gottfried W MATH 1.95
*/