All CoursesC++ Programming Course

Demo Projects (Guided)

Step-by-step guided projects with features list, class design, flowcharts, and full working code.

Student Management System

šŸ“‹ Features

  • Add new student (name, roll, marks)
  • View all students
  • Search by roll number
  • Update student marks
  • Delete a student
  • Save/load data from file

šŸ—ļø Class Design

Class: Student
ā”œā”€ā”€ Attributes: name (string), rollNumber (int), marks (double)
ā”œā”€ā”€ Constructor: Student(name, roll, marks)
ā”œā”€ā”€ Methods: display(), getMarks(), setMarks(), getRoll()
└── Friend: operator<< for easy printing

Class: StudentManager
ā”œā”€ā”€ Attributes: students (vector<Student>), filename (string)
ā”œā”€ā”€ Methods: addStudent(), viewAll(), searchByRoll()
ā”œā”€ā”€ Methods: updateMarks(), deleteStudent()
└── Methods: saveToFile(), loadFromFile()

šŸ“Š Flowchart

START
  │
  ā–¼
Load data from file
  │
  ā–¼
ā”Œā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”
│   MAIN MENU     │
│ 1. Add Student  │
│ 2. View All     │
│ 3. Search       │
│ 4. Update Marks │
│ 5. Delete       │
│ 6. Exit         │
ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”¬ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜
         │
    ā”Œā”€ā”€ā”€ā”€ā–¼ā”€ā”€ā”€ā”€ā”
    │ Choice? │──── 1 ──→ Get name, roll, marks → Add to vector → Save
    │         │──── 2 ──→ Loop through vector → Display each
    │         │──── 3 ──→ Get roll → Linear search → Display or "Not found"
    │         │──── 4 ──→ Search → If found → Get new marks → Update → Save
    │         │──── 5 ──→ Search → If found → Remove from vector → Save
    │         │──── 6 ──→ Save to file → EXIT
    ā””ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”€ā”˜

šŸ’» Full Implementation

student_system.cpp
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

class Student {
    string name;
    int rollNumber;
    double marks;
public:
    Student() : name(""), rollNumber(0), marks(0) {}
    Student(string n, int r, double m) : name(n), rollNumber(r), marks(m) {}
    
    int getRoll() const { return rollNumber; }
    double getMarks() const { return marks; }
    void setMarks(double m) { marks = m; }
    
    void display() const {
        cout << "Roll: " << rollNumber 
             << " | Name: " << name 
             << " | Marks: " << marks << endl;
    }
    
    string toFileString() const {
        return name + "|" + to_string(rollNumber) + "|" + to_string(marks);
    }
    
    static Student fromFileString(const string& line) {
        int p1 = line.find("|");
        int p2 = line.find("|", p1 + 1);
        string n = line.substr(0, p1);
        int r = stoi(line.substr(p1 + 1, p2 - p1 - 1));
        double m = stod(line.substr(p2 + 1));
        return Student(n, r, m);
    }
};

class StudentManager {
    vector<Student> students;
    string filename;
    
public:
    StudentManager(string file) : filename(file) { loadFromFile(); }
    
    void addStudent() {
        string name;
        int roll;
        double marks;
        cout << "Enter name: ";
        cin.ignore();
        getline(cin, name);
        cout << "Enter roll number: ";
        cin >> roll;
        
        // Check duplicate
        for (const auto& s : students) {
            if (s.getRoll() == roll) {
                cout << "Roll number already exists!" << endl;
                return;
            }
        }
        
        cout << "Enter marks: ";
        cin >> marks;
        students.push_back(Student(name, roll, marks));
        saveToFile();
        cout << "Student added successfully!" << endl;
    }
    
    void viewAll() const {
        if (students.empty()) {
            cout << "No students found." << endl;
            return;
        }
        cout << "\n--- All Students (" << students.size() << ") ---" << endl;
        for (const auto& s : students) s.display();
    }
    
    void searchByRoll() const {
        int roll;
        cout << "Enter roll number: ";
        cin >> roll;
        for (const auto& s : students) {
            if (s.getRoll() == roll) {
                cout << "\nStudent found:" << endl;
                s.display();
                return;
            }
        }
        cout << "Student not found!" << endl;
    }
    
    void updateMarks() {
        int roll;
        double newMarks;
        cout << "Enter roll number: ";
        cin >> roll;
        for (auto& s : students) {
            if (s.getRoll() == roll) {
                cout << "Current marks: " << s.getMarks() << endl;
                cout << "Enter new marks: ";
                cin >> newMarks;
                s.setMarks(newMarks);
                saveToFile();
                cout << "Marks updated!" << endl;
                return;
            }
        }
        cout << "Student not found!" << endl;
    }
    
    void deleteStudent() {
        int roll;
        cout << "Enter roll number to delete: ";
        cin >> roll;
        for (int i = 0; i < students.size(); i++) {
            if (students[i].getRoll() == roll) {
                students[i].display();
                cout << "Confirm delete? (y/n): ";
                char ch;
                cin >> ch;
                if (ch == 'y' || ch == 'Y') {
                    students.erase(students.begin() + i);
                    saveToFile();
                    cout << "Student deleted!" << endl;
                }
                return;
            }
        }
        cout << "Student not found!" << endl;
    }
    
    void saveToFile() {
        ofstream file(filename);
        for (const auto& s : students) {
            file << s.toFileString() << endl;
        }
        file.close();
    }
    
    void loadFromFile() {
        ifstream file(filename);
        string line;
        students.clear();
        while (getline(file, line)) {
            if (!line.empty()) {
                students.push_back(Student::fromFileString(line));
            }
        }
        file.close();
    }
};

int main() {
    StudentManager manager("students.txt");
    int choice;
    
    do {
        cout << "\n===== STUDENT MANAGEMENT SYSTEM =====" << endl;
        cout << "1. Add Student" << endl;
        cout << "2. View All Students" << endl;
        cout << "3. Search by Roll Number" << endl;
        cout << "4. Update Marks" << endl;
        cout << "5. Delete Student" << endl;
        cout << "6. Exit" << endl;
        cout << "Enter choice: ";
        cin >> choice;
        
        switch (choice) {
            case 1: manager.addStudent(); break;
            case 2: manager.viewAll(); break;
            case 3: manager.searchByRoll(); break;
            case 4: manager.updateMarks(); break;
            case 5: manager.deleteStudent(); break;
            case 6: cout << "Goodbye!" << endl; break;
            default: cout << "Invalid choice!" << endl;
        }
    } while (choice != 6);
    
    return 0;
}