A study report is only useful if its numbers follow from its data. If you type Total: 75 by hand, the report is right today and wrong the moment a session changes. In this checkpoint you combine the day's tools into a report whose every number is derived from a few named values: change one duration and the whole report updates by itself.
A correct compilation does not prove that your calculation matches the brief. Replacing + with - still compiles. The only way to catch that kind of mistake is to know the expected answer before you run, which is why the plan below starts with writing the expected report.
Predict and run
#include <iostream>
int main() {
int first = 25;
int second = 10;
std::cout << "Total: " << first + second << "\n";
return 0;
}Check your prediction
Total: 35Replace + with - and run again. The program compiles and prints Total: 15 without any complaint. The compiler checks that your code is valid C++; it cannot know that you meant to add. Your written prediction is what exposes the error.
Derived, not typed
Compare two versions of the same report. The first types its answers; the second calculates them.
std::cout << "Total: 75\n"; // correct only until a duration changes
std::cout << "Hours: 1.25\n";#include <iostream>
int main() {
int first = 25;
int second = 10;
int third = 55;
int total = first + second + third;
std::cout << "Total: " << total << "\n";
std::cout << "Hours: " << total / 60.0 << "\n";
return 0;
}Total: 90
Hours: 1.5The third duration changed from 40 to 55, and both lines followed. The total is calculated once and reused, so the two lines can never disagree with each other.
What you will use
- A
std::stringfor the title. Include<string>and initialize it with the text. - Initialized
intvariables. One per duration, plus aconst intgoal that must not change. - One stored total. Calculated once from the durations, then reused by every line that needs it.
- Integer
/and%. To split the total into whole hours and leftover minutes. - A
doubleoperand.60.0keeps the fraction when converting minutes to hours.
Plan before you type
- Write the expected report on paper from the brief, including every line break.
- Declare the title, the three durations and the
constgoal. - Calculate
totalfrom the three durations in one statement. - Print each line using only
title,totalandgoal, never a typed answer. - Run, compare with your written report, then change one duration and predict the new report before running again.
Prove it can fail
After your report matches, break it on purpose. Change 60.0 to 60: the hours line should drop to 1, showing integer division. Then change the third duration to 50: total, hours, split and left should all change together. If any line stays the same, that line is printing a typed answer instead of a calculation.
Mistakes worth checking for
Hours: 1instead ofHours: 1.25: both operands of the division areint, so the fraction was discarded before printing.#include <string>is missing, yet the program still compiles:<iostream>happens to bringstd::stringin with this compiler. Another compiler may not, and then you get an error such asno member named 'string' in namespace 'std'. Include what you use.- A compile error mentioning invalid operands when joining text: two string literals were joined with
+. Start from thestd::stringvariable instead. - The report says
Left: -15 minuteswhen you expected 15: the subtraction is the wrong way round. It should begoal - total. - Two lines disagree after you change a duration: one of them calculated its own sum, or typed a number, instead of reusing
total.
Practice
Your turn
Create the title C++ week, three durations 25, 10 and 40, and a const int goal = 90;. Print this report exactly, deriving every number:
C++ weekTotal: 75 minutesHours: 1.25Split: 1 h 15 minLeft: 15 minutes
Then change one duration, predict the new report, and run it again.
#include <iostream>
int main() {
// Write your solution here.
return 0;
}Hint 1: the idea
Calculate the total once and reuse it. Every other number is an expression built from total or goal.
Hint 2: a first step
Keep each duration in its own initialized variable, write int total = first + second + third;, and get the first two lines printing before adding the rest.
Show one solution
#include <iostream>
#include <string>
int main() {
std::string title = "C++ week";
int first = 25;
int second = 10;
int third = 40;
const int goal = 90;
int total = first + second + third;
std::cout << title << "\n";
std::cout << "Total: " << total << " minutes\n";
std::cout << "Hours: " << total / 60.0 << "\n";
std::cout << "Split: " << total / 60 << " h " << total % 60 << " min\n";
std::cout << "Left: " << goal - total << " minutes\n";
return 0;
}C++ week
Total: 75 minutes
Hours: 1.25
Split: 1 h 15 min
Left: 15 minutesEvery number in the report flows from four values, so a single edit keeps the report consistent. The hours line and the split line use the same total in two different ways: floating-point division keeps the fraction, while / and % on integers give whole hours and leftover minutes. If the durations ever add up to more than 90, Left turns negative; on Day 2 you will learn to make the program choose a different message in that case.
Completion is saved in this browser only. It does not measure mastery.