Day 14: Mini Project - Student Record Management System#
Overview#
Congratulations on making it to Day 14! Today we’ll build a complete, practical application that integrates everything you’ve learned over the past two weeks. We’ll create a Student Record Management System with features to add, view, search, and manage student records.
What We’ll Build#
A menu-driven application that allows users to:
- Add student records
- View all students
- Search for specific students
- Update student information
- Delete student records
- Calculate statistics
- Persist data to a file
Program Design#
Data Structure#
typedef struct {
int id;
char name[50];
char email[50];
float gpa;
int semester;
} Student;Key Features#
- Menu System - User-friendly interface
- Dynamic Memory - Handle variable number of students
- File I/O - Save/load data
- Data Validation - Check user input
- Searching & Sorting - Find specific students
- Statistics - Calculate averages and totals
Complete Implementation#
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_NAME 50
#define MAX_EMAIL 50
#define DATA_FILE "students.dat"
// Student structure
typedef struct {
int id;
char name[MAX_NAME];
char email[MAX_EMAIL];
float gpa;
int semester;
} Student;
// Global variables
Student* students = NULL;
int total_students = 0;
// Function prototypes
void displayMenu();
void addStudent();
void viewAllStudents();
void searchStudent();
void updateStudent();
void deleteStudent();
void calculateStatistics();
void saveToFile();
void loadFromFile();
void freeMemory();
// ============================================
// MAIN FUNCTION
// ============================================
int main() {
int choice;
// Load existing data
loadFromFile();
printf("=== Student Record Management System ===\n\n");
while (1) {
displayMenu();
printf("\nEnter your choice: ");
scanf("%d", &choice);
getchar(); // Consume newline
switch (choice) {
case 1:
addStudent();
break;
case 2:
viewAllStudents();
break;
case 3:
searchStudent();
break;
case 4:
updateStudent();
break;
case 5:
deleteStudent();
break;
case 6:
calculateStatistics();
break;
case 7:
saveToFile();
printf("Data saved successfully!\n");
break;
case 8:
saveToFile();
freeMemory();
printf("Program terminated. Data saved.\n");
return 0;
default:
printf("Invalid choice! Please try again.\n");
}
printf("\nPress Enter to continue...");
getchar();
printf("\n");
}
return 0;
}
// ============================================
// DISPLAY MENU
// ============================================
void displayMenu() {
printf("\n=== Main Menu ===\n");
printf("1. Add Student\n");
printf("2. View All Students\n");
printf("3. Search Student\n");
printf("4. Update Student\n");
printf("5. Delete Student\n");
printf("6. Calculate Statistics\n");
printf("7. Save Data\n");
printf("8. Exit\n");
}
// ============================================
// ADD STUDENT
// ============================================
void addStudent() {
printf("\n=== Add New Student ===\n");
// Resize array
Student* temp = realloc(students, (total_students + 1) * sizeof(Student));
if (temp == NULL) {
printf("Error: Memory allocation failed!\n");
return;
}
students = temp;
Student* new_student = &students[total_students];
printf("Enter Student ID: ");
scanf("%d", &new_student->id);
getchar();
// Check for duplicate ID
for (int i = 0; i < total_students; i++) {
if (students[i].id == new_student->id) {
printf("Error: Student ID already exists!\n");
return;
}
}
printf("Enter Name: ");
fgets(new_student->name, MAX_NAME, stdin);
new_student->name[strcspn(new_student->name, "\n")] = '\0';
printf("Enter Email: ");
fgets(new_student->email, MAX_EMAIL, stdin);
new_student->email[strcspn(new_student->email, "\n")] = '\0';
printf("Enter GPA (0.0 - 4.0): ");
scanf("%f", &new_student->gpa);
printf("Enter Semester: ");
scanf("%d", &new_student->semester);
// Validate GPA
if (new_student->gpa < 0.0 || new_student->gpa > 4.0) {
printf("Error: Invalid GPA!\n");
return;
}
total_students++;
printf("Student added successfully!\n");
}
// ============================================
// VIEW ALL STUDENTS
// ============================================
void viewAllStudents() {
if (total_students == 0) {
printf("\nNo students in the system.\n");
return;
}
printf("\n=== All Students ===\n");
printf("%-5s %-20s %-25s %-6s %-10s\n",
"ID", "Name", "Email", "GPA", "Semester");
printf("================================================================\n");
for (int i = 0; i < total_students; i++) {
printf("%-5d %-20s %-25s %-6.2f %-10d\n",
students[i].id,
students[i].name,
students[i].email,
students[i].gpa,
students[i].semester);
}
}
// ============================================
// SEARCH STUDENT
// ============================================
void searchStudent() {
if (total_students == 0) {
printf("\nNo students in the system.\n");
return;
}
printf("\n=== Search Student ===\n");
int id;
printf("Enter Student ID to search: ");
scanf("%d", &id);
for (int i = 0; i < total_students; i++) {
if (students[i].id == id) {
printf("\n--- Student Found ---\n");
printf("ID: %d\n", students[i].id);
printf("Name: %s\n", students[i].name);
printf("Email: %s\n", students[i].email);
printf("GPA: %.2f\n", students[i].gpa);
printf("Semester: %d\n", students[i].semester);
return;
}
}
printf("Student with ID %d not found.\n", id);
}
// ============================================
// UPDATE STUDENT
// ============================================
void updateStudent() {
if (total_students == 0) {
printf("\nNo students in the system.\n");
return;
}
printf("\n=== Update Student ===\n");
int id;
printf("Enter Student ID to update: ");
scanf("%d", &id);
getchar();
for (int i = 0; i < total_students; i++) {
if (students[i].id == id) {
printf("\nCurrent Information:\n");
printf("Name: %s\n", students[i].name);
printf("Email: %s\n", students[i].email);
printf("GPA: %.2f\n", students[i].gpa);
printf("Semester: %d\n", students[i].semester);
printf("\nEnter new information:\n");
printf("Enter Name: ");
fgets(students[i].name, MAX_NAME, stdin);
students[i].name[strcspn(students[i].name, "\n")] = '\0';
printf("Enter Email: ");
fgets(students[i].email, MAX_EMAIL, stdin);
students[i].email[strcspn(students[i].email, "\n")] = '\0';
printf("Enter GPA: ");
scanf("%f", &students[i].gpa);
printf("Enter Semester: ");
scanf("%d", &students[i].semester);
printf("Student updated successfully!\n");
return;
}
}
printf("Student with ID %d not found.\n", id);
}
// ============================================
// DELETE STUDENT
// ============================================
void deleteStudent() {
if (total_students == 0) {
printf("\nNo students in the system.\n");
return;
}
printf("\n=== Delete Student ===\n");
int id;
printf("Enter Student ID to delete: ");
scanf("%d", &id);
for (int i = 0; i < total_students; i++) {
if (students[i].id == id) {
// Shift remaining students
for (int j = i; j < total_students - 1; j++) {
students[j] = students[j + 1];
}
total_students--;
// Resize array
if (total_students > 0) {
Student* temp = realloc(students, total_students * sizeof(Student));
if (temp != NULL) {
students = temp;
}
} else {
free(students);
students = NULL;
}
printf("Student deleted successfully!\n");
return;
}
}
printf("Student with ID %d not found.\n", id);
}
// ============================================
// CALCULATE STATISTICS
// ============================================
void calculateStatistics() {
if (total_students == 0) {
printf("\nNo students in the system.\n");
return;
}
printf("\n=== Statistics ===\n");
float total_gpa = 0;
float max_gpa = students[0].gpa;
float min_gpa = students[0].gpa;
for (int i = 0; i < total_students; i++) {
total_gpa += students[i].gpa;
if (students[i].gpa > max_gpa) {
max_gpa = students[i].gpa;
}
if (students[i].gpa < min_gpa) {
min_gpa = students[i].gpa;
}
}
float average_gpa = total_gpa / total_students;
printf("Total Students: %d\n", total_students);
printf("Average GPA: %.2f\n", average_gpa);
printf("Highest GPA: %.2f\n", max_gpa);
printf("Lowest GPA: %.2f\n", min_gpa);
}
// ============================================
// SAVE TO FILE
// ============================================
void saveToFile() {
FILE* file = fopen(DATA_FILE, "w");
if (file == NULL) {
printf("Error: Cannot create file!\n");
return;
}
fprintf(file, "%d\n", total_students);
for (int i = 0; i < total_students; i++) {
fprintf(file, "%d %s %s %.2f %d\n",
students[i].id,
students[i].name,
students[i].email,
students[i].gpa,
students[i].semester);
}
fclose(file);
}
// ============================================
// LOAD FROM FILE
// ============================================
void loadFromFile() {
FILE* file = fopen(DATA_FILE, "r");
if (file == NULL) {
return; // File doesn't exist yet
}
int count;
if (fscanf(file, "%d\n", &count) != 1) {
fclose(file);
return;
}
students = malloc(count * sizeof(Student));
if (students == NULL) {
printf("Error: Memory allocation failed!\n");
fclose(file);
return;
}
for (int i = 0; i < count; i++) {
if (fscanf(file, "%d %s %s %f %d\n",
&students[i].id,
students[i].name,
students[i].email,
&students[i].gpa,
&students[i].semester) != 5) {
break;
}
total_students++;
}
fclose(file);
}
// ============================================
// FREE MEMORY
// ============================================
void freeMemory() {
if (students != NULL) {
free(students);
students = NULL;
}
total_students = 0;
}How to Use the Program#
Compile:
gcc student_system.c -o student_systemRun:
./student_systemExample Session:
=== Main Menu === 1. Add Student 2. View All Students 3. Search Student 4. Update Student 5. Delete Student 6. Calculate Statistics 7. Save Data 8. Exit Enter your choice: 1 === Add New Student === Enter Student ID: 101 Enter Name: Alice Johnson Enter Email: alice@university.edu Enter GPA (0.0 - 4.0): 3.8 Enter Semester: 3 Student added successfully!
Key Features Explained#
1. Dynamic Memory Management#
- Uses
malloc(),realloc(), andfree() - Grows array as students are added
- Shrinks array when students are deleted
2. Data Persistence#
loadFromFile()- Load data on startupsaveToFile()- Save data to file- Data survives program restarts
3. Input Validation#
- Checks for duplicate IDs
- Validates GPA range (0.0 - 4.0)
- Uses
fgets()for safe string input
4. User-Friendly Interface#
- Clear menu system
- Formatted output tables
- Error messages for invalid operations
5. Error Handling#
- Checks for NULL pointers
- Validates memory allocation
- Handles file operations safely
Improvements You Can Make#
- Sorting - Sort students by name or GPA
- Advanced Search - Search by name or email
- GPA Grades - Display letter grades
- Semester Filters - View students by semester
- Export - Export data to CSV format
- Import - Import data from files
- Backup - Create automatic backups
- Encryption - Secure sensitive data
Congratulations!#
You’ve completed the 14-day C programming course! 🎉
What You’ve Learned#
✅ Set up a professional C development environment
✅ Mastered fundamental C syntax and concepts
✅ Worked with data types, variables, and constants
✅ Used operators and control flow effectively
✅ Created reusable, modular functions
✅ Manipulated arrays and strings
✅ Understood pointers and memory management
✅ Organized data with structures
✅ Allocated and managed dynamic memory
✅ Implemented recursive algorithms
✅ Persisted data using file I/O
✅ Debugged and handled errors professionally
✅ Built a complete real-world application
Your Next Steps#
Short Term#
- Practice variations of this project
- Solve coding challenges on platforms like:
- LeetCode
- HackerRank
- CodeChef
Medium Term#
- Learn advanced C concepts:
- Linked lists
- Trees
- Graphs
- Algorithms
Long Term#
- Consider learning:
- C++
- Data Structures & Algorithms
- Systems Programming
- Embedded Systems
Resources for Continued Learning#
- Official C Standard: ISO/IEC 9899:2018
- Books:
- “The C Programming Language” by Kernighan & Ritchie
- “C Programming: A Modern Approach” by K.N. King
- Online Platforms:
- GeeksforGeeks
- TutorialsPoint
- W3Schools
- Practice Sites:
- Project Euler
- Codeforces
- SPOJ
Final Tips for Success#
- Keep Coding - The only way to master programming is practice
- Read Others’ Code - Learn from experienced programmers
- Debug Systematically - Use tools, not just print statements
- Understand, Don’t Memorize - Focus on concepts, not syntax
- Build Projects - Apply what you learn to real problems
- Share Your Work - Get feedback from others
- Stay Curious - Always learn new techniques and languages
Feedback & Support#
If you found this course helpful:
- Share it with others learning C
- Practice regularly
- Come back to review concepts as needed
- Challenge yourself with harder projects
Summary#
Congratulations on completing this comprehensive 14-day C programming course! You now have a solid foundation in:
- Core C Programming: Variables, operators, control flow, functions
- Data Structures: Arrays, strings, pointers, structures
- Advanced Concepts: Recursion, dynamic memory, file I/O
- Professional Skills: Debugging, error handling, best practices
- Real-World Application: Complete project management system
You’re ready to tackle real programming challenges and continue your software development journey!
Keep coding, keep learning, and enjoy the programming journey! 🚀
← Back to Day 13 | Back to Course Overview →