Skip to content
C++ project #10Intermediate

CSV Sales Report Generator

Parse real-world CSV (quoted fields, embedded commas and quotes), validate every row, and produce grouped revenue reports with shares and trends.

Estimated time
~4 hours to build
Files
5 files · 336 lines
Technologies
C++17, <map>, <vector>, <algorithm>, <stdexcept>

Introduction

CSV looks trivial until a product is called Monitor, 27". This project writes a correct CSV field splitter, then aggregates sales by region, product and month with std::map, printing shares, bar charts and month-on-month change — the kind of report businesses produce every day.

Problem statement

Read sales data from standard input with the header date,region,product,units,unit_price. Parse quoted fields correctly, reject rows with the wrong number of fields, bad dates, non-numeric units or prices, or unterminated quotes, and report why. Print total revenue and units, revenue by region and by product (sorted by revenue with percentage shares), a monthly trend with percentage change, and the largest single sale.

Objectives

  • Write a small state machine that splits CSV records
  • Convert text to numbers safely with portable error messages
  • Aggregate with std::map and re-sort by value
  • Format currency with thousands separators

Features

  • Quoted fields with commas and doubled quotes
  • Row-level validation with line numbers
  • Grouped totals with shares and bar charts
  • Month-over-month trend

Concepts demonstrated

  • Parsing
  • State machines
  • Exceptions for validation
  • Aggregation
  • Sorting by value

System architecture

split_csv walks the line character by character with one flag, quoted: inside quotes a comma is literal and "" becomes one quote; outside, a comma ends the field. parse_units and parse_price wrap std::stoi/std::stod and turn any failure into the program's own std::invalid_argument message, so the output is identical on every compiler. Each valid row becomes a Sale; a loop adds it to three std::map<std::string, Totals> groupings at once. print_group copies a map into a vector and sorts it by revenue.

Folder structure

TextProject tree
csv-sales-report/
├── 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
split_csv, the conversion helpers, money with thousands separators, print_group and the read–validate–aggregate–report pipeline.
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 9 skipped: units 'ten' must be a positive whole number
Line 12 skipped: expected 5 fields, found 4
Line 17 skipped: unterminated quote
13 rows read, 3 skipped
Total revenue: $25,299.03 from 166 units
Revenue by region
-----------------
North 62 units $8,417.75 33.3% ||||||||
South 19 units $7,524.00 29.7% |||||||
West 58 units $6,431.88 25.4% ||||||
East 27 units $2,925.40 11.6% ||
Revenue by product
------------------
Laptop 20 units $17,640.00 69.7% |||||||||||||||||
Monitor, 27" 17 units $3,817.50 15.1% |||
Keyboard 67 units $2,604.63 10.3% ||
Mouse 62 units $1,236.90 4.9% |
Monthly trend
-------------
2026-07 $8,568.75
2026-08 $8,260.38 -3.6%
2026-09 $8,469.90 +2.5%
Largest single sale: $5,154.00 (6 x Laptop, West)
Input
date,region,product,units,unit_price
2026-07-03,North,Laptop,4,899.00
2026-07-05,South,"Monitor, 27""",6,229.50
2026-07-11,North,Keyboard,25,39.99
2026-07-19,East,Laptop,2,899.00
2026-07-22,West,Mouse,40,19.95
2026-08-02,South,Laptop,5,879.00
2026-08-09,East,"Monitor, 27""",3,229.50
2026-08-15,North,Mouse,ten,19.95
2026-08-16,West,Keyboard,12,39.99
2026-08-21,North,Laptop,3,899.00
2026-08-30,East,Mouse,15
2026-09-04,South,"Monitor, 27""",8,219.00
2026-09-10,West,Laptop,6,859.00
2026-09-18,North,Keyboard,30,37.50
2026-09-25,East,Mouse,22,19.95
2026-09-28,South,"Unclosed,5,10.00

Testing

  • ./app < tests/sample-input.txt must match tests/expected-output.txt.
  • Three rows are rejected: ten units, a row with four fields, and an unterminated quote.
  • "Monitor, 27""" is read as the product name Monitor, 27".
  • North's revenue is 4×899 + 25×39.99 + 3×899 + 30×37.50 = $8,417.75.

Common errors

Fields containing commas are split in two

You can't use getline(ss, field, ',') for CSV; track whether you are inside quotes.

Prices like 39.99 become 3998 cents

Round when converting: llround(v * 100) or v * 100 + 0.5, never plain truncation.

Possible improvements

  • Take the file name and grouping column as command-line arguments
  • Write the report as CSV or Markdown
  • Detect the delimiter (comma or semicolon) automatically

Additional challenges

  1. 1Support fields that contain line breaks inside quotes.
  2. 2Add a top-N products per region report.
  3. 3Compute a 3-month moving average.

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