Skip to content

Checkpoint: Reach a Goal

The Course
•
35 min study

A study tracker should answer two different questions: "how am I doing each day?" and "did I reach my goal this week?" The first is a progress update that belongs inside the loop, once per day. The second is a verdict that belongs after the loop, printed once, using the final total. In this checkpoint you combine the day's tools and place each piece of logic where it belongs.

A goal reached exactly is the most important test case. It is the only value that distinguishes >= from >: with a goal of 60 and a total of 60, one version says "Reached" and the other says "Keep going." If your tests never hit the goal exactly, both versions pass and you cannot tell them apart.

Predict and run

Predict before running C++
#include <iostream>

int main() {
    int total = 0;
    for (int day = 0; day < 3; ++day) {
        total += 20;
    }
    if (total >= 60) {
        std::cout << "Reached\n";
    }
    return 0;
}
Check your prediction
Text
Reached

The loop starts at 0 and stops before 3, so it runs three times and the total is exactly 60. Now move the if inside the loop's braces and trace it: Reached still prints once, but only because the goal is met on the last day. Raise the loop to five days and the message prints three times. A verdict inside a loop repeats; a verdict after a loop is final.

What you will use

  • A for loop with a fixed count. Five iterations, one per day, with a counter that starts at 1 so it can be printed.
  • An accumulator declared before the loop. total must survive every iteration.
  • A const int goal. The target does not change while the program runs.
  • A marker variable. reached_day starts at 0, meaning "not yet," and records the first day the goal was met.
  • A condition joined with &&. Record the day only if the goal is met and no day has been recorded yet.
  • A final if/else after the loop. One verdict, using the finished total.

Plan before you type

  1. Write the expected output for a goal of 60, then for a goal of 80, before writing code.
  2. Declare goal, total and reached_day before the loop.
  3. In the loop, add the day's minutes, print the running total, then check whether this is the first day at or above the goal.
  4. After the loop, print either the day the goal was reached or how many minutes are still missing.
  5. Run with both goals and compare against what you wrote.
Prove it can fail

Once both goals work, change >= to > in the reached check and run with a goal of 60. The verdict should move from day 4 to day 5, because the total equals 60 on day 4. Then remove reached_day == 0 && from the condition: the verdict should claim day 5, because each later day overwrites the first. Restore both and confirm the original output returns.

Mistakes worth checking for

  • The verdict prints five times: the final if is inside the loop's braces.
  • Every running total is 15: total is declared inside the loop body and starts from zero each day.
  • Reached on day 5 when you expected day 4: either the check uses > instead of >=, or later days overwrite reached_day because the reached_day == 0 guard is missing.
  • Keep going: -15 minutes left: the else branch ran with the subtraction reversed, or the condition is wrong so a reached goal falls into the else branch.
  • Six lines of progress, or four: the loop's start or condition is off by one. Trace the first and last iteration.

Practice

Exercise

Your turn

Add 15 minutes on each of five days with a goal of 60. Print the running total for each day, then one verdict:

  • Day 1: 15
  • Day 2: 30
  • Day 3: 45
  • Day 4: 60
  • Day 5: 75
  • Reached on day 4

With a goal of 80, the five progress lines are the same and the last line is Keep going: 5 minutes left.

Your workspace C++
#include <iostream>

int main() {
    const int goal = 60;
    // Write your solution here.
    return 0;
}
Hint 1: the idea

Separate accumulating from interpreting. The loop collects totals and remembers the first day the goal was met; the code after the loop decides what to say.

Hint 2: a first step

Before the loop write int total = 0; and int reached_day = 0;. Inside, after total += 15;, check if (reached_day == 0 && total >= goal) and store day.

Show one solution
Run the solution C++
#include <iostream>

int main() {
    const int goal = 60;
    int total = 0;
    int reached_day = 0;
    for (int day = 1; day <= 5; ++day) {
        total += 15;
        std::cout << "Day " << day << ": " << total << "\n";
        if (reached_day == 0 && total >= goal) {
            reached_day = day;
        }
    }
    if (reached_day != 0) {
        std::cout << "Reached on day " << reached_day << "\n";
    } else {
        std::cout << "Keep going: " << goal - total << " minutes left\n";
    }
    return 0;
}
Text
Day 1: 15
Day 2: 30
Day 3: 45
Day 4: 60
Day 5: 75
Reached on day 4

The marker value 0 works because no real day is numbered 0, so it can safely mean "not yet." The && guard keeps the first qualifying day instead of the last, and the single if/else after the loop speaks once with the finished total. With a goal of 80 the marker stays 0 and the else branch reports the 5-minute gap. On Day 3 you will move calculations like this into named functions and check them automatically.

Was this article helpful?

Your response is saved on this device.