-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcos201.c
More file actions
388 lines (332 loc) · 9.93 KB
/
cos201.c
File metadata and controls
388 lines (332 loc) · 9.93 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdbool.h>
// #include <math.h>
const int PASS_THRESHOLD = 40;
const int MAX_NAME = 100;
// struct Student
// {
// /* data */
// char name[100]; // Character array to store the student's name (max 99 characters + null terminator)
// int rollNumber; // Integer to store the student's roll number
// float marks;
// };
typedef struct
{
char name[MAX_NAME];
int rollNumber;
float marks;
} Student;
// Function definition
bool is_integer(const char *input); // Checks if a string represents a valid integer
void createStudent(Student **students, int *count); // Adds a new student to the array
void updateStudent(Student *students, int count); // Updates an existing student's information
void deleteStudent(Student **students, int *count); // Deletes a student from the array
void viewStudents(Student *students, int count); // Displays all students in the array
void saveStudentsToFile(Student *students, int count); // Saves student data to a file
int searchStudent(Student student[], int size, int searchId);
// int pot (Student student); // Searches for a student by ID
// Implementation
int main()
{
char name[MAX_NAME];
Student student1;
printf("========================================\n");
printf(" Welcome to the Student portal! \n");
printf("========================================\n\n");
printf("Please enter your name: ");
if (fgets(name, sizeof(name), stdin) != NULL)
{
name[strcspn(name, "\n")] = '\0';
printf("\nWelcome, %s! It's great to meet you!\n", name);
}
else
{
printf("\nError reading input.\n");
return 1;
}
printf("========================================\n");
printf(" Enter your Exam mark \n");
printf("========================================\n\n");
float marks;
printf("Please enter your marks: ");
if (scanf("%f", &marks) != 1)
{
printf("\nInvalid input. Please enter a numeric value for marks.\n");
return 1;
}
if (marks >= PASS_THRESHOLD)
{
printf("\nCongratulations, %s! You have passed the exam with %.2f marks.\n", name, marks);
}
else
{
printf("\nSorry, %s. You have not passed the exam. You scored %.2f marks. Better luck next time!\n", name, marks);
}
printf("========================================\n");
printf(" Student Record Management \n");
printf("========================================\n\n");
Student *students2 = NULL;
int studentCount = 0;
int choice;
float average = 0.0;
int searchId = 0;
int result = 0;
float total = 0;
while (1)
{
printf("\n===== STUDENT MANAGEMENT SYSTEM =====\n");
printf("1. Add Student\n");
printf("2. Modify Student\n");
printf("3. Delete Student\n");
printf("4. Display All Students\n");
printf("5. Save to File\n");
printf("6. Search Student by ID\n");
printf("7. Calculate Average Score\n");
printf("10. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
getchar(); // clear input buffer
switch (choice)
{
case 1:
createStudent(&students2, &studentCount);
viewStudents(students2, studentCount);
break;
case 2:
updateStudent(students2, studentCount);
viewStudents(students2, studentCount);
break;
case 3:
deleteStudent(&students2, &studentCount);
viewStudents(students2, studentCount);
break;
case 4:
viewStudents(students2, studentCount);
break;
case 5:
saveStudentsToFile(students2, studentCount);
break;
case 6:
printf("Enter Student ID to search: ");
scanf("%d", &searchId);
result = searchStudent(students2, studentCount, searchId);
if (result == -1)
{
printf("Student not found.\n");
}
break;
case 7:
// Average Score
for (int i = 0; i < studentCount; i++)
{
total += students2[i].marks;
average = total / studentCount;
}
printf("Total score is: %.2f\n", total);
printf("The average score is: %.2f\n", average);
break;
case 10:
printf("Exiting program...\n");
free(students2);
return 0;
default:
printf("Invalid choice! Try again.\n");
}
}
return 0;
}
/**
* @brief Create a Student object
*
* @param students
* @param count
*/
void createStudent(Student **students, int *count)
{
*students = realloc(*students, (*count + 1) * sizeof(Student));
Student *newStudent = &((*students)[*count]);
printf("Enter Student ID: ");
scanf("%d", &newStudent->rollNumber);
getchar();
printf("Enter Student Name: ");
fgets(newStudent->name, MAX_NAME, stdin);
newStudent->name[strcspn(newStudent->name, "\n")] = '\0';
printf("Enter Student Score: ");
scanf("%f", &newStudent->marks);
(*count)++;
printf("Student added successfully!\n");
// viewStudents(*students, *count);
}
/**
* @brief Update an existing Student object
*
* @param students
* @param count
*/
void updateStudent(Student *students, int count)
{
if (count == 0)
{
printf("No students available to modify.\n");
return;
}
int id;
printf("Enter Student ID to modify: ");
scanf("%d", &id);
for (int i = 0; i < count; i++)
{
if (students[i].rollNumber == id)
{
printf("Enter new name: ");
getchar();
fgets(students[i].name, MAX_NAME, stdin);
students[i].name[strcspn(students[i].name, "\n")] = '\0';
printf("Enter new marks: ");
scanf("%f", &students[i].marks);
printf("Record updated successfully!\n");
return;
}
}
printf("Student with ID %d not found.\n", id);
}
/**
* @brief Delete a Student object
*
* @param students
* @param count
*/
void deleteStudent(Student **students, int *count)
{
if (*count == 0)
{
printf("No students available to delete.\n");
return;
}
int id;
printf("Enter Student ID to delete: ");
scanf("%d", &id);
for (int i = 0; i < *count; i++)
{
if ((*students)[i].rollNumber == id)
{
// Shift elements left
for (int j = i; j < *count - 1; j++)
{
(*students)[j] = (*students)[j + 1];
}
(*count)--;
*students = realloc(*students, (*count) * sizeof(Student));
printf("Student deleted successfully!\n");
return;
}
}
printf("Student with ID %d not found.\n", id);
}
/**
* @brief Display all Student objects
*
* @param students
* @param count
*/
void viewStudents(Student *students, int count)
{
if (count == 0)
{
printf("No student records available.\n");
return;
}
printf("\n====== STUDENT LIST ======\n");
for (int i = 0; i < count; i++)
{
printf("Roll Number: %d | Name: %s | Marks: %.2f\n",
students[i].rollNumber, students[i].name, students[i].marks);
}
}
void saveStudentsToFile(Student *students, int count)
{
char filename[100];
printf("Provide a filename e.g (backup.txt, please include the .txt): ");
if (fgets(filename, sizeof(filename), stdin) != NULL)
{
filename[strcspn(filename, "\n")] = '\0';
FILE *file = fopen(filename, "w");
if (file == NULL)
{
printf("Error opening file for writing.\n");
}
for (int i = 0; i < count; i++)
{
fprintf(file, "%d,%s,%.2f\n", students[i].rollNumber, students[i].name, students[i].marks);
}
fclose(file);
printf("Student data saved to %s successfully.\n", filename);
}
else
{
printf("\nError reading input.\n");
}
return;
}
bool is_integer(const char *input)
{
// Handle potential newline character from fgets
size_t len = strlen(input);
if (len > 0 && input[len - 1] == '\n')
{
len--;
}
if (len == 0)
{
return false; // Empty input is not a number
}
// Check for an optional sign at the beginning
int start_index = 0;
if (input[0] == '+' || input[0] == '-')
{
if (len == 1)
{
return false; // Sign alone is not a number
}
start_index = 1;
}
// Loop through the characters to check if they are all digits
for (size_t i = start_index; i < len; i++)
{
if (!isdigit((unsigned char)input[i]))
{
return false; // Found a non-digit character
}
}
return true; // All characters are digits
}
// char *create_string(const Student students[], int size, int targetId)
// {
// for (int i = 0; i < size; i++)
// {
// // Compare the target ID with the current student's ID
// if (students[i].rollNumber == targetId)
// {
// return i; // Return the index if a match is found
// }
// }
// return -1; // Return -1 if no match is found after traver
// }
int searchStudent(Student *student, int size, int searchId)
{
for (int i = 0; i < size; i++)
{
if (student[i].rollNumber == searchId)
{ // Compare the 'rollNumber' field
printf("Product found at index %d: Roll Number=%d, Name=%s, Marks=%.2f\n",
i,
student[i].rollNumber,
student[i].name,
student[i].marks);
return 1;
}
}
return -1; // Return -1 if not found
}