Introduction
Real data is messy: duplicated dates, typos, impossible values. This analyser reads daily high/low temperatures, rejects bad records with a clear reason, and then lets the STL do the heavy lifting — std::accumulate, std::max_element, std::count_if — to summarise what is left.
Problem statement
YYYY-MM-DD high low (°C, decimals allowed; lines starting with # are comments). Reject malformed lines, lows above highs, invalid dates and dates that don't increase. For the valid records print the period, average high and low, hottest day, coldest night, widest daily range, number of frost nights (low ≤ 0), the longest run of rising highs, a histogram of highs in 5° buckets and the average high per month.Objectives
- Parse and validate structured text records
- Use STL algorithms with lambdas instead of hand-written loops
- Detect runs (streaks) in a sequence
- Group data with std::map keys
Features
- Clear rejection messages with line numbers
- Extremes reported with their dates
- Longest warming streak detection
- 5-degree histogram and per-month averages
Concepts demonstrated
- Data validation
- Lambdas
- std::accumulate / max_element / count_if
- std::map grouping
- Streak detection
System architecture
Each valid line becomes a Day struct (date, high, low, and a range() helper) appended to a vector. Validation happens in order: shape of the line, valid_date, low ≤ high, and strictly increasing dates (ISO dates compare correctly as strings). The report is a sequence of independent calculations over the vector. The histogram maps each high to floor(high / 5) * 5 in a std::map<int,int>; monthly averages group by the YYYY-MM prefix into a map of (sum, count) pairs.
Folder structure
Complete source code
Makefile
What each file does
- src/main.cpp
Day,valid_date, the validating reader, and each statistic, the histogram and monthly averages 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.
# date high low 2026-01-27 6.5 -2.0 2026-01-28 8.0 -0.5 2026-01-29 9.5 1.0 2026-01-30 11.0 3.5 2026-01-31 7.0 2.0 2026-02-01 4.0 -3.5 2026-02-02 5.5 -1.0 2026-02-03 12.5 4.0 2026-02-03 13.0 5.0 2026-02-04 10.0 11.0 2026-02-05 hot cold 2026-02-06 14.5 6.0 2026-13-01 10.0 2.0 2026-02-07 16.0 7.5 2026-02-08 17.5 8.0
Testing
./app < tests/sample-input.txtmust matchtests/expected-output.txt; four lines are rejected (duplicate date, low above high, text instead of numbers, month 13).- The warming streak is 4.0 → 5.5 → 12.5 → 14.5 → 16.0 → 17.5: six days ending 2026-02-08.
- Add a line with three numbers after the date; it must be rejected as malformed.
Common errors
Negative temperatures land in the wrong histogram bucket
Use std::floor before casting: (int)(-3.5 / 5) is 0, but floor gives -1.
Every record after a bad one is rejected
Use a fresh std::istringstream per line, so a failed read never leaves std::cin in a fail state.
Possible improvements
- Read Fahrenheit or Celsius and convert
- Detect gaps of missing days between records
- Report the coldest and warmest week (7-day moving average)
Additional challenges
- 1Compute the median high per month.
- 2Print the histogram for lows next to the highs.
- 3Find the longest cooling streak as well.
Built and tested it yourself? Mark it to update your dashboard.