-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAttendance_System.cpp
More file actions
394 lines (352 loc) · 14.6 KB
/
Attendance_System.cpp
File metadata and controls
394 lines (352 loc) · 14.6 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
#include <iostream>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <limits>
using namespace std;
const int MAX_STUDENTS = 100;
const int MAX_COURSES = 10;
void splitString(const string &str, char delimiter, string tokens[], int &tokenCount) {
string token;
stringstream ss(str);
tokenCount = 0;
while (getline(ss, token, delimiter)) {
tokens[tokenCount++] = token;
}
}
class Person {
protected:
string name;
int prn;
public:
Person() : name(""), prn(0) {}
Person(string n, int p) : name(n), prn(p) {}
virtual void display() {
cout << "Name: " << name << ", PRN: " << prn << endl;
}
string getName() const { return name; }
int getPrn() const { return prn; }
static bool validateName(const string &name) {
for (char c : name) {
if (!isalpha(c) && c != ' ') {
return false;
}
}
return true;
}
};
class Course {
public:
string courseName;
int totalHours;
int attendedHours;
int conductedHours;
Course() : courseName(""), totalHours(0), attendedHours(0), conductedHours(0) {}
Course(string name, int hours) : courseName(name), totalHours(hours), attendedHours(0), conductedHours(0) {}
double getAttendancePercentage() const {
if (conductedHours == 0) return 0;
return (attendedHours / (double)conductedHours) * 100;
}
double getOverallAttendancePercentage() const {
if (totalHours == 0) return 0;
return (attendedHours / (double)totalHours) * 100;
}
void display() const {
cout << "Course Name: " << courseName << ", Total Hours: " << totalHours << ", Attended Hours: " << attendedHours << ", Conducted Hours: " << conductedHours << endl;
}
};
class Student : public Person {
public:
Course courses[MAX_COURSES];
int courseCount;
Student() : Person(), courseCount(0) {}
Student(string n, int p) : Person(n, p), courseCount(0) {}
void addCourse(Course course) {
if (courseCount < MAX_COURSES) {
courses[courseCount++] = course;
}
}
virtual void markAttendance(string courseName, bool present) {
for (int i = 0; i < courseCount; ++i) {
if (courses[i].courseName == courseName) {
courses[i].conductedHours++;
if (present) {
courses[i].attendedHours++;
if (courses[i].attendedHours > courses[i].conductedHours)
courses[i].attendedHours = courses[i].conductedHours;
cout << "Marked attendance for " << name << " in " << courseName << endl;
} else {
cout << "Attendance not marked for " << name << " in " << courseName << endl;
}
return;
}
}
cout << "Course not found." << endl;
}
void viewAttendance() const {
cout << setw(15) << left << "Course Name" << setw(15) << "Attended" << setw(15) << "Total" << setw(15) << "Conducted" << setw(15) << "Current %" << setw(15) << "Overall %" << endl;
cout << "--------------------------------------------------------------------------------" << endl;
for (int i = 0; i < courseCount; ++i) {
cout << setw(15) << left << courses[i].courseName
<< setw(15) << courses[i].attendedHours
<< setw(15) << courses[i].totalHours
<< setw(15) << courses[i].conductedHours
<< setw(15) << fixed << setprecision(2) << courses[i].getAttendancePercentage() << "%"
<< setw(15) << fixed << setprecision(2) << courses[i].getOverallAttendancePercentage() << "%" << endl;
}
}
void deleteStudentInfo() {
name = "";
prn = 0;
courseCount = 0;
}
};
class Teacher : public Person {
public:
Teacher(string n, int p) : Person(n, p) {}
void display() override {
cout << "Teacher: " << name << ", PRN: " << prn << endl;
}
void displayAttendanceForAll(const Student students[], int studentCount, const string courseNames[], int courseCount) const {
for (int i = 0; i < courseCount; ++i) {
cout << "\nAttendance for course: " << courseNames[i] << endl;
cout << setw(15) << left << "Student Name" << setw(10) << "PRN" << setw(15) << "Attended" << setw(15) << "Total" << setw(15) << "Conducted" << setw(15) << "Current %" << setw(15) << "Overall %" << endl;
cout << "----------------------------------------------------------------------------------------------------" << endl;
for (int j = 0; j < studentCount; ++j) {
for (int k = 0; k < students[j].courseCount; ++k) {
if (students[j].courses[k].courseName == courseNames[i]) {
const Course& course = students[j].courses[k];
cout << setw(15) << left << students[j].getName()
<< setw(10) << students[j].getPrn()
<< setw(15) << course.attendedHours
<< setw(15) << course.totalHours
<< setw(15) << course.conductedHours
<< setw(15) << fixed << setprecision(2) << course.getAttendancePercentage()
<< setw(15) << fixed << setprecision(2) << course.getOverallAttendancePercentage() << endl;
}
}
}
}
}
};
void writeCSV(const Student students[], int studentCount, const string courseNames[], int courseCount, const string &filename) {
ofstream file(filename);
if (file.is_open()) {
file << "Name,PRN";
for (int i = 0; i < courseCount; ++i) {
file << "," << courseNames[i] << "_TotalHours," << courseNames[i] << "_Attended," << courseNames[i] << "_Conducted," << courseNames[i] << "_Current%," << courseNames[i] << "_Overall%";
}
file << endl;
for (int i = 0; i < studentCount; ++i) {
file << students[i].getName() << "," << students[i].getPrn();
for (int j = 0; j < courseCount; ++j) {
bool courseFound = false;
for (int k = 0; k < students[i].courseCount; ++k) {
if (students[i].courses[k].courseName == courseNames[j]) {
file << "," << students[i].courses[k].totalHours
<< "," << students[i].courses[k].attendedHours
<< "," << students[i].courses[k].conductedHours
<< "," << fixed << setprecision(2) << students[i].courses[k].getAttendancePercentage()
<< "," << fixed << setprecision(2) << students[i].courses[k].getOverallAttendancePercentage();
courseFound = true;
break;
}
}
if (!courseFound) {
file << ",0,0,0,0.00,0.00";
}
}
file << endl;
}
file.close();
cout << "Data saved to " << filename << endl;
} else {
cerr << "Unable to open file " << filename << endl;
}
}
void readCSV(Student students[], int &studentCount, string courseNames[], int &courseCount, const string &filename) {
ifstream file(filename);
if (file.is_open()) {
string line;
bool firstLine = true;
studentCount = 0;
while (getline(file, line)) {
if (firstLine) {
string headers[MAX_COURSES * 5];
int headerCount;
splitString(line, ',', headers, headerCount);
courseCount = 0;
for (int i = 2; i < headerCount; i += 5) {
courseNames[courseCount++] = headers[i].substr(0, headers[i].find("_TotalHours"));
}
firstLine = false;
} else {
string tokens[MAX_COURSES * 5 + 2];
int tokenCount;
splitString(line, ',', tokens, tokenCount);
string studentName = tokens[0];
int prn = stoi(tokens[1]);
Student student(studentName, prn);
for (int i = 2, j = 0; i < tokenCount; i += 5, ++j) {
int totalHours = stoi(tokens[i]);
int attendedHours = stoi(tokens[i + 1]);
int conductedHours = stoi(tokens[i + 2]);
Course course(courseNames[j], totalHours);
course.attendedHours = attendedHours;
course.conductedHours = conductedHours;
student.addCourse(course);
}
students[studentCount++] = student;
}
}
file.close();
cout << "Data loaded from " << filename << endl;
} else {
cerr << "Unable to open file " << filename << endl;
}
}
void markAttendance(Student students[], int studentCount, const string courseNames[], int courseCount) {
cout << "Enter course name: ";
string courseName;
cin >> courseName;
bool courseExists = false;
for (int i = 0; i < courseCount; ++i) {
if (courseNames[i] == courseName) {
courseExists = true;
break;
}
}
if (!courseExists) {
cout << "Course not found." << endl;
return;
}
cout << "Marking attendance for course: " << courseName << endl;
for (int i = 0; i < studentCount; ++i) {
char response;
cout << "Is " << students[i].getName() << " present for " << courseName << "? (Enter 'P' for present, 'A' for absent): ";
while (!(cin >> response) || (response != 'P' && response != 'A')) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please enter 'P' for present or 'A' for absent: ";
}
students[i].markAttendance(courseName, response == 'P');
}
}
void studentPortal(const Student students[], int studentCount) {
cout << "Enter student PRN: ";
int prn;
while (!(cin >> prn)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please enter a valid PRN: ";
}
for (int i = 0; i < studentCount; ++i) {
if (students[i].getPrn() == prn) {
students[i].viewAttendance();
return;
}
}
cout << "Student not found." << endl;
}
void deleteStudent(Student students[], int &studentCount) {
cout << "Enter student PRN to delete: ";
int prn;
while (!(cin >> prn)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please enter a valid PRN: ";
}
for (int i = 0; i < studentCount; ++i) {
if (students[i].getPrn() == prn) {
students[i].deleteStudentInfo();
for (int j = i; j < studentCount - 1; ++j) {
students[j] = students[j + 1];
}
studentCount--;
cout << "Student information deleted." << endl;
return;
}
}
cout << "Student not found." << endl;
}
int main() {
Student students[MAX_STUDENTS];
string courseNames[MAX_COURSES];
int studentCount = 0;
int courseCount = 0;
string filename = "Attendance_File.csv";
readCSV(students, studentCount, courseNames, courseCount, filename);
if (studentCount == 0) {
cout << "Enter number of students: ";
int numStudents;
while (!(cin >> numStudents) || numStudents > MAX_STUDENTS) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please enter a valid number of students (max " << MAX_STUDENTS << "): ";
}
for (int i = 0; i < numStudents; ++i) {
string name;
int prn;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Enter name of student " << (i + 1) << ": ";
getline(cin, name);
while (!Person::validateName(name)) {
cout << "Invalid name. Please enter a name with alphabetic characters only: ";
getline(cin, name);
}
cout << "Enter PRN of student " << (i + 1) << ": ";
while (!(cin >> prn)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please enter a valid PRN: ";
}
students[studentCount++] = Student(name, prn);
}
cout << "Enter number of courses: ";
int numCourses;
while (!(cin >> numCourses) || numCourses > MAX_COURSES) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please enter a valid number of courses (max " << MAX_COURSES << "): ";
}
for (int i = 0; i < numCourses; ++i) {
string courseName;
int credits;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Enter course name and credits for course " << (i + 1) << ": ";
getline(cin, courseName);
while (!(cin >> credits)) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input. Please enter valid credits: ";
}
courseNames[courseCount++] = courseName;
for (int j = 0; j < studentCount; ++j) {
students[j].addCourse(Course(courseName, credits * 15));
}
}
writeCSV(students, studentCount, courseNames, courseCount, filename);
}
while (true) {
cout << "\n1. Mark Attendance\n2. Student Portal\n3. Display Attendance for All Students\n4. Delete Student Information\n5. Exit\nChoose an option: ";
int choice;
cin >> choice;
if (choice == 1) {
markAttendance(students, studentCount, courseNames, courseCount);
writeCSV(students, studentCount, courseNames, courseCount, filename);
} else if (choice == 2) {
studentPortal(students, studentCount);
} else if (choice == 3) {
Teacher teacher("Admin", 0);
teacher.displayAttendanceForAll(students, studentCount, courseNames, courseCount);
} else if (choice == 4) {
deleteStudent(students, studentCount);
writeCSV(students, studentCount, courseNames, courseCount, filename);
} else if (choice == 5) {
break;
} else {
cout << "Invalid choice. Please try again." << endl;
}
}
return 0;
}