0% found this document useful (0 votes)
18 views10 pages

Final Assignment

The document outlines a C program for a Student Information Management System that allows users to enter, update, and display student records. It includes features for input validation, data entry for grades or feedback, and a menu-driven interface for user interaction. The program utilizes structures and unions to manage student data efficiently.

Uploaded by

eram cuet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views10 pages

Final Assignment

The document outlines a C program for a Student Information Management System that allows users to enter, update, and display student records. It includes features for input validation, data entry for grades or feedback, and a menu-driven interface for user interaction. The program utilizes structures and unions to manage student data efficiently.

Uploaded by

eram cuet
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Student Information Management System

Source Code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef union {
float grade;
char feedback[1000];
} StudentData;

typedef struct {
int studentNumber;
char fullName[100];
int studentID;
int dataChoice; // 1 for grade, 2 for feedback
StudentData data; // Union to store either grade or feedback
} Student;

void enterGrade(Student *s) {


float grade;
do {
printf("Please enter a grade (between 0 and 4.00): ");
scanf("%f", &grade);
getchar(); // Remove newline character
if (grade < 0 || grade > 4.00) {
printf("Invalid grade. Please enter a valid grade between 0 and
4.00.\n");
}
} while (grade < 0 || grade > 4.00);
s->data.grade = grade;
}

void addFeedback(Student *s) {


printf("Enter feedback for the student: ");
fgets(s->data.feedback, 1000, stdin);
size_t len = strlen(s->data.feedback);
if (len > 0 && s->data.feedback[len - 1] == '\n') {
s->data.feedback[len - 1] = '\0'; // Remove newline character
}
}

void gatherAdditionalData(Student *s) {


if (s->dataChoice == 1) {
enterGrade(s);
} else if (s->dataChoice == 2) {
addFeedback(s);
}
}

int validateStudentID(Student *s) {


int id;
do {
printf("Enter student ID: ");
scanf("%d", &id);
getchar();
if (id < 0) {
printf("Negative IDs are not allowed. Please enter a valid
student ID.\n");
} else {
s->studentID = id;
}
} while (id < 0);
return id;
}

void modifyStudentDetails(Student *s) {


printf("Enter student's full name: ");
fgets(s->fullName, 100, stdin);
size_t len = strlen(s->fullName);
if (len > 0 && s->fullName[len - 1] == '\n') {
s->fullName[len - 1] = '\0'; // Remove newline character
}

// ID validation
validateStudentID(s);

printf("Would you like to input grade (1) or feedback (2)? ");


scanf("%d", &s->dataChoice);
getchar(); // Clear leftover newline
gatherAdditionalData(s);
}

void updateStudentInfo(Student *students, int totalStudents) {


int targetID, found = 0;
printf("\nEnter the student ID you wish to update: ");
do {
scanf("%d", &targetID);
getchar();
if (targetID < 0) {
printf("Please provide a valid student ID.\n");
}
} while (targetID < 0);

for (int i = 0; i < totalStudents; i++) {


if (targetID == students[i].studentID) {
printf("Updating details for %s (ID: %d):\n",
students[i].fullName, targetID);
modifyStudentDetails(&students[i]);
found = 1;
break;
}
}

if (found) {
printf("Student details successfully updated!\n");
} else {
printf("No student found with the provided ID.\n");
}
}

void displayStudentRecords(Student *students, int totalStudents) {


for (int i = 0; i < totalStudents; i++) {
printf("\nStudent %d\n", students[i].studentNumber);
printf("Name: %s\n", students[i].fullName);
printf("ID: %d\n", students[i].studentID);
if (students[i].dataChoice == 1) {
printf("Grade: %.2f\n", students[i].data.grade);
} else if (students[i].dataChoice == 2) {
printf("Feedback: %s\n", students[i].data.feedback);
}
}
}

void enterStudentData(Student *students, int totalStudents) {


for (int i = 0; i < totalStudents; i++) {
students[i].studentNumber = i + 1;
printf("\nStudent %d:\n", students[i].studentNumber);

printf("Enter student's full name: ");


fgets(students[i].fullName, 100, stdin);
size_t len = strlen(students[i].fullName);
if (len > 0 && students[i].fullName[len - 1] == '\n') {
students[i].fullName[len - 1] = '\0'; // Remove newline character
}

// Validate student ID
validateStudentID(&students[i]);

printf("Would you like to enter a grade (1) or feedback (2)? ");


scanf("%d", &students[i].dataChoice);
getchar(); // Clear leftover newline

gatherAdditionalData(&students[i]);
}
}
int main() {
int totalStudents;
printf("Enter the number of students: ");
scanf("%d", &totalStudents);
getchar(); // Clear leftover newline

Student *students = (Student *)calloc(totalStudents, sizeof(Student));

// Collect student data


enterStudentData(students, totalStudents);

// Main menu
while (1) {
printf("\nMenu:\n");
printf("1. Display all student records\n");
printf("2. Update student information\n");
printf("3. Exit\n");
printf("Enter your choice: ");
int choice;
scanf("%d", &choice);
if (choice == 3) {
free(students);
return 0;
} else if (choice == 1) {
displayStudentRecords(students, totalStudents);
} else if (choice == 2) {
updateStudentInfo(students, totalStudents);
}
}

free(students);
}

Output:

Input Validation:
Update Feature:

Display Student Information:

You might also like