-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
140 lines (128 loc) · 5.1 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include "student_records.h"
#include "menu.h"
#include "student_operations.h"
/**
* freeMemory - Frees allocated memory for student records.
*
* @students: An array of Student pointers.
* @numStudents: The number of students in the array.
*/
void freeMemory(Student **students, int numStudents)
{
for (int i = 0; i < numStudents; i++)
{
free(students[i]);/* Free each student*/
}
free(students);/* Free the array of pointers*/
}
/**
* checkPassOrFail - Checks if a student passed or failed based on marks.
*
* @marks: The marks obtained by the student.
* Return: "Pass" if marks are >= 40, otherwise "Fail".
*/
const char* checkPassOrFail(float marks)
{
return marks >= 40.0 ? "Pass" : "Fail";
}
/**
* main - Entry point for the Student Record System.
* Return: Success status.
*/
int main(void) {
// Allocate memory for a maximum number of students
int maxStudents = 100;
Student **students = malloc(maxStudents * sizeof(Student *));
if (students == NULL) {
fprintf(stderr, "Memory allocation failed!\n");
return EXIT_FAILURE; // Exit if memory allocation fails
}
int numStudents = 0; // Initialize student count
// User greeting
printf("Welcome to the Student Record System!\n");
printf("Please enter your name: ");
char userName[50];
scanf("%49s", userName); // Limit input to avoid buffer overflow
printf("Hello, %s! Let's manage student records.\n", userName);
// Main program loop
while (1) {
displayMenu();
int choice = getUserChoice(); // Function to get user choice
if (choice == -1) { // If invalid input is detected in getUserChoice
continue; // Loop again to ask for valid input
}
switch (choice) {
case 1:
if (numStudents < maxStudents) {
addStudent(&students, &numStudents); // Add new student
printf("Student has %s\n", checkPassOrFail(students[numStudents - 1]->marks)); // Check pass/fail
} else {
printf("Student limit reached. Cannot add more students.\n");
}
break;
case 2:
displayStudents((const Student **)students, numStudents); // Display all students
break;
case 3:
searchAndDisplayStudent(students, numStudents); // Search and display student by roll number
break;
case 4:
modifyStudent(students, numStudents); // Modify student record
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); // Delete student by index
} else {
printf("Student not found.\n");
}
break;
}
case 6:
// Calculate and display average marks
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:
// Sort students by marks
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:
// Save student records to file
saveStudentsToFile((const Student **)students, numStudents, "students.txt");
printf("Student records saved to 'students.txt'.\n");
break; // Success message is handled in the function
case 9:
// Load student records from file
loadStudentsFromFile(&students, &numStudents, "students.txt");
printf("Student records loaded from 'students.txt'.\n");
break; // Success message is handled in the function
case 10:
freeMemory(students, numStudents); // Free allocated memory
printf("Exiting the program. Goodbye!\n");
return EXIT_SUCCESS; // Exit successfully
default:
printf("Invalid choice. Please try again.\n");
}
}
// Memory cleanup is handled in case of exit
freeMemory(students, numStudents); // Ensuring memory is freed on exit
return 0;
}