New! Explore our Programming Academy and AI Tutor - learn to code from scratch, free to start. Explore Now
Programming› C++› Project: A Console Grade Book

Project: A Console Grade Book

💻 C++

Build a complete console program with classes, vectors and formatted reports.

Lesson 9 of 9 Project
0 of 9 completed

This project combines everything in the course into a small program you could extend for a real school.

By the end of this lesson you can:
  • Model students with a class.
  • Store them in a vector.
  • Print a sorted report.

Design

Create a Student class with a name and a vector of scores. A GradeBook class holds students and provides add, report and class average functions.

Build the Model

Keep printing out of the model where possible. Methods should calculate and return values so they are easy to test.

#include <numeric>
#include <string>
#include <vector>

class Student {
private:
    std::string name;
    std::vector<int> scores;

public:
    explicit Student(std::string name) : name(std::move(name)) {}

    void addScore(int score) {
        if (score < 0 || score > 100) {
            throw std::invalid_argument("Score must be 0 to 100");
        }
        scores.push_back(score);
    }

    double average() const {
        if (scores.empty()) return 0;
        int total = std::accumulate(scores.begin(), scores.end(), 0);
        return static_cast<double>(total) / scores.size();
    }

    const std::string& getName() const { return name; }
};

Report

Sort students by average, then print aligned columns and a class average. Handle a student with no scores without dividing by zero.

Tip Compile with -Wall -Wextra and fix every warning. The compiler will catch many mistakes before the program runs.

Practice

  1. Add a highestScore method to Student.
  2. Add a subject field.
  3. Print a pass or fail status for each student.

Hands-on exercise

Build the grade book.
1. Add at least four students with two or three scores.
2. Reject scores outside 0 to 100.
3. Print a report sorted by average, highest first.
4. Print the class average at the end.

Recap

A finished console program separates the model from output, uses standard containers and algorithms, and handles edge cases.

Example

#include <numeric>
#include <string>
#include <vector>

class Student {
private:
    std::string name;
    std::vector<int> scores;

public:
    explicit Student(std::string name) : name(std::move(name)) {}

    void addScore(int score) {
        if (score < 0 || score > 100) {
            throw std::invalid_argument("Score must be 0 to 100");
        }
        scores.push_back(score);
    }

    double average() const {
        if (scores.empty()) return 0;
        int total = std::accumulate(scores.begin(), scores.end(), 0);
        return static_cast<double>(total) / scores.size();
    }

    const std::string& getName() const { return name; }
};

Exercises

Exercise 1. Grade book Hard
Build the grade book.
1. Add at least four students with two or three scores.
2. Reject scores outside 0 to 100.
3. Print a report sorted by average, highest first.
4. Print the class average at the end.
C++ exercise-1.txt
Expected Output
A sorted report with aligned averages and a class average.

Lesson Nav

Lesson sections