-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmenu.c
93 lines (90 loc) · 3.18 KB
/
menu.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
#include <stdio.h>
#include "menu.h"
#include "student_operations.h"
// Function to display the menu options
void displayMenu() {
printf("\n=== Student Record System ===\n");
printf("1. Add Student\n");
printf("2. Search and Display Student\n");
printf("3. Modify Student\n");
printf("4. Display All Students\n");
printf("5. Delete Student\n");
printf("6. Calculate Average Marks\n");
printf("7. Sort Students\n");
printf("8. Save Students to File\n");
printf("9. Load Students from File\n");
printf("10. Exit\n");
printf("Please enter your choice: ");
}
// Function to get the user's choice
int getUserChoice() {
int choice;
if (scanf("%d", &choice) != 1) {
printf("Invalid input. Please enter a number.\n");
while (getchar() != '\n'); // Clear input buffer
return -1; // Invalid choice
}
return choice;
}
// Function to handle the user's choice
void handleUserChoice(int choice, Student ***students, int *numStudents) {
switch (choice) {
case 1:
addStudent(students, numStudents);
break;
case 2:
searchAndDisplayStudent(*students, *numStudents);
break;
case 3:
modifyStudent(*students, *numStudents);
break;
case 4:
displayStudents((const Student **)(*students), *numStudents);
break;
case 5: {
int rollNumber;
printf("Enter roll number to delete: ");
if (scanf("%d", &rollNumber) != 1) {
printf("Invalid input. Please enter a valid roll number.\n");
while (getchar() != '\n'); // Clear the input buffer
break;
}
int index = searchStudentIndex((const Student **)(*students), *numStudents, rollNumber);
if (index != -1) {
deleteStudent(students, numStudents, index);
} else {
printf("Student not found.\n");
}
break;
}
case 6:
if (*numStudents > 0) {
float average = calculateAverageMarks((const Student **)(*students), *numStudents);
printf("The average marks of all students is: %.2f\n", average);
} else {
printf("No students to calculate average.\n");
}
break;
case 7: {
printf("Sort by marks in (1) Ascending or (2) Descending order? ");
int sortOrder;
scanf("%d", &sortOrder);
sortStudentsByMarks(*students, *numStudents, sortOrder == 1 ? 1 : 0);
printf("Students sorted by marks.\n");
break;
}
case 8:
saveStudentsToFile((const Student **)(*students), *numStudents, "students.txt");
printf("Student records saved to 'students.txt'.\n");
break;
case 9:
loadStudentsFromFile(students, numStudents, "students.txt");
printf("Student records loaded from 'students.txt'.\n");
break;
case 10:
printf("Exiting program...\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
}