-
Notifications
You must be signed in to change notification settings - Fork 0
/
project01.cpp
263 lines (235 loc) · 8.29 KB
/
project01.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
#include <iostream>
#include <cstring>
#include <iomanip>
using namespace std;
const int MAX_STUDENTS = 100;
const int MAX_NAME_LENGTH = 50;
const int MAX_COURSE_LENGTH = 50;
struct Student {
int id;
char name[MAX_NAME_LENGTH];
int age;
char course[MAX_COURSE_LENGTH];
};
Student students[MAX_STUDENTS];
int studentCount = 0;
// Function to center text
void printCentered(const string &text, int width) {
int padding = (width - text.length()) / 2;
for (int i = 0; i < padding; ++i) {
cout << " ";
}
cout << text << endl;
}
// Function to check if ID is unique
bool isUniqueID(int id) {
for (int i = 0; i < studentCount; i++) {
if (students[i].id == id) {
return false;
}
}
return true;
}
// Function to insert a new student record
void insertStudent() {
if (studentCount >= MAX_STUDENTS) {
printCentered("Student record list is full.", 100);
return;
}
int id, age;
char name[MAX_NAME_LENGTH], course[MAX_COURSE_LENGTH];
printCentered("Enter Student ID: ", 100);
cin >> id;
while (!isUniqueID(id)) {
printCentered("ID already exists. Enter a unique Student ID: ", 100);
cin >> id;
}
students[studentCount].id = id;
cin.ignore();
printCentered("Enter Student Name: ", 100);
cin.getline(students[studentCount].name, MAX_NAME_LENGTH);
printCentered("Enter Student Age: ", 100);
cin >> students[studentCount].age;
while (cin.fail() || students[studentCount].age <= 0 || students[studentCount].age > 100) {
cin.clear();
cin.ignore(1000, '\n');
printCentered("Invalid age. Please enter a valid age (1-100): ", 100);
cin >> students[studentCount].age;
}
cin.ignore();
printCentered("Enter Student Course: ", 100);
cin.getline(students[studentCount].course, MAX_COURSE_LENGTH);
studentCount++;
printCentered("Student record inserted successfully.", 100);
}
// Function to view all student records
void viewAllStudents() {
if (studentCount == 0) {
printCentered("No student records found.", 100);
return;
}
printCentered("ID\tName\t\tAge\tCourse", 100);
printCentered("-------------------------------------------", 100);
for (int i = 0; i < studentCount; i++) {
cout << setw(25) << setfill(' ') << students[i].id << "\t"
<< setw(25) << setfill(' ') << students[i].name << "\t\t"
<< setw(25) << setfill(' ') << students[i].age << "\t"
<< setw(25) << setfill(' ') << students[i].course << endl;
}
}
// Function to search for a student record by ID
void searchStudentByID() {
int id;
printCentered("Enter Student ID to search: ", 100);
cin >> id;
for (int i = 0; i < studentCount; i++) {
if (students[i].id == id) {
cout << setw(25) << setfill(' ') << "ID: " << students[i].id << ", Name: " << students[i].name
<< ", Age: " << students[i].age << ", Course: " << students[i].course << endl;
return;
}
}
printCentered("Student record not found.", 100);
}
// Function to search for a student record by name
void searchStudentByName() {
char name[MAX_NAME_LENGTH];
printCentered("Enter Student Name to search: ", 50);
cin.ignore();
cin.getline(name, MAX_NAME_LENGTH);
for (int i = 0; i < studentCount; i++) {
if (strcmp(students[i].name, name) == 0) {
cout << setw(25) << setfill(' ') << "ID: " << students[i].id << ", Name: " << students[i].name
<< ", Age: " << students[i].age << ", Course: " << students[i].course << endl;
return;
}
}
printCentered("Student record not found.", 100);
}
// Function to delete a student record by ID
void deleteStudent() {
int id;
printCentered("Enter Student ID to delete: ", 100);
cin >> id;
for (int i = 0; i < studentCount; i++) {
if (students[i].id == id) {
printCentered("Are you sure you want to delete this student record? (y/n): ", 100);
char confirmation;
cin >> confirmation;
if (confirmation == 'y' || confirmation == 'Y') {
for (int j = i; j < studentCount - 1; j++) {
students[j] = students[j + 1];
}
studentCount--;
printCentered("Student record deleted successfully.", 100);
} else {
printCentered("Deletion cancelled.", 100);
}
return;
}
}
printCentered("Student record not found.", 100);
}
// Function to update a student record by ID
void updateStudent() {
int id;
printCentered("Enter Student ID to update: ", 100);
cin >> id;
for (int i = 0; i < studentCount; i++) {
if (students[i].id == id) {
cin.ignore(); // to ignore the newline character left by cin
printCentered("Enter new Student Name: ", 100);
cin.getline(students[i].name, MAX_NAME_LENGTH);
printCentered("Enter new Student Age: ", 100);
cin >> students[i].age;
while (cin.fail() || students[i].age <= 0 || students[i].age > 100) {
cin.clear(); // Clear error flag
cin.ignore(1000, '\n'); // Ignore the rest of the input
printCentered("Invalid age. Please enter a valid age (1-100): ", 100);
cin >> students[i].age;
}
cin.ignore();
printCentered("Enter new Student Course: ", 100);
cin.getline(students[i].course, MAX_COURSE_LENGTH);
printCentered("Student record updated successfully.", 100);
return;
}
}
printCentered("Student record not found.", 100);
}
// Function to sort students by name
void sortStudentsByName() {
for (int i = 0; i < studentCount - 1; i++) {
for (int j = 0; j < studentCount - i - 1; j++) {
if (strcmp(students[j].name, students[j + 1].name) > 0) {
Student temp = students[j];
students[j] = students[j + 1];
students[j + 1] = temp;
}
}
}
printCentered("Students sorted by name.", 100);
}
// Function to display statistics
void displayStatistics() {
if (studentCount == 0) {
printCentered("No student records to calculate statistics.", 100);
return;
}
int totalAge = 0;
for (int i = 0; i < studentCount; i++) {
totalAge += students[i].age;
}
cout << setw(25) << setfill(' ') << "Total number of students: " << studentCount << endl;
cout << setw(25) << setfill(' ') << "Average age of students: " << static_cast<float>(totalAge) / studentCount << endl;
}
int main() {
int choice;
do {
printCentered("-------- Student Management System Menu --------", 100);
printCentered("1. Insert Student record", 100);
printCentered("2. View All Student records", 100);
printCentered("3. Search Student record by ID", 100);
printCentered("4. Search Student record by Name", 100);
printCentered("5. Delete Student record", 100);
printCentered("6. Update Student record", 100);
printCentered("7. Sort Students by Name", 100);
printCentered("8. Display Statistics", 100);
printCentered("9. Exit", 100);
printCentered("--------------------------------------------", 100);
printCentered("Enter your choice: ", 100);
cin >> choice;
switch (choice) {
case 1:
insertStudent();
break;
case 2:
viewAllStudents();
break;
case 3:
searchStudentByID();
break;
case 4:
searchStudentByName();
break;
case 5:
deleteStudent();
break;
case 6:
updateStudent();
break;
case 7:
sortStudentsByName();
break;
case 8:
displayStatistics();
break;
case 9:
printCentered("Exiting the program.", 50);
break;
default:
printCentered("Invalid choice. Please try again.", 50);
}
} while (choice != 9);
return 0;
}