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
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
Complete source code
Makefile
What each file does
- src/main.cpp
- The
Studentstruct,letter_for,median, input parsing with validation, and the report sections inmain. - Makefile
- Build automation:
makebuilds./app,make testruns the sample session and compares it with the expected output,make cleanremoves build files.
How to run it
Build
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
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
- 1Print each student's strongest and weakest subject.
- 2Show the percentile of every student in the class.
- 3Draw the histogram vertically instead of horizontally.
Built and tested it yourself? Mark it to update your dashboard.