Thuta Learning
ProjectsProgrammingbeginner

Mini Project: Student Manager - Part 3

Relax. We'll talk through this in plain words — no textbook voice.

What you'll walk away with

  • Apply Mini Project: Student Manager - Part 3 in a hands-on project
  • Write and run the code yourself
  • Build out an entire project step by step

Let's Think About This For a Second

Part 3 is the final stage of the project, where we'll polish the program to near-production quality. First, we'll add try/catch exception handling so the program doesn't crash if non-numeric input is entered for marks. Second, we'll use the algorithm library's sort() to sort the student list by marks. Finally, we'll use fstream to save student data into a students.txt file, and set things up so that when the program reopens, it can read the data back in (persistence). This is where the file handling topic gets put to real use in a practical project.

Let's Build It

In addStudent(), after reading marks with cin >> marks;, throw an exception if cin.fail() happens, and show an error message with a try/catch block. Build a sortByMarks(vector<Student>& students) function that uses std::sort() with the lambda comparator [](const Student& a, const Student& b){ return a.getMarks() > b.getMarks(); }. In saveToFile(const vector<Student>& students), open an ofstream and write each student's data as a line in a text file. In loadFromFile(vector<Student>& students), open an ifstream, read the file line by line if it exists, and refill the vector — call this function at the start of main() when the program launches. Add Sort by Marks and Save & Exit options to the menu.

Example Code

cpp
#include <fstream>
#include <algorithm>
#include <stdexcept>

void sortByMarks(vector<Student>& students) {
    sort(students.begin(), students.end(),
        [](const Student& a, const Student& b) {
            return a.getMarks() > b.getMarks();
        });
    cout << "Sorted by marks (highest first).\n";
}

void saveToFile(const vector<Student>& students) {
    ofstream fout("students.txt");
    for (const Student& s : students) {
        fout << s.getRoll() << " " << s.getName() << " " << s.getMarks() << "\n";
    }
    fout.close();
    cout << "Saved to students.txt\n";
}

void loadFromFile(vector<Student>& students) {
    ifstream fin("students.txt");
    if (!fin) return;

    int roll;
    string name;
    double marks;
    while (fin >> roll >> name >> marks) {
        students.push_back(Student(name, roll, marks));
    }
    fin.close();
}

void addStudentSafe(vector<Student>& students) {
    try {
        string name;
        int roll;
        double marks;
        cout << "Enter name: ";
        cin >> name;
        cout << "Enter roll: ";
        cin >> roll;
        cout << "Enter marks: ";
        cin >> marks;

        if (cin.fail()) {
            throw runtime_error("Invalid marks input!");
        }
        students.push_back(Student(name, roll, marks));
        cout << "Student added!\n";
    } catch (const runtime_error& e) {
        cout << "Error: " << e.what() << "\n";
        cin.clear();
    }
}
You should see
Running the program and choosing Save & Exit creates a students.txt file and writes the data into it; reopening the program brings back the previously saved student list.

5-Minute Try It

Run the program and add 3 students — try sorting with Sort by Marks, then choose Save & Exit, reopen the program, and check whether the data comes back.

A Quick Word of Caution

After cin.fail() happens, you need cin.ignore() as well as cin.clear() to clear the invalid input out of the buffer — otherwise you can end up in an infinite loop.

Easy traps

  • Forgetting to reset the stream state with cin.clear() after cin.fail() happens, so every input operation after that keeps failing
  • Calling the file save function only from inside the menu loop, so if the program crashes or the user closes it with Ctrl+C, none of the data gets saved at all

Now Try It Yourself

Run the program and add 3 students — try sorting with Sort by Marks, then choose Save & Exit, reopen the program, and check whether the data comes back.

You'll know it worked when: Running the program and choosing Save & Exit creates a students.txt file and writes the data into it; reopening the program brings back the previously saved student list.

Mini Project: Student Manager - Part 3 | Thuta Learning