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
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
Complete source code
Makefile
What each file does
- src/main.cpp
split_csv, the conversion helpers,moneywith thousands separators,print_groupand the read–validate–aggregate–report pipeline.- 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.
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.txtmust matchtests/expected-output.txt.- Three rows are rejected:
tenunits, a row with four fields, and an unterminated quote. "Monitor, 27"""is read as the product nameMonitor, 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
- 1Support fields that contain line breaks inside quotes.
- 2Add a top-N products per region report.
- 3Compute a 3-month moving average.
Built and tested it yourself? Mark it to update your dashboard.