Skip to content
C++ project #02Beginner

Student Grade Tracker

Turn raw marks into a ranked report with averages, letter grades, class statistics, subject toppers and a grade histogram.

Estimated time
~3 hours to build
Files
5 files · 300 lines
Technologies
C++17, <vector>, <algorithm>, <numeric>, <iomanip>

Introduction

This project takes plain-text marks — one student per line — and produces the kind of report a teacher would print. It is a practical tour of the STL's most useful pieces: a vector of structs, std::sort with a custom comparator, std::accumulate, std::max_element and std::count_if.

Problem statement

Read a line naming the subjects, then one line per student with a name and one mark per subject. Skip (and report) lines with the wrong number of marks, marks outside 0–100 or non-numeric values. Print the students ranked by average with shared ranks for ties, then the class mean, median and standard deviation, each subject's average and top scorer, and a distribution of letter grades.

Objectives

  • Model records with a struct and store them in std::vector
  • Sort with a lambda comparator, including a tie-breaker
  • Compute mean, median and standard deviation
  • Validate text input with std::stoi and its position argument

Features

  • Any number of subjects, declared on the first line
  • Competition ranking (1, 2, 2, 4) for equal averages
  • Mean, median and population standard deviation
  • Per-subject average and topper; text histogram of grades

Concepts demonstrated

  • Structs
  • std::sort with comparators
  • STL algorithms
  • Input validation
  • Formatted tables

System architecture

Input is processed line by line. The subjects line fills a vector of names; every other line becomes a Student with a name, a vector of marks and a computed average. Invalid lines are reported with their line number and skipped. After reading, the students are sorted by average (then name) and printed with std::setw columns. Statistics use std::accumulate and a median helper that sorts a copy; subject toppers use std::max_element with a lambda that captures the column index.

Folder structure

TextProject tree
student-grade-tracker/
├── README.md
├── Makefile
├── src/main.cpp
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -O2
SRC := $(wildcard src/*.cpp)
HDR := $(wildcard src/*.h src/*.hpp)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CXX) $(CXXFLAGS) -Isrc $(SRC) -o app
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/main.cpp
The Student struct, letter_for, median, input parsing with validation, and the report sections in main.
Makefile
Build automation: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
g++ -std=c++17 -Wall -Wextra -Isrc src/*.cpp -o app
# or simply: make

Run

Terminal
./app # Linux / macOS
.\app.exe # Windows

Test against the expected output

Terminal
./app < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# Get-Content tests\sample-input.txt | .\app.exe > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
Line 7: skipped (expected 3 marks from 0 to 100)
Line 8: skipped (expected 3 marks from 0 to 100)
Line 10: skipped (expected 3 marks from 0 to 100)
Rank Name Maths Physics English Average Grade
--------------------------------------------------------------
1 Asha 92 88 95 91.67 A
2 Priya 88 92 87 89.00 B
3 Lina 85 91 78 84.67 B
3 Meera 85 91 78 84.67 B
5 Ravi 67 74 80 73.67 C
6 John 45 58 62 55.00 F
Class statistics (6 students)
Mean average : 79.78
Median : 84.67
Std deviation: 12.42
Subject summary
Maths avg 77.00 top Asha (92)
Physics avg 82.33 top Priya (92)
English avg 80.00 top Asha (95)
Grade distribution
A | # 1
B | ### 3
C | # 1
D | 0
F | # 1
Input
subjects Maths Physics English
Asha 92 88 95
Ravi 67 74 80
Meera 85 91 78
John 45 58 62
Priya 88 92 87
Karan 101 70 60
Sam 72 seventy 70
Lina 85 91 78
Omar 55 61

Testing

  • Compare the scripted run with tests/expected-output.txt: three lines are rejected (a mark of 101, the word 'seventy', and only two marks).
  • Lina and Meera have identical marks; both must be ranked 3 and the next student ranked 5.
  • Check the mean by hand: (91.67 + 89 + 84.67 + 84.67 + 73.67 + 55) / 6 ≈ 79.78.

Common errors

terminate called after throwing std::invalid_argument

std::stoi throws for text like 'seventy'. Wrap it in try/catch, as main does.

"12abc" is accepted as 12

std::stoi stops at the first non-digit. Pass a std::size_t* and check that the whole token was used.

Possible improvements

  • Weight subjects differently (e.g. credits)
  • Read the data from a CSV file given on the command line
  • Export the report as CSV for a spreadsheet

Additional challenges

  1. 1Print each student's strongest and weakest subject.
  2. 2Show the percentile of every student in the class.
  3. 3Draw the histogram vertically instead of horizontally.

Built and tested it yourself? Mark it to update your dashboard.