Skip to content

Checkpoint: Your First Study Plan

The Course
•
35 min study

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

Predict before running C++
#include <iostream>

int main() {
    int first = 25;
    int second = 10;
    std::cout << "Total: " << first + second << "\n";
    return 0;
}
Check your prediction
Text
Total: 35

Replace + 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.

C++
std::cout << "Total: 75\n";      // correct only until a duration changes
std::cout << "Hours: 1.25\n";
A report derived from its data C++
#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;
}
Text
Total: 90
Hours: 1.5

The 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::string for the title. Include <string> and initialize it with the text.
  • Initialized int variables. One per duration, plus a const int goal 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 double operand. 60.0 keeps the fraction when converting minutes to hours.

Plan before you type

  1. Write the expected report on paper from the brief, including every line break.
  2. Declare the title, the three durations and the const goal.
  3. Calculate total from the three durations in one statement.
  4. Print each line using only title, total and goal, never a typed answer.
  5. 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: 1 instead of Hours: 1.25: both operands of the division are int, so the fraction was discarded before printing.
  • #include <string> is missing, yet the program still compiles: <iostream> happens to bring std::string in with this compiler. Another compiler may not, and then you get an error such as no 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 the std::string variable instead.
  • The report says Left: -15 minutes when you expected 15: the subtraction is the wrong way round. It should be goal - 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

Exercise

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++ week
  • Total: 75 minutes
  • Hours: 1.25
  • Split: 1 h 15 min
  • Left: 15 minutes

Then change one duration, predict the new report, and run it again.

Your workspace C++
#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
Run the solution C++
#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;
}
Text
C++ week
Total: 75 minutes
Hours: 1.25
Split: 1 h 15 min
Left: 15 minutes

Every 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.

Was this article helpful?

Your response is saved on this device.