Skip to content
C++ project #05Beginner

Weather Data Analyzer

Validate daily temperature records and report averages, extremes, frost nights, the longest warming streak, a histogram and monthly averages.

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

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

Read records of the form 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

TextProject tree
weather-data-analyzer/
├── 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
Day, valid_date, the validating reader, and each statistic, the histogram and monthly averages 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 10: dates must increase, skipped
Line 11: low (11) is above high (10), skipped
Line 12: malformed record skipped
Line 14: malformed record skipped
Records analysed : 11 (4 rejected)
Period : 2026-01-27 to 2026-02-08
Average high/low : 10.2 / 2.3 C
Hottest day : 2026-02-08 (17.5 C)
Coldest night : 2026-02-01 (-3.5 C)
Widest range : 2026-02-08 (9.5 C)
Frost nights : 4
Warming streak : 6 days ending 2026-02-08
Daily highs
0 to 5 | # 1
5 to 10 | ##### 5
10 to 15 | ### 3
15 to 20 | ## 2
Monthly averages (high)
2026-01 8.4 C over 5 days
2026-02 11.7 C over 6 days
Input
# 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.txt must match tests/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

  1. 1Compute the median high per month.
  2. 2Print the histogram for lows next to the highs.
  3. 3Find the longest cooling streak as well.

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