-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmidterm.cpp
More file actions
687 lines (594 loc) · 22.5 KB
/
midterm.cpp
File metadata and controls
687 lines (594 loc) · 22.5 KB
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
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
// File: midterm.cpp
// Console Gradebook with View, Add, Remove, Edit, and Student Averages
#include <bits/stdc++.h> //
using namespace std; // Standard library
// ------------------- DATA STRUCTURES -------------------
struct Record { // Student Record Structure
string firstName; // Desclares first name string
string lastName; // Desclares last name string
string assignment; // Desclares assignment string
int grade; // Desclares grade integer
};
string to_lower_str(const string &s) { // Convert string to lowercase
string out = s;
transform(out.begin(), out.end(), out.begin(),
[](unsigned char c){ return std::tolower(c); });
return out; // Return lowercase string
}
void pause_enter() { // Waits for user to interact
cout << "\nPress Enter to continue...";
string tmp;
getline(cin, tmp);
}
// -------------------------------------------------
// ------------------- MAIN MENU -------------------
void show_main_menu() { // Simple welcome menu
cout << "────── ⋆⋅☆⋅⋆ ──────\n";
cout << " Welcome!\n";
cout << "a. View Gradebook\n";
cout << "b. Edit Gradebook\n";
cout << "c. Save Data and Close\n";
cout << "Choose an option: ";
}
// -------------------------------------------------
// ----------------- VIEW FUNCTIONS -----------------
void show_full_gradebook(const vector<Record>& book) {
cout << "\n # | Name | Assignment | Grade\n";
cout << "-------------------------------------------\n";
if (book.empty()) { // If there's no Gradebook file
cout << "There are no records yet!\n\n";
while (true) {
cout << "a. Go Back\n";
cout << "Choose an option: ";
string choice;
if (!getline(cin, choice)) return;
choice = to_lower_str(choice);
if (!choice.empty() && choice[0] == 'a')// Checks for 'a' input
return; // Goes back to menu
cout << "Invalid choice. Please enter 'a' to go back.\n";
}
} else {
int i = 1;
for (const auto& r : book) {
string fullName = r.firstName + " " + r.lastName;
cout << setw(2) << i << " | "
<< left << setw(13) << fullName << " | "
<< left << setw(14) << r.assignment << " | "
<< right << setw(5) << r.grade << "\n";
++i;
}
cout << "\n";
pause_enter(); // Waits for user to interact
}
}
// -------------------------------------------------
// ------------------- STUDENT AVERAGES -------------------
void show_student_averages(const vector<Record>& book) { // Displays the average grade for each student in the gradebook
if (book.empty()) {
cout << "\nThere are no records yet!\n\n";
pause_enter(); // Waits for user to interact
return; // Goes back to menu
}
// Map each student to their list of grades
map<string, vector<int>> studentGrades;
for (const auto& r : book) {
string fullName = r.lastName + ", " + r.firstName; // What the fuck am I doing
studentGrades[fullName].push_back(r.grade);
}
// Calculate average of each student
vector<pair<string, double>> averages;
for (const auto& [name, grades] : studentGrades) {
double sum = accumulate(grades.begin(), grades.end(), 0.0);
double avg = sum / grades.size();
averages.push_back({name, avg});
}
sort(averages.begin(), averages.end(),
[](auto &a, auto &b){ return a.first < b.first; });
// Header
cout << "\n # | Name | Grade Average\n";
cout << "-------------------------------------------\n";
// Print each student average
int i = 1;
for (const auto& [name, avg] : averages) {
cout << setw(2) << i << " | "
<< left << setw(22) << name << " | "
<< right << fixed << setprecision(2) << avg << "\n";
++i;
}
cout << "\n";
pause_enter(); // Wait for user to interact
}
// ------------------- VIEW MENU -------------------
void view_gradebook(const vector<Record>& book) {
while (true) {
cout << "\nView Menu:\n";
cout << "a. View Gradebook\n";
cout << "b. View Student Averages\n";
cout << "c. Search Student\n";
cout << "d. Go Back\n";
cout << "Choose an option: ";
string choice;
if (!getline(cin, choice)) return;
choice = to_lower_str(choice);
if (choice.empty()) continue;
if (choice[0] == 'a') {
// --- Regular Gradebook View ---
cout << "\n # | Name | Assignment | Grade\n";
cout << "-------------------------------------------\n";
if (book.empty()) {
cout << "There are no records yet!\n\n";
while (true) {
cout << "a. Go Back\n";
cout << "Choose an option: ";
string ch;
if (!getline(cin, ch)) return;
ch = to_lower_str(ch);
if (!ch.empty() && ch[0] == 'a')
break;
cout << "Invalid choice. Please enter 'a' to go back.\n";
}
} else {
int i = 1;
for (const auto& r : book) {
string fullName = r.firstName + " " + r.lastName;
cout << setw(2) << i << " | "
<< left << setw(13) << fullName << " | "
<< left << setw(14) << r.assignment << " | "
<< right << setw(5) << r.grade << "\n";
++i;
}
cout << "\n";
pause_enter();
} // ----------------------
}
else if (choice[0] == 'b') {
// --- Student Averages ---
if (book.empty()) {
cout << "\nThere are no records yet!\n";
pause_enter();
continue;
}
map<string, pair<int, int>> avgMap; // key=name, val={sum, count}
for (const auto& r : book) {
string full = r.lastName + ", " + r.firstName;
avgMap[full].first += r.grade;
avgMap[full].second += 1;
}
// Header
cout << "\n # | Name | Grade Average\n";
cout << "-----------------------------------------\n";
int i = 1;
for (auto& [name, data] : avgMap) {
double avg = static_cast<double>(data.first) / data.second;
cout << setw(2) << i << " | "
<< left << setw(20) << name << " | "
<< fixed << setprecision(2) << avg << "\n";
i++;
}
pause_enter(); // Wait for user to interact
}
else if (choice[0] == 'c') {
// --- Search Student ---
if (book.empty()) {
cout << "\nThere are no records yet!\n";
pause_enter();
continue;
}
// Collect unique names (case-insensitive)
map<string, string> nameMap; // lower->display
for (const auto& r : book) {
string key = to_lower_str(r.lastName + ", " + r.firstName);
string display = r.lastName + ", " + r.firstName;
nameMap[key] = display;
}
vector<string> sortedNames;
for (auto& [k, v] : nameMap)
sortedNames.push_back(v);
cout << "\n # | Name\n";
cout << "----------------\n";
for (int i = 0; i < (int)sortedNames.size(); ++i) {
cout << setw(2) << i + 1 << " | " << sortedNames[i] << "\n";
}
cout << "\nSearch for a student (type part of the name, or #): ";
string query;
getline(cin, query);
query = to_lower_str(query);
vector<string> matches;
// If user entered a number, use that directly
if (!query.empty() && isdigit(query[0])) {
int num = stoi(query);
if (num >= 1 && num <= (int)sortedNames.size())
matches.push_back(sortedNames[num - 1]);
} else {
// Otherwise, find partial matches
for (const auto& name : sortedNames) {
if (to_lower_str(name).find(query) != string::npos)
matches.push_back(name);
}
}
if (matches.empty()) {
cout << "\nNo matching students found.\n";
pause_enter();
continue;
}
// If more than one match, list them
if (matches.size() > 1) {
cout << "\nMatching Students:\n";
for (int i = 0; i < (int)matches.size(); ++i) {
cout << setw(2) << i + 1 << " | " << matches[i] << "\n";
}
cout << "\nType full name or # to view details: ";
string sel;
getline(cin, sel);
sel = to_lower_str(sel);
string chosen;
if (!sel.empty() && isdigit(sel[0])) {
int num = stoi(sel);
if (num >= 1 && num <= (int)matches.size())
chosen = matches[num - 1];
} else {
for (const auto& n : matches)
if (to_lower_str(n) == sel) chosen = n;
}
if (chosen.empty()) {
cout << "\nInvalid selection.\n";
pause_enter();
continue;
}
query = to_lower_str(chosen);
} else {
query = to_lower_str(matches[0]);
}
// Show details for the chosen student
vector<Record> studentRecords;
int total = 0;
for (const auto& r : book) {
string full = to_lower_str(r.lastName + ", " + r.firstName);
if (full == query) {
studentRecords.push_back(r);
total += r.grade;
}
}
cout << "\nStudent Name: " << nameMap[query] << "\n";
if (!studentRecords.empty()) {
double avg = (double)total / studentRecords.size();
cout << "Average Grade: " << fixed << setprecision(2) << avg << "\n";
cout << "\n # | Assignment | Grade\n";
cout << "------------------------\n";
for (int i = 0; i < (int)studentRecords.size(); ++i) {
cout << setw(2) << i + 1 << " | "
<< left << setw(14) << studentRecords[i].assignment << " | "
<< right << setw(3) << studentRecords[i].grade << "\n";
}
} else {
cout << "No records found for this student.\n";
}
cout << "\na. Go Back\n";
string back;
while (true) {
cout << "Choose an option: ";
getline(cin, back);
back = to_lower_str(back);
if (!back.empty() && back[0] == 'a') break;
cout << "Invalid input. Type 'a' to go back.\n";
}
}
else if (choice[0] == 'd') {
return; // Go back to main menu
}
else {
cout << "Invalid option. Please choose a, b, c, or d.\n";
}
}
}
// -------------------------------------------------
// ------------------- EDIT FUNCTIONS -------------------
void add_student(vector<Record>& book) {
Record r;
string gradeInput;
cout << "\nStudent Name: ";
getline(cin, r.firstName);
cout << "Student Lastname: ";
getline(cin, r.lastName);
cout << "Assignment Name: ";
getline(cin, r.assignment);
while (true) {
cout << "Student Grade: ";
getline(cin, gradeInput);
try {
r.grade = stoi(gradeInput);
if (r.grade < 0 || r.grade > 100)
throw out_of_range("Grade out of range");
break;
} catch (...) {
cout << "Invalid grade. Please enter a number between 0 and 100.\n";
}
}
book.push_back(r);
cout << "\nStudent added successfully!\n";
pause_enter();
}
bool matches_query(const Record& r, const string& query) {
string first = to_lower_str(r.firstName);
string last = to_lower_str(r.lastName);
string full = to_lower_str(r.firstName + " " + r.lastName);
return first.find(query) != string::npos ||
last.find(query) != string::npos ||
full.find(query) != string::npos;
}
void remove_student(vector<Record>& book) {
if (book.empty()) {
cout << "\nThere are no students to remove.\n";
pause_enter();
return; // Go back
}
cout << "\nSearch a Student to Remove: ";
string query;
getline(cin, query);
query = to_lower_str(query);
vector<int> matches;
for (int i = 0; i < (int)book.size(); ++i) {
if (matches_query(book[i], query))
matches.push_back(i);
}
if (matches.empty()) {
cout << "\nNo matching students found.\n";
pause_enter();
return; // Go back
}
cout << "\n────── ⋆⋅☆⋅⋆ ──────\n";
cout << " Matching Students:\n";
for (int i = 0; i < (int)matches.size(); ++i) {
const auto& r = book[matches[i]];
cout << " - " << r.firstName << " " << r.lastName << "\n";
}
while (true) {
cout << "\nWhich student do you wish to remove? (Type their full name)\n";
cout << "Type 'Menu' to cancel\n";
cout << "Input: ";
string input;
getline(cin, input);
if (input.empty()) continue;
string lowerInput = to_lower_str(input);
if (lowerInput == "menu") {
cout << "\nReturning to main menu...\n";
pause_enter();
return; // Go back
}
bool found = false;
for (auto it = book.begin(); it != book.end(); ++it) {
string full = to_lower_str(it->firstName + " " + it->lastName);
if (full == lowerInput) {
book.erase(it);
cout << "\nStudent Removed Successfully!\n";
pause_enter();
found = true;
break;
}
}
if (found)
return;
else
cout << "No exact match found for that name. Try again.\n";
}
}
// ------------------- NEW: EDIT STUDENT -------------------
void edit_student(vector<Record>& book) {
if (book.empty()) {
cout << "\nThere are no students to edit.\n";
pause_enter();
return; // Go back
}
// Build sorted list of unique names
map<string, vector<int>> studentMap;
for (int i = 0; i < (int)book.size(); ++i) {
string full = book[i].lastName + ", " + book[i].firstName;
studentMap[full].push_back(i);
}
vector<string> sortedNames;
for (auto &p : studentMap)
sortedNames.push_back(p.first);
cout << "\n────── ⋆⋅☆⋅⋆ ──────\n";
cout << "Students:\n";
int idx = 1;
for (auto &n : sortedNames)
cout << setw(2) << idx++ << ". " << n << "\n";
cout << "\n────── ⋆⋅☆⋅⋆ ──────\n";
cout << "What student do you wish to edit? (Type full name or #): ";
string input;
getline(cin, input);
input = to_lower_str(input);
int chosenIndex = -1;
// Check if numeric
if (!input.empty() && isdigit(input[0])) {
try {
int num = stoi(input);
if (num >= 1 && num <= (int)sortedNames.size()) {
string chosenName = sortedNames[num - 1];
chosenIndex = studentMap[chosenName][0]; // edit first record
}
} catch (...) {}
} else {
// Match by full name
for (auto &p : studentMap) {
if (to_lower_str(p.first) == input) {
chosenIndex = p.second[0];
break;
}
}
}
if (chosenIndex == -1) {
cout << "\nNo matching student found.\n";
pause_enter();
return; // Go back
}
// Show student's record
Record &r = book[chosenIndex];
cout << "\nStudent Record:\n";
cout << " # | Name | Assignment | Grade\n";
cout << "-------------------------------------------\n";
cout << " 1 | " << left << setw(13) << (r.firstName + " " + r.lastName)
<< " | " << left << setw(14) << r.assignment
<< " | " << right << setw(5) << r.grade << "\n";
cout << "\nEnter the line number you want to edit (1): ";
string lineInput;
getline(cin, lineInput); // (for future multi-line support)
cout << "\n";
while (true) {
cout << "────── ⋆⋅☆⋅⋆ ──────\n";
cout << "a. Change Name\n";
cout << "b. Change Assignment\n";
cout << "c. Change Grade\n";
cout << "d. Done\n";
cout << "Choose an option: ";
string choice;
getline(cin, choice);
choice = to_lower_str(choice);
if (choice.empty()) continue;
if (choice[0] == 'a') {
cout << "Student Name: ";
getline(cin, r.firstName);
cout << "Student Lastname: ";
getline(cin, r.lastName);
cout << "\nName updated!\n\n";
} else if (choice[0] == 'b') {
cout << "Assignment: ";
getline(cin, r.assignment);
cout << "\nAssignment updated!\n\n";
} else if (choice[0] == 'c') {
while (true) {
cout << "Grade: ";
string gradeInput;
getline(cin, gradeInput);
try {
int g = stoi(gradeInput);
if (g < 0 || g > 100)
throw out_of_range("Grade out of range");
r.grade = g;
cout << "\nGrade updated!\n\n";
break;
} catch (...) {
cout << "Invalid grade. Please enter 0–100.\n";
}
}
} else if (choice[0] == 'd') {
cout << "\nUpdated Record:\n";
cout << " # | Name | Assignment | Grade\n";
cout << "-------------------------------------------\n";
cout << " 1 | " << left << setw(13) << (r.firstName + " " + r.lastName)
<< " | " << left << setw(14) << r.assignment
<< " | " << right << setw(5) << r.grade << "\n";
pause_enter();
return; // Done editing
} else {
cout << "Invalid choice. Please enter a, b, c, or d.\n";
}
}
}
// ------------------- EDIT MENU -------------------
void edit_gradebook(vector<Record>& book) {
while (true) {
cout << "\n────── ⋆⋅☆⋅⋆ ──────\n";
cout << "Edit Gradebook Menu:\n";
cout << "a. Add Student\n";
cout << "b. Remove Student\n";
cout << "c. Edit Student\n";
cout << "d. Go Back\n";
cout << "Choose an option: ";
string choice;
if (!getline(cin, choice)) return;
choice = to_lower_str(choice);
if (choice.empty()) continue;
switch (choice[0]) {
case 'a':
add_student(book);
break;
case 'b':
remove_student(book);
break;
case 'c':
edit_student(book);
break;
case 'd':
return; // Go back to main menu
default:
cout << "Invalid option. Please choose a, b, c, or d.\n";
}
}
}
// ------------------- FILE SAVE/LOAD -------------------
void save_gradebook(const vector<Record>& book) {
ofstream out("gradebook.txt");
if (!out) {
cerr << "Error: Could not open gradebook.txt for writing.\n";
return;
}
for (const auto& r : book) {
out << r.firstName << ","
<< r.lastName << ","
<< r.assignment << ","
<< r.grade << "\n";
}
out.close();
}
void load_gradebook(vector<Record>& book) {
ifstream in("gradebook.txt");
if (!in) {
// No file yet, start fresh
return;
}
book.clear();
string line;
while (getline(in, line)) {
if (line.empty()) continue;
stringstream ss(line);
Record r;
string gradeStr;
getline(ss, r.firstName, ',');
getline(ss, r.lastName, ',');
getline(ss, r.assignment, ',');
getline(ss, gradeStr, ',');
try {
r.grade = stoi(gradeStr);
} catch (...) {
continue; // skip malformed lines
}
book.push_back(r);
}
in.close();
}
// -------------------------------------------------
// ------------------- MAIN -------------------
int main() {
vector<Record> gradebook;
// Container to store all student records
// Load data at startup
load_gradebook(gradebook);
cout << "Gradebook loaded successfully. (" << gradebook.size() << " records)\n\n";
while (true) { // Main program loop
show_main_menu(); // Display main menu
string input;
if (!getline(cin, input)) break; //Exit on EOF
input = to_lower_str(input); //Convert input to lowercase
if (input.empty()) { // Handle empty input
cout << "No input received. Please choose a, b, or c.\n\n";
continue; // Prompt again
}
char choice = input[0]; // Get first character of input
if (choice == 'a') {
view_gradebook(gradebook); // View gradebook option
cout << "\n";
} else if (choice == 'b') {
edit_gradebook(gradebook); // Edit gradebook option
cout << "\n";
} else if (choice == 'c') { // Save and close option
cout << "Saving gradebook and closing program...\n";
save_gradebook(gradebook);
cout << "Data saved to gradebook.txt\n";
break;
} else { // Invalid input message
cout << "Invalid option. Please choose a, b, or c.\n\n";
}
}
return 0; // End of program FINALLY
} // What if I just jump brah