-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudent.c
2905 lines (2534 loc) · 100 KB
/
student.c
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
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <locale.h>
#include <ctype.h>
#define MAX_USERS 200
#define MAX_STUDENTS 100
#define MAX_LECTURERS 20
#define MAX_COURSES 10
#define MAX_GRADES 5
#define MAX_LENGTH 50 // for user input
#define MAX_DATE 12
#define MAX_HOLIDAYS 20
#define MAX_FEEDBACK_LENGTH 1000
#define MAX_ENTRIES 100
#define MAX_ATTENDANCE_RECORDS 20
// ----------------------------------------------------------------
// ---------- STRUCTS ---------------------------------------------
// ----------------------------------------------------------------
typedef struct {
int phone_number;
char dob[11];
char nationality[30];
char gender;
} Details;
// Define a struct for attendance record
typedef struct AttendanceNode {
int studentID;
char date[5];
int attendanceLab;
int attendanceLecture;
struct AttendanceNode* next; // Pointer to the next node in the linked list
} AttendanceNode;
typedef struct {
int courseID;
char course_name[MAX_LENGTH];
int lecturerID;
float marks;
char grade;
AttendanceNode* attendance_head; // Head of the attendance linked list
} Course;
typedef struct {
int studentID;
char student_fname[50];
char student_lname[50];
char level[4];
Details student_details;
Course enrolled_courses[2]; // Assume each student can enroll in 2 courses for now
float cgpa;
} Student;
typedef struct {
int userID;
int password;
char usertype[2]; // 's' for student, 'p' for programme admin, 'l' for lecturer, 'y' for system admin.
} User;
typedef struct HolidayNode {
char holidayName[MAX_LENGTH];
char startDate[11];
char endDate[11];
struct HolidayNode *next;
} HolidayNode;
typedef struct FeedbackNode {
int feedbackID;
char date[MAX_DATE];
int courseID;
char feedback[MAX_FEEDBACK_LENGTH];
char reply[MAX_FEEDBACK_LENGTH]; // To store lecturer's reply
struct FeedbackNode* next;
} FeedbackNode;
FeedbackNode* feedbackHead = NULL;
User users[MAX_USERS];
Student students[MAX_STUDENTS];
Course courses[MAX_COURSES];
AttendanceNode attendance[MAX_STUDENTS];
const char grades[MAX_GRADES] = { 'A', 'B', 'C', 'D', 'F' };
HolidayNode *holidayList;
//-----------------------------------------------------------------
// -- FUNCTION PROTOTYPES -----------------------------------------
// ----------------------------------------------------------------
void capitalize(char* str);
int checkDOB(int year, int month, int day);
void clearBuffer();
int isNumerical(const char* input);
int getID(const char* entity);
int createUsers();
int createStudents();
int createCourses();
int createAttendance();
int createGrades();
int createFeedback();
void registerUser();
void viewUsers();
void deleteUser();
char calculateGrade(float marks, float lowerBoundary[MAX_GRADES]);
int updateStudentGrades();
void modifyLowerBoundaries(const char grades[]);
void updateCGPAToFile(int studentIndex, float cgpa);
void calculateCGPA();
void generateStudentReport();
void viewCourses();
void viewLecturers();
int createLecturers();
void viewGrades();
int lecturerMenu();
int studentMenu(int loggedInUserId);
int programmeAdminMenu();
int programAdmin_viewMenu();
int programAdmin_manageMenu();
int systemAdminMenu();
void mainMenu();
void registerAndManageUsers();
void manageCourses();
void manageGrades();
void viewAndUpdateStudentAttendance();
void updateStudentMarksFile();
int attendanceRecordExists(int studentID, int courseID, const char* date, Course* course);
int isValidDate(const char* date);
void viewAndReplyFeedback();
HolidayNode* createHolidayList();
void printHolidayList(HolidayNode *head);
void freeHolidayList(HolidayNode *head);
int checkHolidayDate(int month, int day, char *date);
void createNewHoliday(HolidayNode **head);
void deleteHoliday(HolidayNode **head);
int holidayManagement(HolidayNode **holidayList);
void viewPersonalDetails(int loggedInUserId);
void updatePersonalDetails(int loggedInUserId);
void viewEnrolledCoursesAndGrades(int loggedInUserId);
void viewAttendance(int loggedInUserId);
void viewHolidayCalendar();
void freeFeedbackList();
// ----------------------------------------------------------------
// -- HARDCODED INITIAL DATA --------------------------------------
// ----------------------------------------------------------------
int createUsers() {
FILE* fp;
fp = fopen("users.txt", "w");
if (fp == NULL) {
printf("(!) Error opening users.txt.\n");
return 1;
}
users[0] = (User){ 1001, 119, "s" };
users[1] = (User){ 2001, 272, "p" };
users[2] = (User){ 3001, 123, "l" };
users[3] = (User){ 4001, 456, "y" };
users[4] = (User){ 1002, 119, "s" };
users[5] = (User){ 2002, 272, "p" };
users[6] = (User){ 3002, 123, "l" };
users[7] = (User){ 4004, 456, "y" };
for (int i = 0; i < 8; i++) {
fprintf(fp, "%d,%d,%s\n", users[i].userID, users[i].password, users[i].usertype);
}
fclose(fp);
return 0;
}
int numStudents = 0; // Variable to track the number of ALL students -- intialise as 0
int createStudents() {
// Student 1 details
students[0].studentID = 1001;
strcpy(students[0].student_fname, "Miyeon");
strcpy(students[0].student_lname, "Cho");
strcpy(students[0].level, "F");
students[0].cgpa = 3.0;
students[0].student_details = (Details){ 1234567890, "1997-01-31", "International", 'F' };
// Student 1 courses
students[0].enrolled_courses[0] = (Course){ 101, "C Programming", 3001, 80.00, 'A' };
students[0].enrolled_courses[1] = (Course){ 102, "Java", 3002, 50.00, 'C' };
// Student 2 details
students[1].studentID = 1002;
strcpy(students[1].student_fname, "Soyeon");
strcpy(students[1].student_lname, "Jeon");
strcpy(students[1].level, "D1");
students[1].cgpa = 3.2;
students[1].student_details = (Details){ 987654321, "1998-08-26", "Local", 'F' };
// Student 2 courses
students[1].enrolled_courses[0] = (Course){ 101, "C Programming", 3001, 15.00, 'F' };
students[1].enrolled_courses[1] = (Course){ 102, "Java", 3002, 70.00, 'B' };
// Write student details to "studentdetails.txt"
FILE* fp;
fp = fopen("studentdetails.txt", "w");
if (fp == NULL) {
printf("Error opening file.\n");
return 1;
}
for (int i = 0; i < 2; i++) {
fprintf(fp, "%d,%s,%s,%d,%s,%s,%c,%.2f,%s\n",
students[i].studentID,
students[i].student_fname,
students[i].student_lname,
students[i].student_details.phone_number,
students[i].student_details.dob,
students[i].student_details.nationality,
students[i].student_details.gender,
students[i].cgpa,
students[i].level);
}
fclose(fp);
// Write student marks to "studentmarks.txt" FILE
fp = fopen("studentmarks.txt", "w");
if (fp == NULL) {
printf("Error opening file.\n");
return 1;
}
for (int i = 0; i < 2; i++) {
fprintf(fp, "%d,", students[i].studentID);
for (int j = 0; j < 2; j++) {
fprintf(fp, "%d,%.2f,", students[i].enrolled_courses[j].courseID, students[i].enrolled_courses[j].marks);
fprintf(fp, "%c", students[i].enrolled_courses[j].grade);
if (j < 1) {
fprintf(fp, ",");
}
}
fprintf(fp, "\n");
}
fclose(fp);
numStudents += 2;
return 0;
}
int numCourses = 0; // Variable to track the number of courses -- initialise as 0
int createCourses() {
FILE* fp = fopen("courses.txt", "w");
if (fp == NULL) {
printf("Error opening file.\n");
return 1;
}
// Example course creation
Course course1 = (Course){ 101, "C Programming", 3001, 0.0, 'F', NULL };
courses[numCourses++] = course1;
Course course2 = (Course){ 102, "Java", 3001, 0.0, 'F', NULL };
courses[numCourses++] = course2;
for (int i = 0; i < 2; i++) {
fprintf(fp, "%d,%s,%d\n",
courses[i].courseID, courses[i].course_name, courses[i].lecturerID);
}
fclose(fp);
return 0;
}
int createAttendance() {
FILE* fp = fopen("attendance.txt", "w");
if (fp == NULL) {
printf("Error opening file.\n");
return 1;
}
// Sample attendance data creation (for demonstration)
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) { // Assuming each student has 2 courses
AttendanceNode* node = (AttendanceNode*)malloc(sizeof(AttendanceNode));
if (node == NULL) {
printf("Memory allocation failed\n");
fclose(fp);
return 1;
}
node->studentID = students[i].studentID;
strcpy(node->date, "W1"); // Example date
node->attendanceLab = 1; // Example data - present
node->attendanceLecture = 1; // Example data - present
node->next = students[i].enrolled_courses[j].attendance_head; // Link to the current head
students[i].enrolled_courses[j].attendance_head = node; // Set new node as head
fprintf(fp, "%d,%d,%s,%d,%d\n",
node->studentID,
students[i].enrolled_courses[j].courseID,
node->date,
node->attendanceLab,
node->attendanceLecture);
}
}
fclose(fp);
return 0;
}
int createGrades() {
float lowerBoundary[MAX_GRADES] = { 75.00, 65.00, 50.00, 40.00, 0.00 };
FILE* file = fopen("grades.txt", "w");
if (file == NULL) {
printf("Error opening file!\n");
return 1;
}
for (int i = 0; i < MAX_GRADES; i++) {
fprintf(file, "%.2f\n", lowerBoundary[i]);
}
fclose(file);
return 0;
}
int createFeedback() {
// Hardcoded feedback entries
char* dates[] = {"2024-04-01", "2024-04-02", "2024-04-03", "2024-04-04"};
int courseIDs[] = {101, 102, 101, 102};
char* feedbackTexts[] = {"Great lecture!", "I dont understand the assignment", "hello","Best lecturer ever!"};
int feedbackCount = sizeof(courseIDs) / sizeof(courseIDs[0]); // Calculate the number of feedback entries
int feedbackID = feedbackCount; // Start assigning feedback IDs from the total count downwards
for (int i = 0; i < feedbackCount; i++) { // Loop through each feedback entry
FeedbackNode* newNode = (FeedbackNode*)malloc(sizeof(FeedbackNode)); // Allocate memory for the new node
if (!newNode) {
printf("Memory allocation failed.\n");
continue;
}
newNode->feedbackID=i+1; // Assign feedback ID
strcpy(newNode->date, dates[i]);
newNode->courseID = courseIDs[i];
strcpy(newNode->feedback, feedbackTexts[i]);
strcpy(newNode->reply, ""); // Initialize reply as empty
newNode->next = feedbackHead; // Inserting new nodes at the head
feedbackHead = newNode; // Update the head to the new node
}
// write the linked list to the file
FILE* fp = fopen("feedbacks.txt", "w");
if (!fp) {
printf("Error opening file for writing.\n");
return 1;
}
FeedbackNode* current = feedbackHead;
while (current != NULL) {
fprintf(fp, "%d,%s,%d,\"%s\",\"%s\"\n",
current->feedbackID, current->date, current->courseID, current->feedback, current->reply);
current = current->next;
}
fclose(fp);
return 0;
}
HolidayNode* createHolidayList() {
// Define holidays
char holidays[][MAX_HOLIDAYS] = {"Thaipusam", "Chinese New Year", "Hari Raya Puasa", "Labour Day"};
char startDates[][11] = {"2024-01-25", "2024-02-10", "2024-04-08", "2024-05-01"};
char endDates[][11] = {"2024-01-25", "2024-02-13", "2024-04-11", "2024-05-01"};
HolidayNode *head = NULL;
HolidayNode *current = NULL;
// Create nodes for each holiday and link them together
for (int i = 0; i < sizeof(holidays) / sizeof(holidays[0]); i++) {
HolidayNode *newHoliday = (HolidayNode*)malloc(sizeof(HolidayNode));
if (newHoliday == NULL) {
// Memory allocation failed
printf("Memory allocation failed for holiday node.\n");
// Free the allocated memory before returning NULL
while (head != NULL) {
current = head;
head = head->next;
free(current);
}
return NULL;
}
// Copy holiday details to the new node
strcpy(newHoliday->holidayName, holidays[i]);
strcpy(newHoliday->startDate, startDates[i]);
strcpy(newHoliday->endDate, endDates[i]);
newHoliday->next = NULL;
// Link the new node to the list
if (head == NULL) {
head = newHoliday;
current = newHoliday;
} else {
current->next = newHoliday;
current = current->next;
}
}
return head;
}
// ----------------------------------------------------------------
// -- OTHER FUNCTIONS ---------------------------------------------
// ----------------------------------------------------------------
int isValidDate(const char* date) {
int year, month, day;
if (sscanf(date, "%d-%d-%d", &year, &month, &day) != 3)
return 0;
if (year != 2024) // Enter the year is 2024
return 0;
if (month < 1 || month > 12) // Check month range
return 0;
if (day < 1 || day > 31) // Check day range
return 0;
return 1; // The date is valid
}
void capitalize(char* str) {
// Handle NULL or empty string cases
if (str == NULL || str[0] == '\0') {
return;
}
int capitalizeNext = 1; // Flag to indicate whether the next character should be capitalized
for (int i = 0; str[i] != '\0'; i++) {
char currentChar = str[i];
if (isspace(currentChar)) {
capitalizeNext = 1; // Set the flag to capitalize the next character
}
else if (capitalizeNext) {
str[i] = toupper(currentChar); // Capitalize the current character
capitalizeNext = 0; // Reset the flag
}
else {
str[i] = tolower(currentChar); // Lowercase the current character
}
}
}
int checkDOB(int year, int month, int day) {
// Validate year range
if (year < 1970 || year > 2008)
return 1;
// Validate month range
if (month < 1 || month > 12)
return 1;
// Validate day range based on month and leap year
int daysInMonth;
switch (month) {
case 2:
// Check for leap year
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
daysInMonth = 29;
else
daysInMonth = 28;
break;
case 4:
case 6:
case 9:
case 11:
daysInMonth = 30;
break;
default:
daysInMonth = 31;
break;
}
// Validate day
if (day < 1 || day > daysInMonth)
return 1;
return 0;
}
void clearBuffer() {
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
void freeFeedbackList() {
FeedbackNode* current = feedbackHead;
while (current != NULL) {
FeedbackNode* nextNode = current->next;
free(current);
current = nextNode;
}
feedbackHead = NULL; // Ensure the head pointer is no longer pointing to freed memory
}
// Function to check if input is numerical
int isNumerical(const char* input) {
if (input == NULL || *input == '\0') {
return 0; // Empty string or NULL is not numerical
}
while (*input != '\0' && *input != '\n') { // Stop at newline character
if (!isdigit(*input)) {
return 0; // Non-digit character found
}
input++; // Move to the next character
}
return 1; // All characters are digits
}
int getID(const char* entity) {
char input[MAX_LENGTH]; // Buffer for user input
int value;
int validInput = 0;
do {
printf("Enter %s ID: ", entity);
fgets(input, sizeof(input), stdin); // Read user input as a string
// Remove newline character if present
if (input[strlen(input) - 1] == '\n') {
input[strlen(input) - 1] = '\0';
}
// Check if input is numerical
if (isNumerical(input)) {
value = atoi(input); // Convert string to integer
validInput = 1; // Set flag to exit loop
}
else {
printf("(!) Please enter a numerical value.\n");
}
} while (!validInput); // Repeat until valid input is received
return value;
}
int isValidWeek(const char* week) {
int len = strlen(week);
// Check if the string starts with 'W' and has at least 2 characters
if (len < 2 || week[0] != 'W') {
return 0;
}
// Convert the substring after 'W' to an integer
int weekNumber = atoi(week + 1);
// Check if the week number is between 1 and 15
if (weekNumber >= 1 && weekNumber <= 15) {
return 1;
}
return 0;
}
// ----------------------------------------------------------------
// ----------------- SYSTEM ADMINISTRATOR -------------------------
// ----------------------------------------------------------------
int studentUserIDs[MAX_STUDENTS]; // Array to store NEW student IDs
int numStudentUsers = 0; // Variable to track the number of NEW students
int lecturerUserIDs[MAX_LECTURERS]; // Array to store lecturer IDs
int numLecturerUsers = 0; // Variable to track the number of lecturer users
void registerUser() {
int newUserID, numUsers, newPassword;
char newUserType[2], tempPassword[4];
// Get the number of users before adding a new user
numUsers = 0;
while (users[numUsers].userID != 0) {
numUsers++;
}
do {
do {
printf("['s' for Student, 'p' for Program Admin, 'l' for Lecturer, 'y' for System Admin]\n");
printf("Enter user type: ");
scanf("%s", newUserType);
newUserType[0] = tolower(newUserType[0]); // Convert to lowercase
} while (newUserType[0] != 's' && newUserType[0] != 'p' && newUserType[0] != 'l' && newUserType[0] != 'y');
// Validate user ID format based on user type
do {
printf("Enter new user ID: ");
scanf("%d", &newUserID);
getchar(); // Clear input buffer
switch (newUserType[0]) {
case 's':
if (newUserID / 1000 != 1) {
printf("(!) Student user ID must start with 1.\n");
newUserID = 0; // Reset invalid ID
}
break;
case 'p':
if (newUserID / 1000 != 2) {
printf("(!) Program Admin user ID must start with 2.\n");
newUserID = 0; // Reset invalid ID
}
break;
case 'l':
if (newUserID / 1000 != 3) {
printf("(!) Lecturer user ID must start with 3.\n");
newUserID = 0; // Reset invalid ID
}
break;
case 'y':
if (newUserID / 1000 != 4) {
printf("(!) System Admin user ID must start with 4.\n");
newUserID = 0; // Reset invalid ID
}
break;
}
} while (newUserID == 0);
int valid = 1;
for (int i = 0; i < numUsers; i++) {
if (users[i].userID == newUserID) {
valid = 0;
printf("(!) User ID already exists.\n");
break;
}
}
if (valid) {
break; // Break out of loop if user ID is valid
}
} while (1);
do {
printf("Enter new password (3 numbers): ");
scanf("%s", tempPassword);
} while (strlen(tempPassword) != 3 || !isdigit(tempPassword[0]) || !isdigit(tempPassword[1]) || !isdigit(tempPassword[2]) ||
tempPassword[3] != '\0'); // Check if the password consists of 3 digits
newPassword = atoi(tempPassword); // Convert string to integer
FILE* fp = fopen("users.txt", "a"); // Open file in append mode to add new user
if (fp == NULL) {
printf("Error opening file.\n");
return;
}
fprintf(fp, "%d,%d,%s\n", newUserID, newPassword, newUserType);
fclose(fp);
printf("(*) New user registered successfully.\n");
// Store the new user in the users array
users[numUsers].userID = newUserID;
users[numUsers].password = newPassword;
users[numUsers].usertype[0] = newUserType[0];
users[numUsers].usertype[1] = '\0'; // Null-terminate the string
// If the user type is 's', store the new userID in the studentUserIDs array
if (newUserType[0] == 's') {
if (numStudentUsers < MAX_STUDENTS) {
studentUserIDs[numStudentUsers] = newUserID;
numStudentUsers++;
} else {
printf("(!) Maximum number of student users reached.\n");
}
}
// If the user type is 'l', store the new userID in the lecturerUserIDs array
if (newUserType[0] == 'l') {
if (numLecturerUsers < MAX_LECTURERS) {
lecturerUserIDs[numLecturerUsers] = newUserID;
numLecturerUsers++;
} else {
printf("(!) Maximum number of lecturer users reached.\n");
}
}
}
void viewUsers() {
// Calculate the number of users
int numUsers = sizeof(users) / sizeof(users[0]);
printf("User List:\n");
printf("+---------+----------+-----------+\n");
printf("| User ID | Password | User Type |\n");
printf("+---------+----------+-----------+\n");
for (int i = 0; i < numUsers; i++) {
if (users[i].userID != 0) {
printf("| %-7d | %-8d | %-9s |\n", users[i].userID, users[i].password, users[i].usertype);
}
}
printf("+---------+----------+-----------+\n");
}
void modifyUser() {
int userID, numUsers;
int found = 0;
numUsers = 0; // Get the number of users before adding a new user
while (users[numUsers].userID != 0) {
numUsers++;
}
printf("List of existing users:\n"); // Display a list of existing users
for (int i = 0; i < numUsers; i++) {
printf("User ID: %d\n", users[i].userID);
// Display other user details if needed
}
printf("Enter the user ID to modify: "); // Prompt the user to choose a user ID to modify
scanf("%d", &userID);
// Search for the selected user ID in the array
for (int i = 0; i < numUsers; i++) {
if (users[i].userID == userID) {
found = 1;
printf("User ID: %d\n", users[i].userID);
printf("Modifying password...\n");
char newPassword[MAX_LENGTH]; // Buffer to store user input for the new password
do {
printf("Enter new password: ");
scanf("%s", newPassword);
if (!isNumerical(newPassword)) {
printf("Invalid input. Password must consist of numerical characters.\n");
}
} while (!isNumerical(newPassword)); // Continue prompting until a valid numerical password is entered
users[i].password = atoi(newPassword); // Convert the numerical password from string to integer
printf("Password modified successfully.\n");
break; // Exit loop after modifying the user
}
}
// If the selected user ID is not found
if (!found) {
printf("User ID not found.\n");
}
}
void deleteUser() {
int inputUserID;
printf("Enter User ID to delete: ");
scanf("%d", &inputUserID);
int found = 0;
int numUsers;
// Get the number of users before adding a new user
numUsers = 0;
while (users[numUsers].userID != 0) {
numUsers++;
}
// Open users.txt for writing
FILE* fp = fopen("users.txt", "w");
if (fp == NULL) {
printf("Error opening users.txt for writing.\n");
return;
}
// Write users back to the file excluding the deleted user
for (int i = 0; i < numUsers; i++) {
if (users[i].userID == inputUserID) {
found = 1;
if (users[i].usertype[0] == 'l') {
printf("Lecturers cannot be deleted.\n");
} else {
// Remove the user from the users array by shifting the remaining elements
for (int j = i; j < numUsers - 1; j++) {
users[j] = users[j + 1];
}
memset(&users[numUsers - 1], 0, sizeof(User)); // Clear the last element
printf("User deleted successfully.\n");
}
}
// Write the users back to the file
fprintf(fp, "%d,%d,%s\n", users[i].userID, users[i].password, users[i].usertype);
}
fclose(fp);
if (!found) {
printf("User with ID %d not found. Exiting.\n", inputUserID);
}
}
int systemAdminSubmenu() {
char choice;
do {
printf("\nManage Users\n");
printf("1. Register User\n");
printf("2. View Users\n");
printf("3. Modify User Password\n");
printf("4. Delete User\n");
printf("5. Back\n");
printf("Enter your choice: ");
scanf(" %c", &choice);
switch (choice) {
case '1': registerUser(); break;
case '2': viewUsers(); break;
case '3': modifyUser(); break;
case '4': deleteUser(); break;
case '5': return 0;
default: printf("Please enter a number between 1 and 5.\n");
}
} while (1);
return 0;
}
// Function to update a student's grades
char calculateGrade(float marks, float lowerBoundary[MAX_GRADES]) {
const char grades[MAX_GRADES] = { 'A', 'B', 'C', 'D', 'F' };
for (int k = 0; k < MAX_GRADES; k++) {
if (marks >= lowerBoundary[k]) {
return grades[k];
}
}
return grades[MAX_GRADES - 1]; // Return 'F' if no boundary is satisfied
}
int updateStudentGrades() {
// Read lower boundaries from grades.txt
float lowerBoundary[MAX_GRADES];
FILE* gradesFile = fopen("grades.txt", "r");
if (gradesFile == NULL) {
printf("Error opening grades file!\n");
return 1;
}
for (int i = 0; i < MAX_GRADES; i++) {
if (fscanf(gradesFile, "%f", &lowerBoundary[i]) != 1) {
printf("Error reading from grades file!\n");
fclose(gradesFile);
return 1;
}
}
fclose(gradesFile);
// Read student marks from studentmarks.txt and update grades
FILE* marksFile = fopen("studentmarks.txt", "r");
if (marksFile == NULL) {
printf("Error opening marks file!\n");
return 1;
}
int studentCount = 0;
while (studentCount < MAX_STUDENTS &&
fscanf(marksFile, "%d,%d,%f,%c,%d,%f,%c",
&students[studentCount].studentID,
&students[studentCount].enrolled_courses[0].courseID,
&students[studentCount].enrolled_courses[0].marks,
&students[studentCount].enrolled_courses[0].grade,
&students[studentCount].enrolled_courses[1].courseID,
&students[studentCount].enrolled_courses[1].marks,
&students[studentCount].enrolled_courses[1].grade) == 7) {
// Update grades based on lower boundaries
for (int j = 0; j < 2; j++) {
char grade = calculateGrade(students[studentCount].enrolled_courses[j].marks, lowerBoundary);
students[studentCount].enrolled_courses[j].grade = grade;
}
studentCount++;
}
fclose(marksFile);
// Rewrite updated marks to studentmarks.txt
marksFile = fopen("studentmarks.txt", "w");
if (marksFile == NULL) {
printf("Error opening marks file for writing!\n");
return 1;
}
for (int i = 0; i < studentCount; i++) {
fprintf(marksFile, "%d,%d,%.2f,%c,%d,%.2f,%c\n",
students[i].studentID,
students[i].enrolled_courses[0].courseID,
students[i].enrolled_courses[0].marks,
students[i].enrolled_courses[0].grade,
students[i].enrolled_courses[1].courseID,
students[i].enrolled_courses[1].marks,
students[i].enrolled_courses[1].grade);
}
fclose(marksFile);
printf("Entries in STUDENTMARKS.TXT updated successfully.\n");
// Update the grades directly in the students array
for (int i = 0; i < studentCount; i++) {
for (int j = 0; j < 2; j++) {
students[i].enrolled_courses[j].grade = calculateGrade(students[i].enrolled_courses[j].marks, lowerBoundary);
}
}
return 0;
}
// Once grading system is redefined, studentmarks.txt is automatically updated
void modifyLowerBoundaries(const char grades[]) {
float lowerBoundary[MAX_GRADES];
// Read lower boundaries from grades.txt
FILE* file = fopen("grades.txt", "r");
if (file == NULL) {
printf("Error opening grades.txt.\n");
return;
}
for (int i = 0; i < MAX_GRADES; i++) {
if (fscanf(file, "%f", &lowerBoundary[i]) != 1) {
printf("Error reading grades.txt.\n");
fclose(file);
return;
}
}
fclose(file);
printf("Enter new lower boundaries for each grade:\n");
for (int i = 0; i < MAX_GRADES; i++) {
float newBoundary;
printf("Grade %c (current boundary: %.2f): ", grades[i], lowerBoundary[i]);
scanf("%f", &newBoundary);
if (newBoundary >= 0.0 && newBoundary <= 100.0) {
lowerBoundary[i] = newBoundary;
}
else {
printf("Invalid input! Lower boundary must be between 0 and 100.\n");
i--; // Decrement i to re-prompt for the same grade
}
}
// Write updated lower boundaries to grades.txt
file = fopen("grades.txt", "w");
if (file == NULL) {
printf("Error opening grades.txt for writing!\n");
return;
}
for (int i = 0; i < MAX_GRADES; i++) {
fprintf(file, "%.2f\n", lowerBoundary[i]);
}
fclose(file);
printf("Lower boundaries updated in grades.txt\n");
if (updateStudentGrades() != 0) {
printf("Error updating STUDENTMARKS.TXT.\n");
}
}
void updateCGPAToFile(int studentIndex, float cgpa) {
FILE* file = fopen("studentdetails.txt", "w");
if (file == NULL) {
printf("Error opening file for writing.\n");
return;
}
// Write student details including CGPA to file
for (int i = 0; i < studentIndex; i++) {
fprintf(file, "%d,%s,%s,%d,%s,%s,%c,%.2f,%s\n",
students[i].studentID,
students[i].student_fname,
students[i].student_lname,
students[i].student_details.phone_number,
students[i].student_details.dob,
students[i].student_details.nationality,
students[i].student_details.gender,
students[i].cgpa,
students[i].level);
}
// Write the current student's details including the new CGPA
fprintf(file, "%d,%s,%s,%d,%s,%s,%c,%.2f,%s\n",
students[studentIndex].studentID,
students[studentIndex].student_fname,
students[studentIndex].student_lname,
students[studentIndex].student_details.phone_number,
students[studentIndex].student_details.dob,
students[studentIndex].student_details.nationality,
students[studentIndex].student_details.gender,
cgpa,
students[studentIndex].level);
fclose(file);
}
void calculateCGPA() {
int studentCount = 0;
while (studentCount < numStudents) {
// Calculate CGPA for the current student
float totalGradePoints = 0.0;
for (int j = 0; j < 2; j++) {
// Calculate grade points based on grades
float gradePoint;
switch (students[studentCount].enrolled_courses[j].grade) {
case 'A': gradePoint = 4.0; break;
case 'B': gradePoint = 3.0; break;
case 'C': gradePoint = 2.0; break;
case 'D': gradePoint = 1.7; break;
case 'F': gradePoint = 0.0; break;
default: gradePoint = 0.0; break; // Handle invalid grades
}
// Update total grade points and credits
totalGradePoints += gradePoint;
}
// Calculate CGPA for the current student
float cgpa = totalGradePoints / 2;
students[studentCount].cgpa = cgpa; // Update CGPA in the students array
updateCGPAToFile(studentCount, cgpa); // Update CGPA to file
studentCount++;
}
}