-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathattM.cpp
More file actions
546 lines (469 loc) · 13.9 KB
/
Copy pathattM.cpp
File metadata and controls
546 lines (469 loc) · 13.9 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
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#include <iostream>
#include <limits>
#include <fstream>
#include <string>
#include <ctime>
#include <thread>
#include <chrono>
#include <unistd.h>
#include <termios.h>
#include <iomanip>
#include <chrono>
#include <thread>
// Function to create a pause
using namespace std;
// Constants
const int MAX_ATTENDANCE = 100;
// Function declarations
void clearConsole();
void configureStdinEcho(bool enable = true);
void displayTitle();
void displayDate();
void pause(unsigned int milliseconds);
bool verifyStudentCredentials(const string& username, const string& password);
int adminAuthenticate();
int adminDashboard();
int registerNewStudent();
int viewRegisteredStudents();
int studentAuthenticate();
int studentDashboard();
int recordAttendance(const string& username);
int getAttendanceCount(const string& username);
int removeAllStudents();
int listRegisteredStudentsByUsername();
int removeStudentData();
void NewPart(){
cout << "\nPress Enter key to continue ... ";
cin.get();
cin.get();
}
void clearConsole() {
cout << "\033[2J\033[1;1H";
}
int delay() {
for (int i = 0; i < 3; i++) {
cout << "\nSaving Records ...\n";
for (int ii = 0; ii < 20000; ii++) {
for (int iii = 0; iii < 20000; iii++) {}
}
}
cout << "\nExiting Now ...";
for (int i = 0; i < 3; i++) {
for (int ii = 0; ii < 20000; ii++) {
for (int iii = 0; iii < 20000; iii++) {}
}
}
return 0;
}
void delay(unsigned int mseconds) {
clock_t goal = mseconds + clock();
while (goal > clock()) ;
}
void configureStdinEcho(bool enable) {
termios tty;
tcgetattr(STDIN_FILENO, &tty);
if (enable) {
tty.c_lflag |= ECHO;
} else {
tty.c_lflag &= ~ECHO;
}
tcsetattr(STDIN_FILENO, TCSANOW, &tty);
}
void displayTitle() {
cout << "\n";
cout << "\t\t\t\t\t";
cout << "\n";
cout << "\t\t\t-------------------------------------\n";
cout << "\t\t\t| Attendance Management Of Students |\n";
cout << "\t\t\t-------------------------------------\n";
cout << "\n\n";
cout << "\t\t\t\t";
}
void displayDate() {
time_t T = time(nullptr);
tm tm = *localtime(&T);
cout << "\n\n\n";
cout << "Date: " << setw(2) << setfill('0') << tm.tm_mday << "/"
<< setw(2) << setfill('0') << tm.tm_mon + 1 << "/"
<< tm.tm_year + 1900 << endl;
}
void pause(unsigned int milliseconds) {
this_thread::sleep_for(chrono::milliseconds(milliseconds));
}
int adminAuthenticate() {
clearConsole();
displayDate();
displayTitle();
cout << "\n --------- Admin Login --------- \n\n";
string username, password;
cout << "\nEnter username: ";
cin >> username;
cout << "\nEnter password: ";
configureStdinEcho(false);
cin >> password;
configureStdinEcho(true);
cout << endl;
if (username == "admin" && password == "spark") {
adminDashboard();
delay(3000); // Replace pause() with delay()
} else {
cout << "\n\nError! Invalid Credentials ...";
NewPart();
}
return 0;
}
int adminDashboard() {
int goBack = 0;
while (1) {
clearConsole();
displayDate();
displayTitle();
cout << "\n\n\nPlease select an option ...\n";
cout << "---------------------------\n";
cout << "\n1. Register a Student";
cout << "\n2. Check list of Students registered and view Roll No.";
cout << "\n3. Get list of Students registered by Username";
cout << "\n4. Delete Data of Student";
cout << "\n5. Delete all registered Students";
cout << "\n0. Go back \n";
int choice;
cout << "\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1:
registerNewStudent();
break;
case 2:
viewRegisteredStudents();
break;
case 3:
listRegisteredStudentsByUsername();
break;
case 4:
{
char confirm;
cout << "\nAre you sure you want to delete student data? (y/n): ";
cin >> confirm;
if (confirm == 'y' || confirm == 'Y')
{
removeStudentData();
} else {
cout << "\nOperation canceled.";
}
break;
}
case 5:
{
char confirm;
cout << "\nAre you sure you want to delete all registered students? (y/n): ";
cin >> confirm;
if (confirm == 'y' || confirm == 'Y') {
removeAllStudents();
} else {
cout << "\nOperation canceled.";
}
break;
}
case 0:
goBack = 1;
break;
default:
cout << "\nInvalid choice. Please enter again.";
cin.ignore();
cin.get();
}
if (goBack == 1) {
break;
}
}
return 0;
}
int registerNewStudent() {
clearConsole();
cout << "\n ------ Form to Register Student ------ \n\n";
string name, username, password, rollno, address, fathersname, mothersname;
int initialAttendance = 00; // Changed initial attendance to 0 by default
cout << "\nEnter Name: ";
getline(cin >> ws, name); // Use getline to capture full name, ignoring leading whitespace
cout << "\nEnter Username: ";
getline(cin >> ws, username); // Use getline to capture full username
cout << "\nEnter Password: ";
getline(cin >> ws, password); // Use getline to capture full password
cout << "\nEnter Roll No.: ";
getline(cin >> ws, rollno); // Use getline to capture full roll number
cout << "\nEnter Address: ";
getline(cin, address); // Read address (include spaces)
cout << "\nEnter Father's Name: ";
getline(cin, fathersname); // Read father's name (include spaces)
cout << "\nEnter Mother's Name: ";
getline(cin, mothersname); // Read mother's name (include spaces)
// File operations
ifstream dbFile("db.dat");
if (dbFile) {
string line;
while (getline(dbFile, line)) {
if (line == username + ".dat") {
cout << "\nUsername already Registered. Please choose another username ...";
cin.ignore();
cin.get();
return 0;
}
}
dbFile.close();
}
ofstream dbFileOut("db.dat", ios::app);
if (dbFileOut) {
dbFileOut << username + ".dat" << "\n";
dbFileOut.close();
} else {
cout << "\nError opening database file!";
return 0;
}
ofstream studentFile(username + ".dat");
if (studentFile) {
studentFile << name << "\n";
studentFile << username << "\n";
studentFile << password << "\n";
studentFile << rollno << "\n";
studentFile << address << "\n";
studentFile << fathersname << "\n";
studentFile << mothersname << "\n";
studentFile << initialAttendance << "\n";
studentFile.close();
} else {
cout << "\nError creating student file!";
return 0;
}
cout << "\nStudent Registered Successfully!!\n";
NewPart();
return 0;
}
int viewRegisteredStudents() {
cout << "\n --- Get list of Students registered by Username --- \n\n";
ifstream read("db.dat");
if (read) {
string line;
while (getline(read, line)) {
// Output the filename, which is the username + ".dat"
cout << "\n" << line;
}
read.close();
} else {
cout << "\nNo Record found :(";
}
NewPart();
return 0;
}
int studentAuthenticate() {
clearConsole();
displayDate();
displayTitle();
cout << "\n --------- Student Login --------- \n\n";
studentDashboard();
return 0;
}
int studentDashboard() {
string username, password;
cout << "\nEnter username: ";
cin >> username;
cout << "\nEnter password: ";
configureStdinEcho(false);
cin >> password;
configureStdinEcho(true);
cout << endl;
if (verifyStudentCredentials(username, password)) {
int goBack = 0;
while (true) {
clearConsole();
displayDate();
displayTitle();
cout << "\n\n\nPlease select an option ...\n";
cout << "-------------------------------\n";
cout << "\n1. Mark Attendance for today";
cout << "\n2. Check your Attendance Count";
cout << "\n0. Go back \n";
int choice;
cout << "\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1:
recordAttendance(username);
break;
case 2:
getAttendanceCount(username);
break;
case 0:
goBack = 1;
break;
default:
cout << "\nInvalid choice. Please enter again.";
cin.ignore();
cin.get();
}
if (goBack == 1) {
break;
}
}
} else {
cout << "\n\nInvalid Credentials ...";
NewPart();
}
return 0;
}
bool verifyStudentCredentials(const string& username, const string& password) {
ifstream db("db.dat");
if (!db) {
return false;
}
string line;
bool userExists = false;
while (getline(db, line)) {
if (line == username + ".dat") {
userExists = true;
break;
}
}
db.close();
if (!userExists) {
return false;
}
ifstream studentFile(username + ".dat");
if (!studentFile) {
return false;
}
string filePassword;
studentFile.ignore(numeric_limits<streamsize>::max(), '\n'); // Skip name
studentFile.ignore(numeric_limits<streamsize>::max(), '\n'); // Skip username
studentFile >> filePassword; // Read password
studentFile.close();
return password == filePassword;
}
int recordAttendance(const string& username) {
time_t T = time(nullptr);
tm tm = *localtime(&T);
string currentDate = to_string(tm.tm_mday) + "/" + to_string(tm.tm_mon + 1) + "/" + to_string(tm.tm_year + 1900);
ifstream studentFile(username + ".dat");
if (!studentFile) {
cout << "\nError opening student file!";
return 0;
}
string line;
bool attendanceMarked = false;
while (getline(studentFile, line)) {
if (line == currentDate) {
attendanceMarked = true;
break;
}
}
studentFile.close();
if (attendanceMarked) {
cout << "\nAttendance already marked for today!\n";
NewPart();
return 0;
}
ofstream studentFileOut(username + ".dat", ios::app);
if (studentFileOut) {
studentFileOut << currentDate << "\n";
studentFileOut.close();
cout << "\nAttendance marked for today!\n";
} else {
cout << "\nError opening student file!\n";
}
NewPart();
return 0;
}
int getAttendanceCount(const string& username) {
ifstream studentFile(username + ".dat");
if (!studentFile) {
cout << "\nError opening student file!\n";
return 0;
}
string line;
int count = 0;
while (getline(studentFile, line)) {
++count;
}
studentFile.close();
cout << "\nYour total attendance count is: " << count << "\n";
NewPart();
return 0;
}
int removeAllStudents() {
if (remove("db.dat") != 0) {
perror("Error deleting database file");
NewPart();
return 1;
}
cout << "\nAll student records deleted successfully!\n";
NewPart();
return 0;
}
int listRegisteredStudentsByUsername() {
cout << "\n --- Get list of Students registered by Username --- \n\n";
ifstream read("db.dat");
if (read) {
string line;
while (getline(read, line)) {
string filename = line;
ifstream studentFile(filename);
string username;
int lineCount = 0;
while (getline(studentFile, line)) {
if (++lineCount == 2) { // Username is on the 2nd line
username = line;
break;
}
}
cout << "\n" << filename << " -> " << username;
}
} else {
cout << "\nNo Record found :(\n";
}
NewPart();
return 0;
}
int removeStudentData() {
string username;
cout << "\nEnter the username of the student whose data you want to delete: ";
cin >> username;
string fileName = username + ".dat";
if (remove(fileName.c_str()) != 0) {
cout << "\nError deleting student data.\n";
} else {
cout << "\nStudent data deleted successfully.\n";
}
NewPart();
return 0;
}
int main() {
while (true) {
clearConsole();
displayDate();
displayTitle();
cout << "\n\n\nPlease select an option ...\n";
cout << "---------------------------\n";
cout << "\n1. Student Login";
cout << "\n2. Admin Login";
cout << "\n0. Exit\n";
int choice;
cout << "\nEnter your choice: ";
cin >> choice;
switch (choice) {
case 1:
studentAuthenticate();
break;
case 2:
adminAuthenticate();
break;
case 0:
cout<<"\nExiting ...\n\n\n\n\n";
NewPart();
clearConsole();
return 0;
default:
cout << "\nInvalid choice. Please enter again.";
cin.ignore();
cin.get();
}
}
}