C++ Temperature Logger Project
Record daily temperatures to a file and report trends and extremes.
What you will build
Log temperatures with dates into a file, then ask the log questions: What was the hottest day? The weekly average? Is it trending warmer? It's your first data-analysis program — small, but the read → store → aggregate pipeline is the shape of real data tools.
What you will learn
- Appending records to a file
- Parsing lines back into values
- Computing min, max, and averages
- Grouping data (e.g. by week or month)
Feature checklist
- Add a reading: date + temperature
- Readings persist in a text file
- Report: highest, lowest, and average temperature
- Average per week or month
- Simple text trend: warming / cooling / steady
Starter code
#include <iostream>
#include <fstream>
#include <string>
#include <limits>
struct Reading { std::string date; double temp; };
int main() {
std::ofstream out("temps.txt", std::ios::app);
std::string date; double t;
std::cout << "Date (YYYY-MM-DD): "; std::cin >> date;
std::cout << "Temp (C): "; std::cin >> t;
out << date << " " << t << "\n";
out.close();
std::ifstream in("temps.txt");
double sum = 0, hi = -std::numeric_limits<double>::max(),
lo = std::numeric_limits<double>::max();
int n = 0; Reading r;
while (in >> r.date >> r.temp) {
sum += r.temp; ++n;
if (r.temp > hi) hi = r.temp;
if (r.temp < lo) lo = r.temp;
}
if (n) std::cout << "Readings: " << n << " Avg: " << sum / n
<< " High: " << hi << " Low: " << lo << "\n";
}Full walkthrough
Step 1: Model a reading as a struct
struct Reading { std::string date; double temp; } — the date kept as text ('2026-10-21') because the logger never does arithmetic on dates, only grouping by their prefixes. Choosing the simplest type that supports the operations you actually perform is a real design skill, and this is a clean example.
Step 2: Load the log file at startup
Open temps.txt with std::ifstream and read pairs (date, temp) into a std::vector<Reading> — use while (in >> r.date >> r.temp) so a missing file simply means an empty log, not an error. Whitespace-separated values need no parsing code at all.
Step 3: Add readings and append them to the file
Prompt for a date and a temperature, push the Reading into the vector, and append the same line to the file with std::ofstream out("temps.txt", std::ios::app). The std::ios::app flag is the whole trick: without it, ofstream truncates the file and every save destroys your history.
Step 4: Compute the report in one pass
Highest, lowest, and average come from a single loop: track max and min (initialised from the FIRST reading, not from 0 — a log of sub-zero temperatures breaks a 0-initialised maximum), and accumulate the sum for the average. Guard the empty log before dividing.
double hi = rs[0].temp, lo = rs[0].temp, sum = 0;
for (auto& r : rs) {
hi = std::max(hi, r.temp);
lo = std::min(lo, r.temp);
sum += r.temp;
}
double avg = sum / rs.size();Step 5: Average per week or month by prefix
Because dates are stored as YYYY-MM-DD text, a month is just the first 7 characters. Group with a std::map<std::string, std::pair<double,int>> keyed by the prefix, accumulating sum and count — the map sorts the periods chronologically for free because the date format is big-endian.
Step 6: Describe the trend in words
Compare the average of the first half of the log with the second half: more than half a degree apart is 'warming' or 'cooling', otherwise 'steady'. It is crude statistics and honest about it — exactly right for a beginner project, and a template for every 'insights' feature you will ever bolt onto data.
Step 7: Test with a hand-checkable log
Enter 24.0, 26.5, 33.5 across three dates: highest 33.5, lowest 24.0, average 28.0. Restart the program and confirm the report is identical — that restart is your proof the file round-trip works.
Common mistakes
- Opening the save file without std::ios::app, erasing the entire history on every new reading.
- Initialising highest to 0, so a winter log where every reading is below zero reports a 'highest' that never happened.
- Dividing by rs.size() without checking the log is non-empty on a first run.
Compile and run
g++ -std=c++17 -o temp_logger temp_logger.cppSample run
$ ./temp_logger
Loaded 0 readings from temps.txt
1) Add reading 2) Report 3) Monthly averages 0) Quit
> 1
Date and temperature (YYYY-MM-DD, °C):
> 2026-10-20, 24.0
Saved.
1) Add reading 2) Report 3) Monthly averages 0) Quit
> 1
Date and temperature (YYYY-MM-DD, °C):
> 2026-10-21, 26.5
Saved.
> 2026-10-22, 33.5
> 2
Readings: 3
Highest: 33.5 °C
Lowest: 24.0 °C
Average: 28.0 °C
Trend: warming
> 0Try it in your browser
No browser demo is included for this project. Build the C++ version below — that is the real program.