-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1856 lines (1607 loc) · 53.3 KB
/
Copy pathmain.cpp
File metadata and controls
1856 lines (1607 loc) · 53.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
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <sstream>
#include <limits>
using namespace std;
// UnitSync
const int MAX_USER = 500;
const int NUM_TRAINING_DAY = 15;
// User files
const string FILE_BASIC = "basicCadet.txt";
const string FILE_ADVANCE = "advanceCadet.txt";
const string FILE_OFFICER = "staffOfficer.txt";
const string FILE_BRIGADE = "brigadeStaff.txt";
const string FILE_ANNOUNCE = "announcement.txt";
// 2D array for all conversions
const string CONVERT_TABLE[5][4] = {
{"Basic Cadet", "Advance Cadet", "Staff Officer", "Brigade Staff"}, // Role (row 0)
{"Alpha", "Bravo", "Charlie", "Delta"}, // Company (row 1)
{"1st Platoon", "2nd Platoon", "3rd Platoon", "4th Platoon"}, // Platoon (row 2)
{"S1", "S3", "", ""}, // Staff (row 3)
{"Present", "Late", "Excuse", "Absent"} // Status (row 4)
};
// Struct for User's data
struct User
{
string userName = "";
string lastName = "";
string sureName = "";
string middleName = "";
string studentID = "";
string program = "";
string company = "";
string platoon = "";
string status[NUM_TRAINING_DAY] = {""};
string role = "";
string rank = ""; // For advance cadets, staff officers and brigade staff
string staff = ""; // For staff officers and brigade staff
string password = ""; // User Password
int numPresent = 0;
int numLate = 0;
int numAbsent = 0;
int numExcuse = 0;
};
struct Announcement
{
string what = "";
string who = "";
string where = "";
string when = "";
string attire = "";
string toBring = "";
string note = "";
};
void clearScreen();
void displaySubHeader(string title, int width);
void displayHeader(string title, int borderWidth);
void pressEnter();
void displayUserMenu(string typeMenu, string userName = "", int *size = 0);
bool verifyInput(int size, int choice);
void verifyInput(int size, int *choice, bool *verify);
int userMenu(string typeMenu, string userName = "");
void displayLogout(string userName);
string companyConverter(int company);
string platoonConverter(int platoon);
string staffConverter(int staff);
string statusConverter(int status);
void roleConverter(User *user, int role, int index);
void selectUnit(User *basicCadets, int index);
void companyPlatoonSelection(string *company, string *platoon);
void registerUser(User *user, int index);
void registerRank(User *user, int index);
void registerStaff(User *user, int index);
string spaceRemover(string text);
string generateUserName(User *user, int index);
string generatePassword(User *user, int index);
template <typename T>
void swapUser(T &a, T &b);
void sortUsersAlphabetically(User *user, int numUser);
void writeFile(fstream &file, User *user, int index);
void saveUser(User *user, int index, string fileName);
int loadUser(User *user, string fileName);
void updateFile(User *user, int numUser, string fileName);
void registerUser(User *user, int *index, int *numberOfUser, string fileName);
bool verifyUserName(User *user, string userName, int numUser, int *index);
bool verifyPassword(User *user, string password, int index);
bool errorTracker(int *numError);
bool userLogIn(User *user, int numUser, int *index);
void viewProfile(User *user, int index);
void printColoredStatus(string status, string text);
void statusCounter(User *user, int index, int statusIndex);
void userTableHeader(string role);
void displayUser(User *user, int index, int number);
void trainingDayHeader();
void displayStatus(User *user, int index);
void viewAttendance(User *user, int index);
void takeAttendance(User *user, int numUser, string company, string platoon);
void takeAttendance(User *user, int numUser, string staff);
void loadAnnouncement(Announcement *announcement);
void displayAnnouncement(Announcement *announcement);
void saveAnnouncement(Announcement *announcement);
void list(User *user, int numUser);
void listOfUsers();
void attendanceSmartSearch(User *user, int numUser, string target, string type);
void attendanceSmartSearch(User *user, int numUser);
void attendanceSmartSearch();
void createAnnouncement();
void usersFeature(User *user, int index);
int main()
{
int option = 0; // Store the user's option
int role = 0; // Store the user's role
int userIndex = 0; // Store the user's index in logging in
bool running = true; // Condition to keep the system running
bool loginSuccess = false; // Track if login was successful
// Basic Cadets Main Variables
int numCadets = 0; // total number of basic cadets registered
int lastCadet = 0; // track the last index of basic cadets
User *basicCadets = new User[MAX_USER]; // dynamic array to store basic cadets
// Advance Cadets Main Variables
int numAdvanceCadets = 0; // total number of advance cadets registered
int lastAdvanceCadet = 0; // track the last index of advance cadets
User *advanceCadets = new User[MAX_USER]; // dynamic array to store advance cadets
// Staff Officers Main Variables
int numOfficers = 0; // total number of staff officer registered
int lastOfficer = 0; // track the last index of staff officer
User *staffOfficers = new User[MAX_USER]; // dynamic array to store staff officers
// Brigade Staffs Main Variables
int numBrigades = 0; // total number of brigade staff registered
int lastBrigade = 0; // track the last index of brigade staff
User *brigadeStaffs = new User[MAX_USER]; // dynamic array to store brigade staffs
while (running)
{
// Load all users from files
lastCadet = loadUser(basicCadets, FILE_BASIC);
lastAdvanceCadet = loadUser(advanceCadets, FILE_ADVANCE);
lastOfficer = loadUser(staffOfficers, FILE_OFFICER);
lastBrigade = loadUser(brigadeStaffs, FILE_BRIGADE);
// Store the number of user in different role
numCadets = lastCadet;
numAdvanceCadets = lastAdvanceCadet;
numOfficers = lastOfficer;
numBrigades = lastBrigade;
option = userMenu("Main", ""); // Main Menu
switch (option)
{
case 1: // Registration Section
// Role Based Registration
role = userMenu("Role", ""); // Role Menu
switch (role)
{
case 1: // Basic Cadets
roleConverter(basicCadets, role, lastCadet);
registerUser(basicCadets, &lastCadet, &numCadets, FILE_BASIC);
break;
case 2: // Advance Cadets
roleConverter(advanceCadets, role, lastAdvanceCadet);
registerUser(advanceCadets, &lastAdvanceCadet, &numAdvanceCadets, FILE_ADVANCE);
break;
case 3: // Staff Officers
roleConverter(staffOfficers, role, lastOfficer);
registerUser(staffOfficers, &lastOfficer, &numOfficers, FILE_OFFICER);
break;
case 4: // Brigade Staff
roleConverter(brigadeStaffs, role, lastBrigade);
registerUser(brigadeStaffs, &lastBrigade, &numBrigades, FILE_BRIGADE);
break;
}
break;
case 2: // Log In Section
// Role Based Log In
role = userMenu("Role", ""); // Role Menu
switch (role)
{
case 1: // Basic Cadets
loginSuccess = userLogIn(basicCadets, numCadets, &userIndex);
if (loginSuccess)
{
usersFeature(basicCadets, userIndex);
}
break;
case 2: // Advance Cadets
loginSuccess = userLogIn(advanceCadets, numAdvanceCadets, &userIndex);
if (loginSuccess)
{
usersFeature(advanceCadets, userIndex);
}
break;
case 3: // Staff Officers
loginSuccess = userLogIn(staffOfficers, numOfficers, &userIndex);
if (loginSuccess)
{
usersFeature(staffOfficers, userIndex);
}
break;
case 4: // Brigade Staff
loginSuccess = userLogIn(brigadeStaffs, numBrigades, &userIndex);
if (loginSuccess)
{
usersFeature(brigadeStaffs, userIndex);
}
break;
}
break;
case 3: // Exit
running = false; // Stop the system from looping
cout << "\nThank you for using UnitSync!\n";
break;
}
}
// Free Memory
delete[] basicCadets;
delete[] advanceCadets;
delete[] staffOfficers;
delete[] brigadeStaffs;
return 0;
}
// Function that clear the screen
void clearScreen()
{
cout << "\033[2J\033[1;1H";
}
void displaySubHeader(string title, int width)
{
string line = string(width, '-');
cout << line << "\n";
cout << " " << title << "\n";
cout << line << "\n";
}
// Displays a centered title with border
void displayHeader(string title, int borderWidth)
{
clearScreen();
int titleLength = title.length(); // Length of the title text
int spaces = (borderWidth - titleLength) / 2; // Spaces needed to center title
cout << string(borderWidth, '=') << "\n";
cout << string(spaces, ' ') << title << "\n";
cout << string(borderWidth, '=') << "\n";
}
void pressEnter()
{
cout << "\nPress Enter to Continue...";
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
// Function that display the user menu
void displayUserMenu(string typeMenu, string userName, int *size)
{
int width = 40;
string line1 = string(width, '=');
string line2 = string(width, '-');
if (typeMenu == "Main")
{
displayHeader("UnitSync", width); // Display header and clear the screen
displaySubHeader("MAIN MENU", width); // Display sub heaader
// Display the menu choices
cout << " 1. Register\n";
cout << " 2. Log In\n";
cout << " 3. Exit\n";
// Display a line for cleaner UI
cout << line1 << "\n";
*size = 3; // Modify the size for error handling
}
else if (typeMenu == "Role")
{
displayHeader("UnitSync", width);
displaySubHeader("ROLE SELECTION", width);
cout << " 1. Basic Cadet\n";
cout << " 2. Advance Cadet\n";
cout << " 3. Staff Officer\n";
cout << " 4. Brigade Staff\n";
cout << line1 << "\n";
*size = 4;
}
else if (typeMenu == "Cadets Menu")
{
displayHeader("UnitSync", width);
displaySubHeader("User: " + userName, width);
cout << " 1. View My Profile\n";
cout << " 2. Announcements\n";
cout << " 3. View My Attendance\n";
cout << " 4. Log Out\n";
cout << line1 << "\n";
*size = 4;
}
else if (typeMenu == "Staff Officer Menu")
{
displayHeader("UnitSync", width);
displaySubHeader("User: " + userName, width);
cout << " 1. View My Profile\n";
cout << " 2. Take Attendance\n";
cout << " 3. Announcements\n";
cout << " 4. Log Out\n";
cout << line1 << "\n";
*size = 4;
}
else if (typeMenu == "Brigade Staff Menu")
{
displayHeader("UnitSync", width);
displaySubHeader("User: " + userName, width);
cout << " 1. View My Profile\n";
cout << " 2. Create an Announcement\n";
cout << " 3. List of Users\n";
cout << " 4. Attendance Smart Search\n";
cout << " 5. Log Out\n";
cout << line1 << "\n";
*size = 5;
}
else if (typeMenu == "Company Menu")
{
displayHeader("Company Selection", width);
cout << line2 << "\n";
cout << " 1. Alpha\n";
cout << " 2. Bravo\n";
cout << " 3. Charlie\n";
cout << " 4. Delta\n";
cout << line1 << "\n";
*size = 4;
}
else if (typeMenu == "Platoon Menu")
{
displayHeader("Platoon Selection", width);
cout << line2 << "\n";
cout << " 1. 1st Platoon\n";
cout << " 2. 2nd Platoon\n";
cout << " 3. 3rd Platoon\n";
cout << " 4. 4th Platoon\n";
cout << line1 << "\n";
*size = 4;
}
else if (typeMenu == "Staff Menu")
{
displayHeader("Staff Selection", width);
cout << line2 << "\n";
cout << " 1. S1\n";
cout << " 2. S3\n";
cout << line1 << "\n";
*size = 2;
}
else if (typeMenu == "Status Menu")
{
cout << line2 << line2 << "\n";
cout << " STATUS SELECTION\n";
cout << line2 << line2 << "\n";
cout << " [1] Present ";
cout << "[2] Late ";
cout << "[3] Excuse ";
cout << "[4] Absent\n";
cout << line1 << line1 << "\n";
}
else if (typeMenu == "Role Smart Search")
{
displayHeader("Search Selection", width);
cout << line2 << "\n";
cout << " 1. Basic Cadets\n";
cout << " 2. Advance Cadets\n";
cout << line1 << "\n";
*size = 2;
}
else if (typeMenu == "Search By Name Menu")
{
displayHeader("Search Selection", width);
cout << line2 << "\n";
cout << " 1. Last Name\n";
cout << " 2. Sure Name\n";
cout << line1 << "\n";
*size = 2;
}
else if (typeMenu == "Basic Cadet Search Menu")
{
displayHeader("Search Selection", width);
cout << line2 << "\n";
cout << " 1. Company\n";
cout << " 2. Platoon\n";
cout << " 3. Program\n";
cout << " 4. Name\n";
cout << line1 << "\n";
*size = 4;
}
else if (typeMenu == "Advance Cadet Search Menu")
{
displayHeader("Search Selection", width);
cout << line2 << "\n";
cout << " 1. Platoon\n";
cout << " 2. Program\n";
cout << " 3. Name\n";
cout << line1 << "\n";
*size = 3;
}
}
// OVerload 1: function to verify the user input
bool verifyInput(int size, int choice)
{
return (choice >= 1 && choice <= size);
}
// Overload 2: function to handle non interger input
void verifyInput(int size, int *choice, bool *verify)
{
// Vefiry if the choice is an interger
if (cin >> *choice)
{
// Handle range of integer input
*verify = verifyInput(size, *choice);
if (!*verify)
{
cout << "\n[!] Invalid Input. Enter 1-" << size << " Only\n";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
}
else
{
// Handle non-integer input
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "\n[!] Please enter a number.\n";
*verify = false;
}
}
// Function that verify the choice before returning the value
int userMenu(string typeMenu, string userName)
{
int choice = 0;
int size = 0;
bool verify = false;
do
{
displayUserMenu(typeMenu, userName, &size);
cout << "\n Enter Choice: ";
verifyInput(size, &choice, &verify);
pressEnter();
} while (!verify);
return choice;
}
// Function to display logout confirmation
void displayLogout(string userName)
{
const int width = 40;
string line1 = string(width, '=');
string line2 = string(width, '-');
displayHeader("UnitSync", width);
displaySubHeader("LOGOUT", width);
cout << " User: " << userName << endl;
cout << line2 << endl;
cout << " [/] You have successfully logged out.\n";
cout << line1 << endl;
}
// Convert company choice to string
string companyConverter(int company)
{
return CONVERT_TABLE[1][company - 1];
}
// Convert platoon choice to string
string platoonConverter(int platoon)
{
return CONVERT_TABLE[2][platoon - 1];
}
// Convert staff choice to string
string staffConverter(int staff)
{
return CONVERT_TABLE[3][staff - 1];
}
// Convert status choice to string
string statusConverter(int status)
{
return CONVERT_TABLE[4][status - 1];
}
// Convert role
void roleConverter(User *user, int role, int index)
{
(user + index)->role = CONVERT_TABLE[0][role - 1];
}
// Function to select company or platoon
void selectUnit(User *basicCadets, int index)
{
int choice = 0; // Store the users choice
choice = userMenu("Company Menu", "");
(basicCadets + index)->company = companyConverter(choice);
choice = userMenu("Platoon Menu", "");
(basicCadets + index)->platoon = platoonConverter(choice);
}
// Function for selecting company and platoon
void companyPlatoonSelection(string *company, string *platoon)
{
int choice = 0;
choice = userMenu("Company Menu", "");
*company = companyConverter(choice);
choice = userMenu("Platoon Menu", "");
*platoon = platoonConverter(choice);
}
// Function to register user student information
// This is a required for every type of user
void registerUser(User *user, int index)
{
cout << "Last Name(ex. DELA CRUZ) : ";
getline(cin, (user + index)->lastName);
cout << "Sure Name(ex. JUAN) : ";
getline(cin, (user + index)->sureName);
cout << "Middle Name(ex. REYES) : ";
getline(cin, (user + index)->middleName);
cout << "Student ID(ex. LQ-00093-2025): ";
getline(cin, (user + index)->studentID);
cout << "Program(ex. BSCE-1-1) : ";
getline(cin, (user + index)->program);
}
// Function to register advance cadets, staff officer and brigade staff ranks
void registerRank(User *user, int index)
{
cout << "Rank(ex. C/1LT): ";
getline(cin, (user + index)->rank);
}
// Function to register the staff of advance cadets, staff officer and brigade staff
void registerStaff(User *user, int index)
{
int staff = 0;
staff = userMenu("Staff Menu");
(user + index)->staff = staffConverter(staff);
}
// Function that removes the space in text
string spaceRemover(string text)
{
string result = "";
for (char c : text)
{
if (c != ' ')
{
result += c;
}
}
return result;
}
// Function that auto generate the user's name in a format
string generateUserName(User *user, int index)
{
string noSpaceLastName = "",
noSpaceSureName = "",
userName = "";
noSpaceLastName = spaceRemover((user + index)->lastName);
noSpaceSureName = spaceRemover((user + index)->sureName);
userName = noSpaceLastName + "." + noSpaceSureName;
return userName;
}
// Function that auto generate the user's password
string generatePassword(User *user, int index)
{
string password = ""; // Store the user password
string userIndex = to_string(index);
string lastName = spaceRemover((user + index)->lastName);
string studentID = (user + index)->studentID;
// Find positions of the dashes
int firstDash = studentID.find("-");
int secondDash = studentID.find("-", firstDash + 1);
// Extract between first and second dash
string idNumber = studentID.substr(firstDash + 1, secondDash - firstDash - 1);
password = lastName + "-" + idNumber + "-" + userIndex;
return password;
}
template <typename T>
void swapUser(T &a, T &b)
{
T temp = a;
a = b;
b = temp;
}
// Sort users alphabetically by last name (A-Z)
void sortUsersAlphabetically(User *user, int numUser)
{
for (int i = 0; i < numUser - 1; i++)
{
for (int j = 0; j < numUser - i - 1; j++)
{
// Direct comparison (case sensitive)
if ((user + j)->lastName > (user + j + 1)->lastName)
{
// Dereference the pointers to pass by reference
swapUser(*(user + j), *(user + j + 1));
}
}
}
}
// Function to write data in files, serve as the helper function for save user and update file function
void writeFile(fstream &file, User *user, int index)
{
file << (user + index)->userName << ",";
file << (user + index)->lastName << ",";
file << (user + index)->sureName << ",";
file << (user + index)->middleName << ",";
file << (user + index)->studentID << ",";
file << (user + index)->program << ",";
file << (user + index)->company << ",";
file << (user + index)->platoon << ",";
file << (user + index)->role << ",";
file << (user + index)->rank << ",";
file << (user + index)->staff << ",";
file << (user + index)->password << ",";
file << (user + index)->numPresent << ",";
file << (user + index)->numLate << ",";
file << (user + index)->numAbsent << ",";
file << (user + index)->numExcuse << ",";
// Save status array
for (int i = 0; i < NUM_TRAINING_DAY; i++)
{
if ((user + index)->status[i].empty())
file << "--";
else
file << (user + index)->status[i];
if (i < NUM_TRAINING_DAY - 1)
{
file << ",";
}
}
file << "\n";
}
// Function for saving the user in a file based on their role
void saveUser(User *user, int index, string fileName)
{
fstream file;
file.open(fileName, ios::app);
// Save structure members
if (file.is_open())
{
writeFile(file, user, index);
file.close();
}
}
// Function that load users from file
int loadUser(User *user, string fileName)
{
fstream file;
file.open(fileName, ios::in);
if (!file.is_open())
{
cout << "File could not be opened.\n";
return 0;
}
string line;
int index = 0;
while (getline(file, line))
{
stringstream ss(line);
string temp;
getline(ss, (user + index)->userName, ',');
getline(ss, (user + index)->lastName, ',');
getline(ss, (user + index)->sureName, ',');
getline(ss, (user + index)->middleName, ',');
getline(ss, (user + index)->studentID, ',');
getline(ss, (user + index)->program, ',');
getline(ss, (user + index)->company, ',');
getline(ss, (user + index)->platoon, ',');
getline(ss, (user + index)->role, ',');
getline(ss, (user + index)->rank, ',');
getline(ss, (user + index)->staff, ',');
getline(ss, (user + index)->password, ',');
// Convert string to int
getline(ss, temp, ',');
(user + index)->numPresent = stoi(temp);
getline(ss, temp, ',');
(user + index)->numLate = stoi(temp);
getline(ss, temp, ',');
(user + index)->numAbsent = stoi(temp);
getline(ss, temp, ',');
(user + index)->numExcuse = stoi(temp);
// Load status array
for (int i = 0; i < NUM_TRAINING_DAY; i++)
{
getline(ss, (user + index)->status[i], ',');
// Convert "--" back to empty string
if ((user + index)->status[i] == "--")
{
(user + index)->status[i] = "";
}
}
index++;
}
file.close();
return index; // returns number of users loaded
}
// For attendance update — rewrites entire file
void updateFile(User *user, int numUser, string fileName)
{
fstream file;
file.open(fileName, ios::out);
if (file.is_open())
{
for (int i = 0; i < numUser; i++)
{
writeFile(file, user, i);
}
file.close();
}
}
// Function to register a user
void registerUser(User *user, int *index, int *numberOfUser, string fileName)
{
// Store the role
string role = (user + *index)->role;
int platoon = 0;
const int width = 40;
string line1 = string(width, '=');
string line2 = string(width, '-');
displayHeader("UnitSync", width);
displaySubHeader("REGISTRATION", width);
registerUser(user, *index); // register student information
// Registration of Basic Cadets
if (role == "Basic Cadet")
{
// Function for selecting the unit
// Select Company and Platoon
selectUnit(user, *index);
}
// Registration of Advance Cadets
else if (role == "Advance Cadet")
{
registerRank(user, *index);
// Select Platoon
platoon = userMenu("Platoon Menu", "");
(user + *index)->platoon = platoonConverter(platoon);
}
// Registration of Staff Officers and Brigade Staff
else if (role == "Staff Officer" || role == "Brigade Staff")
{
registerRank(user, *index);
registerStaff(user, *index);
}
(user + *index)->userName = generateUserName(user, *index);
(user + *index)->password = generatePassword(user, *index);
displayHeader("UnitSync", width);
displaySubHeader("REGISTRATION", width);
cout << " User Name: " << (user + *index)->userName << endl;
cout << " Your Password: " << (user + *index)->password << endl;
cout << " Please Save your password!\n";
cout << line2 << endl;
saveUser(user, *index, fileName);
(*index)++;
(*numberOfUser)++;
sortUsersAlphabetically(user, *numberOfUser);
updateFile(user, *numberOfUser, fileName);
cout << line1 << endl;
cout << " [/] Registration Complete!\n";
cout << line1 << endl;
pressEnter();
}
// Function that verify the user name
bool verifyUserName(User *user, string userName, int numUser, int *index)
{
// Linear Search to search if the user name exist
for (int i = 0; i < numUser; i++)
{
if (userName == (user + i)->userName) // Condition to verify the user name
{
*index = i; // Store the index of the user
return true; // return if the user name exist
}
}
cout << " [!] No Name Found\n";
cout << string(40, '=') << endl;
pressEnter();
return false; // return if the user name is not found
}
// Function that verify the user's password
bool verifyPassword(User *user, string password, int index)
{
if (password == (user + index)->password) // Condition to verify the password
{
return true; // return if the password is correct
}
else
{
cout << " [!] Password Incorrect\n";
cout << string(40, '=') << endl;
pressEnter();
return false; // return if the password is incorrect
}
}
bool errorTracker(int *numError)
{
const int width = 40;
(*numError)++;
if (*numError == 3)
{
displayHeader("UnitSync", width);
displaySubHeader("LOG IN", width);
cout << " [!] Too many failed attempts.\n Please try again later.\n";
cout << string(width, '=') << endl;
pressEnter();
*numError = 0;
return true; // signal to stop trying
}
return false; // signal to keep trying
}
// Function for user log in - returns true if successful, false if too many attempts
bool userLogIn(User *user, int numUser, int *index)
{
int numError = 0;
string userName = "";
string password = "";
bool verification = false;
bool tryAgain = false;
const int width = 40;
// Username validation
do
{
displayHeader("UnitSync", width);
displaySubHeader("LOG IN", width);
cout << "User Name: ";
getline(cin, userName);
verification = verifyUserName(user, userName, numUser, index);
if (!verification)
{
tryAgain = errorTracker(&numError);
if (tryAgain)
{
return false; // Too many username attempts - exit
}
}
} while (!verification);
numError = 0; // reset counter for password attempts
// Password validation
do
{
displayHeader("UnitSync", width);
displaySubHeader("LOG IN", width);
cout << "User Name: " << (user + *index)->userName;
cout << "\nPassword : ";
cin >> password;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
verification = verifyPassword(user, password, *index);
if (!verification)
{
tryAgain = errorTracker(&numError);
if (tryAgain)
{
return false; // Too many password attempts - exit
}
}
} while (!verification);
cout << " [/] Log In Successfully\n";
cout << string(width, '=') << endl;
pressEnter();
return true; // Login successful
}
// Function for the user's to view their profile
void viewProfile(User *user, int index)
{
string role = (user + index)->role;
const int width = 70;
string line1 = string(width, '=');
string line2 = string(width, '-');
// Display the user's profile using setw
displayHeader("USER PROFILE", width);
cout << left << setw(15) << "User Name" << ": " << (user + index)->userName << "\n";
cout << line2 << "\n";
cout << left << setw(15) << "Last Name" << ": " << setw(25) << (user + index)->lastName
<< left << setw(10) << "Sure Name" << ": " << (user + index)->sureName << "\n";
cout << left << setw(15) << "Middle Name" << ": " << setw(25) << (user + index)->middleName
<< left << setw(10) << "Student ID" << ": " << (user + index)->studentID << "\n";
cout << left << setw(15) << "Program" << ": " << setw(25) << (user + index)->program
<< left << setw(10) << "Role" << ": " << (user + index)->role << "\n";
// Additional information based on the user's role
if (role == "Basic Cadet")