-
Notifications
You must be signed in to change notification settings - Fork 0
/
Main.cpp
153 lines (144 loc) · 5.58 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
#include"Student.h"
#include<vector>
#include <fstream>
#include<iostream>
#include <algorithm>
using namespace std;
int main() {
vector<Student> students;
int choice;
do {
std::cout << "Student Management System\n";
std::cout << "1. Add Student\n";
std::cout << "2. Display Student List\n";
std::cout << "3. Edit Student Information\n";
std::cout << "4. Remove Student Information\n";
std::cout << "5. Search Student Information\n";
std::cout << "6. Save Students to File\n";
std::cout << "7. Load Students from File\n";
std::cout << "8. Exit\n";
std::cout << "Enter your choice: ";
std::cin >> choice;
switch (choice) {
case 1: {
Student newStudent;
newStudent.inputInformation();
students.push_back(newStudent);
std::cout << "Student added successfully!\n";
break;
}
case 2: {
if (students.empty()) {
std::cout << "No students found!\n";
} else {
std::cout << "Student List:\n";
for (const Student& student : students) {
student.displayInformation();
std::cout << endl;
}
}
break;
}
case 3: {
if (students.empty()) {
std::cout << "No students found!\n";
} else {
int studentID;
std::cout << "Enter the ID of the student to edit: ";
std::cin >> studentID;
bool found = false;
for (Student& student : students) {
if (student.getStudentID() == studentID) {
student.editInformation();
std::cout << "Student information has been updated.\n";
found = true;
break;
}
}
if (!found) {
std::cout << "Student not found.\n";
}
}
break;
}
case 4: {
if (students.empty()) {
std::cout << "No students found!\n";
} else {
int studentID;
std::cout << "Enter the ID of the student to remove: ";
std::cin >> studentID;
auto it = find_if(students.begin(), students.end(), [studentID](const Student& student) {
return student.getStudentID() == studentID;
});
if (it != students.end()) {
students.erase(it);
std::cout << "Student information has been removed.\n";
} else {
std::cout << "Student not found.\n";
}
}
break;
}
case 5: {
if (students.empty()) {
std::cout << "No students found!\n";
} else {
string studentName;
std::cout << "Enter the name of the student to search: ";
std::cin.ignore();
std::getline(std::cin, studentName);
bool found = false;
for (const Student& student : students) {
if (student.getStudentName() == studentName) {
student.displayInformation();
std::cout <<endl;
found = true;
break;
}
}
if (!found) {
std::cout << "Student not found.\n";
}
}
break;
}
case 6: {
std::ofstream outputFile("students.txt");
if (outputFile.is_open()) {
for (const Student& student : students) {
student.writeFile(outputFile);
}
outputFile.close();
std::cout << "Students saved to file successfully!\n";
} else {
std::cout << "Failed to open file for writing!\n";
}
break;
}
case 7: {
std::ifstream inputFile("students.txt");
if (inputFile.is_open()) {
students.clear(); // Clear existing student data
while (!inputFile.eof()) {
Student student;
student.readFile(inputFile);
students.push_back(student);
}
inputFile.close();
std::cout << "Students loaded from file successfully!\n";
} else {
std::cout << "Failed to open file for reading!\n";
}
break;
}
case 8:
std::cout << "Exiting the program.\n";
break;
default:
std::cout << "Invalid choice! Please try again.\n";
}
std::cout <<endl;
} while (choice != 8);
return 0;
}