-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
250 lines (213 loc) · 6.75 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
#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <algorithm>
using namespace std;
// Structure to hold student information
struct Student {
string name;
string regNumber;
int age;
char gender;
string course;
};
// Function prototypes
void readStudents(vector<Student>& students);
void displayStudents(const vector<Student>& students);
void addStudent(vector<Student>& students, vector<string>& courses);
void saveStudents(const vector<Student>& students);
void readCourses(vector<string>& courses);
void displayCourses(const vector<string>& courses);
void addCourse(vector<string>& courses);
void saveCourses(const vector<string>& courses);
void addSubjectsToCourse(vector<string>& courses);
int main() {
vector<Student> students;
vector<string> courses;
// Read initial data from files
readStudents(students);
readCourses(courses);
int choice;
do {
cout << "\n===== Student Management System Menu =====\n";
cout << "1. Display all students\n";
cout << "2. Add a student\n";
cout << "3. Display all courses\n";
cout << "4. Add a course\n";
cout << "5. Add subjects to a course\n";
cout << "6. Save and exit\n";
cout << "Enter your choice: ";
cin >> choice;
cin.ignore();
switch(choice) {
case 1:
displayStudents(students);
break;
case 2:
addStudent(students, courses);
break;
case 3:
displayCourses(courses);
break;
case 4:
addCourse(courses);
break;
case 5:
addSubjectsToCourse(courses);
break;
case 6:
saveStudents(students);
saveCourses(courses);
cout << "Data saved. Exiting program.\n";
break;
default:
cout << "Invalid choice. Please enter a number between 1 and 6.\n";
}
} while(choice != 6);
return 0;
}
void readStudents(vector<Student>& students) {
ifstream file("students.txt");
if (!file) {
cout << "Error opening students.txt. Exiting...\n";
exit(1);
}
students.clear();
string line;
while (getline(file, line)) {
stringstream ss(line);
Student student;
ss >> student.name >> student.regNumber >> student.age >> student.gender >> student.course;
students.push_back(student);
}
file.close();
}
void displayStudents(const vector<Student>& students) {
if (students.empty()) {
cout << "No students registered.\n";
return;
}
cout << "\n===== List of Students =====\n";
for (const auto& student : students) {
cout << "Name: " << student.name << ", Reg. Number: " << student.regNumber
<< ", Age: " << student.age << ", Gender: " << student.gender
<< ", Course: " << student.course << endl;
}
}
void addStudent(vector<Student>& students, vector<string>& courses) {
Student newStudent;
cout << "\nEnter student details:\n";
cout << "Name: ";
getline(cin, newStudent.name);
cout << "Registration Number: ";
getline(cin, newStudent.regNumber);
cout << "Age: ";
cin >> newStudent.age;
cin.ignore();
cout << "Gender (M/F): ";
cin >> newStudent.gender;
cin.ignore();
// Display courses and allow the user to select or add a new course
displayCourses(courses);
if (!courses.empty()) {
int choice;
cout << "Select a course number (or enter 0 to add a new course): ";
cin >> choice;
cin.ignore();
if (choice >= 1 && choice <= courses.size()) {
newStudent.course = courses[choice - 1];
} else if (choice == 0) {
addCourse(courses);
newStudent.course = courses.back();
} else {
cout << "Invalid choice. Using default course.\n";
newStudent.course = "Unknown";
}
} else {
cout << "No courses available. Adding student with default course.\n";
newStudent.course = "Unknown";
}
students.push_back(newStudent);
cout << "Student added successfully.\n";
// Save the updated list of students to file
saveStudents(students);
}
void saveStudents(const vector<Student>& students) {
ofstream file("students.txt");
if (!file) {
cout << "Error saving students to file.\n";
return;
}
for (const auto& student : students) {
file << student.name << " " << student.regNumber << " " << student.age << " "
<< student.gender << " " << student.course << "\n";
}
file.close();
}
void readCourses(vector<string>& courses) {
ifstream file("courses.txt");
if (!file) {
cout << "Error opening courses.txt. Exiting...\n";
exit(1);
}
courses.clear();
string course;
while (getline(file, course)) {
courses.push_back(course);
}
file.close();
}
void displayCourses(const vector<string>& courses) {
if (courses.empty()) {
cout << "No courses available.\n";
return;
}
cout << "\n===== List of Courses =====\n";
for (size_t i = 0; i < courses.size(); ++i) {
cout << i + 1 << ". " << courses[i] << endl;
}
}
void addCourse(vector<string>& courses) {
string newCourse;
cout << "\nEnter course name to add: ";
getline(cin, newCourse);
courses.push_back(newCourse);
cout << "Course added successfully.\n";
// Save the updated list of courses to file
saveCourses(courses);
}
void saveCourses(const vector<string>& courses) {
ofstream file("courses.txt");
if (!file) {
cout << "Error saving courses to file.\n";
return;
}
for (const auto& course : courses) {
file << course << "\n";
}
file.close();
}
void addSubjectsToCourse(vector<string>& courses) {
displayCourses(courses);
if (courses.empty()) {
cout << "No courses available to add subjects to.\n";
return;
}
int courseIndex;
cout << "\nEnter the index of the course to add subjects: ";
cin >> courseIndex;
cin.ignore(); // Ignore newline left in the input buffer
if (courseIndex < 1 || courseIndex > courses.size()) {
cout << "Invalid course index.\n";
return;
}
string subjects;
cout << "Enter subjects to add to " << courses[courseIndex - 1] << " (separate by commas): ";
getline(cin, subjects);
// Append subjects to the selected course
courses[courseIndex - 1] += ": " + subjects;
cout << "Subjects added to course " << courses[courseIndex - 1] << " successfully.\n";
// Save the updated list of courses to file
saveCourses(courses);
}