Darekdari / C++ Projects

C++ Project for Beginners: Student Management System

Create a system to store and manage student information in a school. Users can add students, list them, search by ID, name or grade, update contact details or grade, and delete records. It is a classic C++ project for beginners that practices classes, vectors and input validation. Read the code, try the live demo, then add the optional enhancements.

Classes & vectorsLive demoPortfolio readyBeginner friendly
01 / Overview

How the C++ Student Management System Works

The project has two classes. Student holds the data (ID, name, age, grade, contact). StudentManagementSystem keeps a std::vector<Student> and provides the operations. A text menu in main() is the user interface.

Requirements covered

  • Add students with ID, name, age, grade and contact (duplicate IDs are rejected).
  • List all students with their details.
  • Search by student ID, name or grade.
  • Update the contact details and grade.
  • Delete a student record.
  • Validate input so letters or out-of-range numbers do not break the program.
What changed from the original: the original code did not compile and had no menu. It was missing #include <algorithm>, it passed getContact() (a temporary copy) to getline, and it called std::remove on Student objects that have no operator==. It also hard-coded one update and one delete instead of a user interface, and had no search. This version fixes all of that with setters, remove_if with a lambda, input helpers and a menu.
02 / The Student class

Step 1: Helpers and the Student Class

Start with the includes, two input helpers and the Student class. readInt() keeps asking until the user enters a number in range, and readLine() refuses empty text. The setters let the system update a student without touching private data directly.

main.cpp
#include <algorithm>
#include <cctype>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <string>
#include <vector>

// Read a non-empty line of text
std::string readLine(const std::string& prompt) {
    std::string line;
    do {
        std::cout << prompt;
        if (!std::getline(std::cin, line)) std::exit(0); // input closed
    } while (line.empty());
    return line;
}

// Read an integer between min and max
int readInt(const std::string& prompt, int min, int max) {
    int value;
    while (true) {
        std::cout << prompt;
        if (std::cin >> value && value >= min && value <= max) {
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            return value;
        }
        if (std::cin.eof()) std::exit(0);
        std::cout << "Please enter a number between " << min << " and " << max << ".\n";
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }
}

std::string toLower(std::string text) {
    std::transform(text.begin(), text.end(), text.begin(),
                   [](unsigned char c) { return std::tolower(c); });
    return text;
}

class Student {
private:
    std::string studentID;
    std::string name;
    int age;
    int grade;
    std::string contact;

public:
    Student(const std::string& studentID, const std::string& name, int age, int grade,
            const std::string& contact)
        : studentID(studentID), name(name), age(age), grade(grade), contact(contact) {}

    std::string getStudentID() const { return studentID; }
    std::string getName() const { return name; }
    int getAge() const { return age; }
    int getGrade() const { return grade; }
    std::string getContact() const { return contact; }

    void setGrade(int newGrade) { grade = newGrade; }
    void setContact(const std::string& newContact) { contact = newContact; }

    void displayDetails() const {
        std::cout << "Student ID: " << studentID << std::endl;
        std::cout << "Name: " << name << std::endl;
        std::cout << "Age: " << age << std::endl;
        std::cout << "Grade: " << grade << std::endl;
        std::cout << "Contact: " << contact << std::endl;
    }
};
03 / The system class

Step 2: The StudentManagementSystem Class

This class owns the vector. addStudent() rejects duplicate IDs. findStudentByID() returns a pointer, or nullptr when nothing matches. Search by name ignores case and matches part of the name. Deleting uses the erase-remove idiom: remove_if moves the students you keep to the front, and erase cuts off the rest.

main.cpp (continued)
class StudentManagementSystem {
private:
    std::vector<Student> students;

public:
    bool addStudent(const Student& student) {
        if (findStudentByID(student.getStudentID()) != nullptr) {
            return false; // ID already exists
        }
        students.push_back(student);
        return true;
    }

    void displayAllStudents() const {
        if (students.empty()) {
            std::cout << "No students in the system.\n";
            return;
        }
        std::cout << "Student List (" << students.size() << "):" << std::endl;
        for (const auto& student : students) {
            std::cout << "---------------------" << std::endl;
            student.displayDetails();
        }
        std::cout << "---------------------" << std::endl;
    }

    Student* findStudentByID(const std::string& studentID) {
        for (auto& student : students) {
            if (student.getStudentID() == studentID) {
                return &student;
            }
        }
        return nullptr;
    }

    std::vector<Student> searchByName(const std::string& text) const {
        std::vector<Student> found;
        const std::string needle = toLower(text);
        for (const auto& student : students) {
            if (toLower(student.getName()).find(needle) != std::string::npos) {
                found.push_back(student);
            }
        }
        return found;
    }

    std::vector<Student> searchByGrade(int grade) const {
        std::vector<Student> found;
        for (const auto& student : students) {
            if (student.getGrade() == grade) {
                found.push_back(student);
            }
        }
        return found;
    }

    bool updateStudent(const std::string& studentID) {
        Student* student = findStudentByID(studentID);
        if (student == nullptr) return false;

        std::cout << "Current contact: " << student->getContact() << std::endl;
        student->setContact(readLine("Enter updated contact details: "));
        std::cout << "Current grade: " << student->getGrade() << std::endl;
        student->setGrade(readInt("Enter updated grade (1-12): ", 1, 12));
        return true;
    }

    bool deleteStudent(const std::string& studentID) {
        auto newEnd = std::remove_if(students.begin(), students.end(),
            [&](const Student& s) { return s.getStudentID() == studentID; });
        if (newEnd == students.end()) return false; // nothing removed
        students.erase(newEnd, students.end());
        return true;
    }
};
04 / The menu

Step 3: The Menu and main()

The user interface is a loop that shows a menu and runs the chosen action until the user picks 0. Each action asks for input with the helpers from Step 1, so bad input is handled in one place. Compile with g++ -std=c++11 main.cpp -o students and run ./students.

main.cpp (continued)
void printResults(const std::vector<Student>& results) {
    if (results.empty()) {
        std::cout << "No students found.\n";
        return;
    }
    for (const auto& student : results) {
        std::cout << "---------------------" << std::endl;
        student.displayDetails();
    }
}

int main() {
    StudentManagementSystem sms;

    // Sample data
    sms.addStudent(Student("001", "John Doe", 18, 12, "john.doe@example.com"));
    sms.addStudent(Student("002", "Jane Smith", 17, 11, "jane.smith@example.com"));
    sms.addStudent(Student("003", "Robert Johnson", 16, 10, "robert.johnson@example.com"));

    int choice;
    do {
        std::cout << "\n=== Student Management System ===\n"
                  << "1. Add student\n2. Show all students\n3. Search students\n"
                  << "4. Update student\n5. Delete student\n0. Exit\n";
        choice = readInt("Choose an option: ", 0, 5);

        switch (choice) {
        case 1: {
            std::string id = readLine("Student ID: ");
            std::string name = readLine("Name: ");
            int age = readInt("Age (3-100): ", 3, 100);
            int grade = readInt("Grade (1-12): ", 1, 12);
            std::string contact = readLine("Contact: ");
            if (sms.addStudent(Student(id, name, age, grade, contact))) {
                std::cout << "Student added successfully!\n";
            } else {
                std::cout << "A student with that ID already exists.\n";
            }
            break;
        }
        case 2:
            sms.displayAllStudents();
            break;
        case 3: {
            std::cout << "Search by: 1) ID  2) Name  3) Grade\n";
            int by = readInt("Choose: ", 1, 3);
            if (by == 1) {
                const Student* s = sms.findStudentByID(readLine("Student ID: "));
                if (s != nullptr) s->displayDetails();
                else std::cout << "Student not found in the system.\n";
            } else if (by == 2) {
                printResults(sms.searchByName(readLine("Name contains: ")));
            } else {
                printResults(sms.searchByGrade(readInt("Grade (1-12): ", 1, 12)));
            }
            break;
        }
        case 4:
            if (sms.updateStudent(readLine("Student ID to update: "))) {
                std::cout << "Student details updated successfully!\n";
            } else {
                std::cout << "Student not found in the system.\n";
            }
            break;
        case 5:
            if (sms.deleteStudent(readLine("Student ID to delete: "))) {
                std::cout << "Student record deleted successfully!\n";
            } else {
                std::cout << "Student not found in the system.\n";
            }
            break;
        }
    } while (choice != 0);

    std::cout << "Goodbye!\n";
    return 0;
}
05 / Try it

Live Demo: Manage Students in Your Browser

This demo follows the same rules as the C++ code: unique IDs, validated input, search by ID, name or grade, and updating only the contact and grade. Data lives in memory, so a refresh resets it.

IDNameAgeGradeContactActions
0Students
–Avg age
–Avg grade
06 / Enhancements

Optional Enhancements with Solutions

The demo above already shows statistics and sorting. Here is how to add each optional feature to the C++ version. Try each one yourself first.

1. Statistics: average age and grade

Add this method to StudentManagementSystem and call it from a new menu option.

solution.cpp
void displayStatistics() const {
    if (students.empty()) {
        std::cout << "No students in the system.\n";
        return;
    }
    double ageSum = 0, gradeSum = 0;
    for (const auto& s : students) {
        ageSum += s.getAge();
        gradeSum += s.getGrade();
    }
    std::cout << "Students: " << students.size() << "\n"
              << "Average age: " << ageSum / students.size() << "\n"
              << "Average grade: " << gradeSum / students.size() << "\n";
}
2. Reports: students in an age range

Return the students whose age is between two values. A report by grade is already searchByGrade().

solution.cpp
std::vector<Student> reportByAgeRange(int minAge, int maxAge) const {
    std::vector<Student> found;
    for (const auto& s : students) {
        if (s.getAge() >= minAge && s.getAge() <= maxAge) {
            found.push_back(s);
        }
    }
    return found;
}
3. Sort by ID or name

Use std::sort with a lambda that compares the field you want.

solution.cpp
void sortByName() {
    std::sort(students.begin(), students.end(),
        [](const Student& a, const Student& b) { return a.getName() < b.getName(); });
}

void sortByID() {
    std::sort(students.begin(), students.end(),
        [](const Student& a, const Student& b) { return a.getStudentID() < b.getStudentID(); });
}
4. Administrator login

Ask for a password before add, update and delete. This is a learning example only: real systems never store a plain-text password in the source code.

solution.cpp
bool loginAsAdmin() {
    std::string password = readLine("Admin password: ");
    return password == "admin123"; // demo only
}

// in main(), before cases 1, 4 and 5:
// if (!loginAsAdmin()) { std::cout << "Access denied.\n"; break; }

Looking for more C++ projects? Browse our C++ projects and the free practice sets.

07 / FAQ

Frequently Asked Questions About the C++ Student Management System

How do you delete an element from a vector in C++?

Use the erase-remove idiom: std::remove_if moves the elements you want to keep to the front and returns the new end, then vector::erase removes the leftover tail. Step 2 uses it to delete a student by ID.

Why does the original student management code not compile?

It misses #include <algorithm>, passes the return value of getContact() (a temporary copy) to getline, and calls std::remove on Student objects that have no operator==. The version here uses setters and remove_if with a lambda.

How do you validate user input in C++?

Check that cin >> value succeeded and that the value is in range. If not, call cin.clear() and cin.ignore() to discard the bad input, then ask again, as readInt() does in Step 1.

How long does it take to learn C++?

Most beginners learn the basics (variables, functions, loops, classes) in a few months of regular practice. Small projects like this one speed it up.

Conclusion

Keep Building with C++

You built a student management system with classes, a vector, input validation and a text menu. Next, save the records to a file so they survive after the program closes, or add a search by contact. Small projects like this are the fastest way to learn C++.

Learn more

More info about us

Scroll to Top