Lesson 59 of 60 – C++ Mini Project
98%

C++ Mini Project – Student Management System

In this lesson, we will build a simple Student Management System using C++. This project combines several concepts learned throughout the C++ tutorial, including variables, input/output, conditions, loops, functions, structures, vectors, and file handling.

Note: The purpose of this mini project is to practice C++ concepts together in one practical application. The project uses a text file to save student records.

1. Project Introduction

Our project will manage basic student information.

The application will allow the user to:

  • Add a student
  • Display all students
  • Search for a student
  • Update student information
  • Delete a student
  • Save records to a file
  • Load records from a file
  • Exit the application

2. Project Features

The project will have a menu-driven interface.

===== Student Management System =====

1. Add Student
2. Display Students
3. Search Student
4. Update Student
5. Delete Student
6. Exit

The user selects an option and the program performs the corresponding operation.

3. Concepts Used

This project combines several C++ concepts:

  • Variables
  • Data types
  • Input and output
  • if-else statements
  • switch statements
  • Loops
  • Functions
  • Structures
  • Vectors
  • Strings
  • File handling

4. Creating the Student Structure

A structure can be used to group student information together.

struct Student {

    int id;
    std::string name;
    int age;
    float marks;
};

Each Student object contains an ID, name, age, and marks.

5. Creating a Vector of Students

A vector can store multiple student objects.

std::vector<Student> students;

We can add students dynamically using push_back().

Student s;

s.id = 101;
s.name = "Rahul";
s.age = 20;
s.marks = 85.5;

students.push_back(s);

6. Adding a Student

Create a function to take student information from the user.

void addStudent(
    std::vector<Student>& students
) {

    Student s;

    std::cout <<
        "Enter ID: ";

    std::cin >>
        s.id;

    std::cin.ignore();

    std::cout <<
        "Enter Name: ";

    std::getline(
        std::cin,
        s.name
    );

    std::cout <<
        "Enter Age: ";

    std::cin >>
        s.age;

    std::cout <<
        "Enter Marks: ";

    std::cin >>
        s.marks;

    students.push_back(s);

    std::cout <<
        "Student added successfully.\n";
}

7. Displaying Students

A range-based for loop can display all student records.

void displayStudents(
    const std::vector<Student>& students
) {

    for(
        const Student& s :
        students
    ) {

        std::cout <<
            "ID: "
            << s.id
            << std::endl;

        std::cout <<
            "Name: "
            << s.name
            << std::endl;

        std::cout <<
            "Age: "
            << s.age
            << std::endl;

        std::cout <<
            "Marks: "
            << s.marks
            << std::endl;

        std::cout <<
            "-----------------\n";
    }
}

8. Searching for a Student

We can search for a student using the student ID.

void searchStudent(
    const std::vector<Student>& students
) {

    int id;

    std::cout <<
        "Enter Student ID: ";

    std::cin >>
        id;

    for(
        const Student& s :
        students
    ) {

        if(s.id == id) {

            std::cout <<
                "Student Found\n";

            std::cout <<
                "Name: "
                << s.name
                << std::endl;

            std::cout <<
                "Marks: "
                << s.marks
                << std::endl;

            return;
        }
    }

    std::cout <<
        "Student not found.\n";
}

9. Updating a Student

The program can search for a student and update the student's information.

void updateStudent(
    std::vector<Student>& students
) {

    int id;

    std::cout <<
        "Enter Student ID: ";

    std::cin >>
        id;

    for(
        Student& s :
        students
    ) {

        if(s.id == id) {

            std::cin.ignore();

            std::cout <<
                "Enter New Name: ";

            std::getline(
                std::cin,
                s.name
            );

            std::cout <<
                "Enter New Age: ";

            std::cin >>
                s.age;

            std::cout <<
                "Enter New Marks: ";

            std::cin >>
                s.marks;

            std::cout <<
                "Student updated.\n";

            return;
        }
    }

    std::cout <<
        "Student not found.\n";
}

10. Deleting a Student

The erase() function can remove a student from the vector.

void deleteStudent(
    std::vector<Student>& students
) {

    int id;

    std::cout <<
        "Enter Student ID: ";

    std::cin >>
        id;

    for(
        auto it = students.begin();
        it != students.end();
        ++it
    ) {

        if(it->id == id) {

            students.erase(it);

            std::cout <<
                "Student deleted.\n";

            return;
        }
    }

    std::cout <<
        "Student not found.\n";
}

11. Opening a Data File

The project can store student information in a text file.

#include <fstream>

std::ofstream file(
    "students.txt"
);

The file can be used to save student records so that the information is available when the program runs again.

12. Saving Students to a File

A function can write all student records to a file.

void saveStudents(
    const std::vector<Student>& students
) {

    std::ofstream file(
        "students.txt"
    );

    for(
        const Student& s :
        students
    ) {

        file <<
            s.id << "|"
            << s.name << "|"
            << s.age << "|"
            << s.marks
            << "\n";
    }

    file.close();
}

The pipe character is used here as a simple separator between fields.

13. Loading Students from a File

The saved records can be loaded when the program starts.

void loadStudents(
    std::vector<Student>& students
) {

    std::ifstream file(
        "students.txt"
    );

    Student s;

    std::string separator;

    while(
        file >>
        s.id
    ) {

        file.ignore(
            1,
            '|'
        );

        std::getline(
            file,
            s.name,
            '|'
        );

        file >>
            s.age;

        file.ignore(
            1,
            '|'
        );

        file >>
            s.marks;

        students.push_back(s);
    }

    file.close();
}

14. Creating the Main Menu

The menu provides an easy way for the user to select an operation.

void showMenu() {

    std::cout <<
        "\n===== Student Management System =====\n";

    std::cout <<
        "1. Add Student\n";

    std::cout <<
        "2. Display Students\n";

    std::cout <<
        "3. Search Student\n";

    std::cout <<
        "4. Update Student\n";

    std::cout <<
        "5. Delete Student\n";

    std::cout <<
        "6. Exit\n";
}

15. Using a while Loop

A while loop can keep the application running until the user selects Exit.

int choice = 0;

while(choice != 6) {

    showMenu();

    std::cout <<
        "Enter choice: ";

    std::cin >>
        choice;

}

The loop continues until choice becomes 6.

16. Using switch for Menu Options

A switch statement is suitable for handling the menu choices.

switch(choice) {

case 1:
    addStudent(students);
    break;

case 2:
    displayStudents(students);
    break;

case 3:
    searchStudent(students);
    break;

case 4:
    updateStudent(students);
    break;

case 5:
    deleteStudent(students);
    break;

case 6:
    std::cout <<
        "Goodbye!";
    break;

default:
    std::cout <<
        "Invalid choice.";
}

17. Complete Menu Logic

The main program can connect the menu, vector, and functions together.

int main() {

    std::vector<Student> students;

    loadStudents(students);

    int choice;

    do {

        showMenu();

        std::cin >>
            choice;

        switch(choice) {

        case 1:
            addStudent(students);
            break;

        case 2:
            displayStudents(students);
            break;

        case 3:
            searchStudent(students);
            break;

        case 4:
            updateStudent(students);
            break;

        case 5:
            deleteStudent(students);
            break;

        case 6:
            saveStudents(students);
            break;

        default:
            std::cout <<
                "Invalid choice.\n";
        }

    } while(choice != 6);

    return 0;
}

18. Complete Program Structure

A clean project can be divided into the following parts:

Headers
    ↓
Student Structure
    ↓
Add Function
    ↓
Display Function
    ↓
Search Function
    ↓
Update Function
    ↓
Delete Function
    ↓
Save Function
    ↓
Load Function
    ↓
Menu Function
    ↓
main()

Dividing the program into functions makes the code easier to read and maintain.

19. Adding Input Validation

Programs should validate user input whenever possible.

if(s.age < 1 || s.age > 100) {

    std::cout <<
        "Invalid age.";

    return;
}

if(
    s.marks < 0 ||
    s.marks > 100
) {

    std::cout <<
        "Invalid marks.";

    return;
}

This prevents obviously invalid values from being stored.

20. Preventing Duplicate Student IDs

Student IDs should normally be unique. We can check whether an ID already exists before adding a new student.

bool idExists(
    const std::vector<Student>& students,
    int id
) {

    for(
        const Student& s :
        students
    ) {

        if(s.id == id) {

            return true;
        }
    }

    return false;
}

The function returns true if the ID already exists.

21. Improved Add Student Function

void addStudent(
    std::vector<Student>& students
) {

    Student s;

    std::cout <<
        "Enter ID: ";

    std::cin >>
        s.id;

    if(
        idExists(
            students,
            s.id
        )
    ) {

        std::cout <<
            "ID already exists.\n";

        return;
    }

    std::cin.ignore();

    std::cout <<
        "Enter Name: ";

    std::getline(
        std::cin,
        s.name
    );

    std::cout <<
        "Enter Age: ";

    std::cin >>
        s.age;

    std::cout <<
        "Enter Marks: ";

    std::cin >>
        s.marks;

    students.push_back(s);

    std::cout <<
        "Student added successfully.\n";
}

22. Sample Program Output

===== Student Management System =====

1. Add Student
2. Display Students
3. Search Student
4. Update Student
5. Delete Student
6. Exit

Enter choice: 1

Enter ID: 101
Enter Name: Rahul Kumar
Enter Age: 20
Enter Marks: 87

Student added successfully.

Enter choice: 2

ID: 101
Name: Rahul Kumar
Age: 20
Marks: 87
-----------------

This is an example of how the menu-driven application can interact with the user.

23. Searching Example

Enter choice: 3

Enter Student ID: 101

Student Found

Name: Rahul Kumar
Marks: 87

The search function compares the entered ID with the IDs stored in the vector.

24. Updating Example

Enter choice: 4

Enter Student ID: 101

Enter New Name: Rahul Singh
Enter New Age: 21
Enter New Marks: 91

Student updated.

The existing student object is modified using a reference.

25. Deleting Example

Enter choice: 5

Enter Student ID: 101

Student deleted.

The matching element is removed from the vector using erase().

26. Complete Mini Project Code

The following program combines the main concepts into one complete student management application.

#include <iostream>
#include <fstream>
#include <string>
#include <vector>

struct Student {

    int id;
    std::string name;
    int age;
    float marks;
};

bool idExists(
    const std::vector<Student>& students,
    int id
) {

    for(
        const Student& s :
        students
    ) {

        if(s.id == id) {

            return true;
        }
    }

    return false;
}

void addStudent(
    std::vector<Student>& students
) {

    Student s;

    std::cout <<
        "Enter ID: ";

    std::cin >>
        s.id;

    if(
        idExists(
            students,
            s.id
        )
    ) {

        std::cout <<
            "ID already exists.\n";

        return;
    }

    std::cin.ignore();

    std::cout <<
        "Enter Name: ";

    std::getline(
        std::cin,
        s.name
    );

    std::cout <<
        "Enter Age: ";

    std::cin >>
        s.age;

    std::cout <<
        "Enter Marks: ";

    std::cin >>
        s.marks;

    students.push_back(s);

    std::cout <<
        "Student added successfully.\n";
}

void displayStudents(
    const std::vector<Student>& students
) {

    if(students.empty()) {

        std::cout <<
            "No students found.\n";

        return;
    }

    for(
        const Student& s :
        students
    ) {

        std::cout <<
            "\nID: "
            << s.id;

        std::cout <<
            "\nName: "
            << s.name;

        std::cout <<
            "\nAge: "
            << s.age;

        std::cout <<
            "\nMarks: "
            << s.marks;

        std::cout <<
            "\n-----------------\n";
    }
}

void searchStudent(
    const std::vector<Student>& students
) {

    int id;

    std::cout <<
        "Enter Student ID: ";

    std::cin >>
        id;

    for(
        const Student& s :
        students
    ) {

        if(s.id == id) {

            std::cout <<
                "\nStudent Found\n";

            std::cout <<
                "Name: "
                << s.name
                << std::endl;

            std::cout <<
                "Age: "
                << s.age
                << std::endl;

            std::cout <<
                "Marks: "
                << s.marks
                << std::endl;

            return;
        }
    }

    std::cout <<
        "Student not found.\n";
}

void updateStudent(
    std::vector<Student>& students
) {

    int id;

    std::cout <<
        "Enter Student ID: ";

    std::cin >>
        id;

    for(
        Student& s :
        students
    ) {

        if(s.id == id) {

            std::cin.ignore();

            std::cout <<
                "Enter New Name: ";

            std::getline(
                std::cin,
                s.name
            );

            std::cout <<
                "Enter New Age: ";

            std::cin >>
                s.age;

            std::cout <<
                "Enter New Marks: ";

            std::cin >>
                s.marks;

            std::cout <<
                "Student updated.\n";

            return;
        }
    }

    std::cout <<
        "Student not found.\n";
}

void deleteStudent(
    std::vector<Student>& students
) {

    int id;

    std::cout <<
        "Enter Student ID: ";

    std::cin >>
        id;

    for(
        auto it = students.begin();
        it != students.end();
        ++it
    ) {

        if(it->id == id) {

            students.erase(it);

            std::cout <<
                "Student deleted.\n";

            return;
        }
    }

    std::cout <<
        "Student not found.\n";
}

void saveStudents(
    const std::vector<Student>& students
) {

    std::ofstream file(
        "students.txt"
    );

    if(!file) {

        std::cout <<
            "Unable to save data.\n";

        return;
    }

    for(
        const Student& s :
        students
    ) {

        file <<
            s.id << "|"
            << s.name << "|"
            << s.age << "|"
            << s.marks
            << "\n";
    }
}

void loadStudents(
    std::vector<Student>& students
) {

    std::ifstream file(
        "students.txt"
    );

    if(!file) {

        return;
    }

    Student s;

    while(
        file >>
        s.id
    ) {

        file.ignore(
            1,
            '|'
        );

        std::getline(
            file,
            s.name,
            '|'
        );

        file >>
            s.age;

        file.ignore(
            1,
            '|'
        );

        file >>
            s.marks;

        students.push_back(s);
    }
}

void showMenu() {

    std::cout <<
        "\n===== Student Management System =====\n";

    std::cout <<
        "1. Add Student\n";

    std::cout <<
        "2. Display Students\n";

    std::cout <<
        "3. Search Student\n";

    std::cout <<
        "4. Update Student\n";

    std::cout <<
        "5. Delete Student\n";

    std::cout <<
        "6. Exit\n";
}

int main() {

    std::vector<Student> students;

    loadStudents(students);

    int choice;

    do {

        showMenu();

        std::cout <<
            "Enter choice: ";

        std::cin >>
            choice;

        switch(choice) {

        case 1:
            addStudent(students);
            break;

        case 2:
            displayStudents(students);
            break;

        case 3:
            searchStudent(students);
            break;

        case 4:
            updateStudent(students);
            break;

        case 5:
            deleteStudent(students);
            break;

        case 6:
            saveStudents(students);
            std::cout <<
                "Data saved. Goodbye!\n";
            break;

        default:
            std::cout <<
                "Invalid choice.\n";
        }

    } while(choice != 6);

    return 0;
}

27. Understanding the Complete Project

The complete project follows this basic flow:

Program Starts
      ↓
Load Existing Students
      ↓
Display Menu
      ↓
User Selects Option
      ↓
Perform Operation
      ↓
Display Menu Again
      ↓
User Selects Exit
      ↓
Save Students
      ↓
Program Ends

This structure demonstrates how individual C++ concepts can be combined to create a useful application.

28. Possible Improvements

The basic project can be expanded with many additional features.

  • Student course information
  • Phone number
  • Email address
  • Attendance management
  • Fee management
  • Grade calculation
  • Sorting students by marks
  • Searching by name
  • Separate admin and student menus
  • Database integration
  • Better input validation
  • CSV or structured data storage

These improvements can turn the basic project into a larger student management application.

29. Project Learning Outcomes

After completing this project, you should understand how to combine multiple C++ concepts.

  • How to define and use structures.
  • How to store objects in vectors.
  • How to create reusable functions.
  • How to use loops and switch statements.
  • How to search and modify vector data.
  • How to use file input and output.
  • How to build a menu-driven program.
  • How to organize a small real-world application.

30. C++ Mini Project – Final Summary

The Student Management System is a practical example of combining fundamental and intermediate C++ concepts.

Concept Used For
Structure Representing student information.
Vector Storing multiple students.
Functions Separating application operations.
Switch Handling menu choices.
Loops Repeating the application menu and processing records.
File Handling Saving and loading student records.
Search Finding students by ID.
erase() Deleting a student from the vector.

The most important lesson is that programming concepts become more useful when they are combined to solve practical problems.

📌 Key Points

  • A mini project helps combine multiple C++ concepts in one application.
  • Structures can represent real-world records such as students.
  • Vectors can store multiple student objects.
  • Functions make the program modular and easier to maintain.
  • Switch statements are useful for menu-driven applications.
  • File handling allows student records to be stored permanently.
  • Search, update, and delete operations can be implemented using vectors.
  • Input validation helps prevent invalid data.
  • Unique student IDs can be checked before adding records.
  • The project can be expanded into a larger real-world application.

🧠 Quick Quiz

Question: Which C++ container is used in this mini project to store multiple student records dynamically?