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
#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
ReachedThe 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
forloop 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.
totalmust survive every iteration. - A
const intgoal. The target does not change while the program runs. - A marker variable.
reached_daystarts 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
- Write the expected output for a goal of 60, then for a goal of 80, before writing code.
- Declare
goal,totalandreached_daybefore the loop. - 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.
- After the loop, print either the day the goal was reached or how many minutes are still missing.
- 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
ifis inside the loop's braces. - Every running total is 15:
totalis declared inside the loop body and starts from zero each day. Reached on day 5when you expected day 4: either the check uses>instead of>=, or later days overwritereached_daybecause thereached_day == 0guard 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
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: 15Day 2: 30Day 3: 45Day 4: 60Day 5: 75Reached 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.
#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
#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;
}Day 1: 15
Day 2: 30
Day 3: 45
Day 4: 60
Day 5: 75
Reached on day 4The 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.
Completion is saved in this browser only. It does not measure mastery.