-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudent Management System.cpp
More file actions
461 lines (404 loc) · 17.3 KB
/
Student Management System.cpp
File metadata and controls
461 lines (404 loc) · 17.3 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
/*
* ============================================================
* Student Management System
* Console-based | File Handling | Menu-Driven
* Developed for Thiranex Internship - Task 1
* ============================================================
*/
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <vector>
#include <iomanip>
#include <limits>
#include <algorithm>
using namespace std;
// ─────────────────────────────────────────
// Student Structure
// ─────────────────────────────────────────
struct Student {
int id;
string name;
int age;
string department;
double cgpa;
string email;
};
// ─────────────────────────────────────────
// Constants
// ─────────────────────────────────────────
const string DATA_FILE = "students.dat";
const string SEPARATOR = "|";
// ─────────────────────────────────────────
// Utility: Clear screen (cross-platform)
// ─────────────────────────────────────────
void clearScreen() {
#ifdef _WIN32
system("cls");
#else
system("clear");
#endif
}
// ─────────────────────────────────────────
// Utility: Print horizontal line
// ─────────────────────────────────────────
void printLine(char c = '-', int len = 65) {
cout << string(len, c) << "\n";
}
// ─────────────────────────────────────────
// Utility: Print banner
// ─────────────────────────────────────────
void printBanner() {
clearScreen();
printLine('=');
cout << setw(48) << " STUDENT MANAGEMENT SYSTEM\n";
cout << setw(48) << " Thiranex Internship — Task 1\n";
printLine('=');
cout << "\n";
}
// ─────────────────────────────────────────
// Utility: Safe integer input
// ─────────────────────────────────────────
int getInt(const string& prompt) {
int val;
while (true) {
cout << prompt;
if (cin >> val) {
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return val;
}
cout << " [!] Invalid input. Please enter a number.\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
}
// ─────────────────────────────────────────
// Utility: Safe double input
// ─────────────────────────────────────────
double getDouble(const string& prompt) {
double val;
while (true) {
cout << prompt;
if (cin >> val && val >= 0.0 && val <= 10.0) {
cin.ignore(numeric_limits<streamsize>::max(), '\n');
return val;
}
cout << " [!] Enter a valid CGPA between 0.0 and 10.0.\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
}
// ─────────────────────────────────────────
// Utility: Safe string input
// ─────────────────────────────────────────
string getString(const string& prompt) {
string val;
cout << prompt;
getline(cin, val);
return val;
}
// ─────────────────────────────────────────
// File: Serialize one student to a line
// ─────────────────────────────────────────
string serialize(const Student& s) {
ostringstream oss;
oss << s.id << SEPARATOR
<< s.name << SEPARATOR
<< s.age << SEPARATOR
<< s.department << SEPARATOR
<< fixed << setprecision(2) << s.cgpa << SEPARATOR
<< s.email;
return oss.str();
}
// ─────────────────────────────────────────
// File: Deserialize a line to a student
// ─────────────────────────────────────────
bool deserialize(const string& line, Student& s) {
istringstream iss(line);
string token;
try {
if (!getline(iss, token, '|')) return false; s.id = stoi(token);
if (!getline(iss, token, '|')) return false; s.name = token;
if (!getline(iss, token, '|')) return false; s.age = stoi(token);
if (!getline(iss, token, '|')) return false; s.department = token;
if (!getline(iss, token, '|')) return false; s.cgpa = stod(token);
if (!getline(iss, token, '|')) return false; s.email = token;
return true;
} catch (...) {
return false;
}
}
// ─────────────────────────────────────────
// File: Load all students from file
// ─────────────────────────────────────────
vector<Student> loadAll() {
vector<Student> students;
ifstream file(DATA_FILE);
if (!file.is_open()) return students;
string line;
while (getline(file, line)) {
if (line.empty()) continue;
Student s;
if (deserialize(line, s))
students.push_back(s);
}
file.close();
return students;
}
// ─────────────────────────────────────────
// File: Save all students to file
// ─────────────────────────────────────────
void saveAll(const vector<Student>& students) {
ofstream file(DATA_FILE, ios::trunc);
for (const auto& s : students)
file << serialize(s) << "\n";
file.close();
}
// ─────────────────────────────────────────
// Utility: Generate next unique ID
// ─────────────────────────────────────────
int nextId(const vector<Student>& students) {
int maxId = 1000;
for (const auto& s : students)
if (s.id > maxId) maxId = s.id;
return maxId + 1;
}
// ─────────────────────────────────────────
// Utility: Print table header
// ─────────────────────────────────────────
void printTableHeader() {
printLine('-');
cout << left
<< setw(6) << "ID"
<< setw(20) << "Name"
<< setw(5) << "Age"
<< setw(15) << "Department"
<< setw(7) << "CGPA"
<< setw(24) << "Email"
<< "\n";
printLine('-');
}
// ─────────────────────────────────────────
// Utility: Print one student row
// ─────────────────────────────────────────
void printRow(const Student& s) {
cout << left
<< setw(6) << s.id
<< setw(20) << s.name
<< setw(5) << s.age
<< setw(15) << s.department
<< setw(7) << fixed << setprecision(2) << s.cgpa
<< setw(24) << s.email
<< "\n";
}
// ─────────────────────────────────────────
// FEATURE 1: Add Student
// ─────────────────────────────────────────
void addStudent() {
printBanner();
cout << " [ ADD NEW STUDENT ]\n\n";
vector<Student> students = loadAll();
Student s;
s.id = nextId(students);
s.name = getString(" Full Name : ");
s.age = getInt (" Age : ");
s.department = getString(" Department : ");
s.cgpa = getDouble(" CGPA (0.0-10.0) : ");
s.email = getString(" Email : ");
students.push_back(s);
saveAll(students);
cout << "\n [✓] Student added successfully! (ID: " << s.id << ")\n";
}
// ─────────────────────────────────────────
// FEATURE 2: Display All Students
// ─────────────────────────────────────────
void displayAll() {
printBanner();
cout << " [ ALL STUDENT RECORDS ]\n\n";
vector<Student> students = loadAll();
if (students.empty()) {
cout << " No records found.\n";
return;
}
printTableHeader();
for (const auto& s : students)
printRow(s);
printLine('-');
cout << " Total records: " << students.size() << "\n";
}
// ─────────────────────────────────────────
// FEATURE 3: Search Student
// ─────────────────────────────────────────
void searchStudent() {
printBanner();
cout << " [ SEARCH STUDENT ]\n\n";
cout << " Search by: 1. ID 2. Name\n";
int choice = getInt(" Choice: ");
vector<Student> students = loadAll();
vector<Student> results;
if (choice == 1) {
int id = getInt(" Enter Student ID: ");
for (const auto& s : students)
if (s.id == id) results.push_back(s);
} else {
string keyword = getString(" Enter Name (partial ok): ");
string kLower = keyword;
transform(kLower.begin(), kLower.end(), kLower.begin(), ::tolower);
for (const auto& s : students) {
string nLower = s.name;
transform(nLower.begin(), nLower.end(), nLower.begin(), ::tolower);
if (nLower.find(kLower) != string::npos)
results.push_back(s);
}
}
cout << "\n";
if (results.empty()) {
cout << " No matching student found.\n";
return;
}
printTableHeader();
for (const auto& s : results) printRow(s);
printLine('-');
cout << " Found: " << results.size() << " record(s).\n";
}
// ─────────────────────────────────────────
// FEATURE 4: Update Student
// ─────────────────────────────────────────
void updateStudent() {
printBanner();
cout << " [ UPDATE STUDENT RECORD ]\n\n";
int id = getInt(" Enter Student ID to update: ");
vector<Student> students = loadAll();
bool found = false;
for (auto& s : students) {
if (s.id == id) {
found = true;
cout << "\n Current Record:\n";
printTableHeader();
printRow(s);
printLine('-');
cout << "\n Enter new details (press Enter to keep current):\n\n";
auto updateField = [](const string& prompt, const string& current) -> string {
cout << " " << prompt << " [" << current << "]: ";
string val;
getline(cin, val);
return val.empty() ? current : val;
};
s.name = updateField("Full Name ", s.name);
string ageStr = updateField("Age ", to_string(s.age));
s.age = stoi(ageStr);
s.department = updateField("Department ", s.department);
string cgpaStr = updateField("CGPA ", to_string(s.cgpa));
double newCgpa = stod(cgpaStr);
s.cgpa = (newCgpa >= 0.0 && newCgpa <= 10.0) ? newCgpa : s.cgpa;
s.email = updateField("Email ", s.email);
break;
}
}
if (!found) {
cout << "\n [!] Student with ID " << id << " not found.\n";
return;
}
saveAll(students);
cout << "\n [✓] Student record updated successfully!\n";
}
// ─────────────────────────────────────────
// FEATURE 5: Delete Student
// ─────────────────────────────────────────
void deleteStudent() {
printBanner();
cout << " [ DELETE STUDENT RECORD ]\n\n";
int id = getInt(" Enter Student ID to delete: ");
vector<Student> students = loadAll();
auto it = remove_if(students.begin(), students.end(),
[id](const Student& s) { return s.id == id; });
if (it == students.end()) {
cout << "\n [!] Student with ID " << id << " not found.\n";
return;
}
// Confirm deletion
printTableHeader();
printRow(*it);
printLine('-');
cout << "\n Are you sure you want to delete this record? (y/n): ";
char confirm;
cin >> confirm;
cin.ignore();
if (tolower(confirm) != 'y') {
cout << " [!] Deletion cancelled.\n";
return;
}
students.erase(it, students.end());
saveAll(students);
cout << "\n [✓] Student record deleted successfully!\n";
}
// ─────────────────────────────────────────
// FEATURE 6: Statistics
// ─────────────────────────────────────────
void showStatistics() {
printBanner();
cout << " [ STATISTICS ]\n\n";
vector<Student> students = loadAll();
if (students.empty()) {
cout << " No records found.\n";
return;
}
double totalCgpa = 0, maxCgpa = students[0].cgpa, minCgpa = students[0].cgpa;
Student* topper = &students[0];
for (auto& s : students) {
totalCgpa += s.cgpa;
if (s.cgpa > maxCgpa) { maxCgpa = s.cgpa; topper = &s; }
if (s.cgpa < minCgpa) minCgpa = s.cgpa;
}
cout << " Total Students : " << students.size() << "\n";
cout << " Average CGPA : " << fixed << setprecision(2) << totalCgpa / students.size() << "\n";
cout << " Highest CGPA : " << maxCgpa << " (" << topper->name << ")\n";
cout << " Lowest CGPA : " << minCgpa << "\n";
}
// ─────────────────────────────────────────
// Main Menu
// ─────────────────────────────────────────
void showMenu() {
cout << "\n";
printLine('-');
cout << " MENU\n";
printLine('-');
cout << " 1. Add Student\n";
cout << " 2. Display All Students\n";
cout << " 3. Search Student\n";
cout << " 4. Update Student\n";
cout << " 5. Delete Student\n";
cout << " 6. Statistics\n";
cout << " 0. Exit\n";
printLine('-');
}
// ─────────────────────────────────────────
// Entry Point
// ─────────────────────────────────────────
int main() {
int choice;
do {
printBanner();
showMenu();
choice = getInt(" Enter your choice: ");
switch (choice) {
case 1: addStudent(); break;
case 2: displayAll(); break;
case 3: searchStudent(); break;
case 4: updateStudent(); break;
case 5: deleteStudent(); break;
case 6: showStatistics(); break;
case 0:
cout << "\n Goodbye! — Thiranex Internship\n\n";
break;
default:
cout << "\n [!] Invalid choice. Try again.\n";
}
if (choice != 0) {
cout << "\n Press Enter to return to menu...";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
} while (choice != 0);
return 0;
}