The report says how much time remains, but not how many tasks. "205 minutes remaining" could be one long practice session or twenty short reviews, and those plans feel very different. In this lesson you add a pending-task count, the way you would extend any real project: write the calculation as a read-only helper, test it on its own, and only then connect it to the program's output.
A count answers how many tasks remain; minutes answer how much time remains. They are different questions, so they need different code, and a test that passes for one can fail for the other. Keep the new calculation in a function that takes const std::vector<Task>&, so it cannot change the plan it describes.
Predict and run
#include <iostream>
#include <vector>
int main() {
std::vector<bool> done{false, true, false};
int pending = 0;
for (bool value : done) {
if (!value) {
++pending;
}
}
std::cout << pending << "\n";
return 0;
}Check your prediction
2Change all the flags to true and the count becomes 0. Now picture two pending tasks of 180 minutes and one of 5: the count is 3, while remaining minutes is 365. Neither number can be derived from the other.
Where the change goes
Adding a feature to a multi-file project touches each file for a different reason. Make the changes in this order, running the tests after each step:
planner.h: declareint pending_count(const std::vector<Task>& tasks);with a one-line contract comment, such as "Counts tasks that are not done. An empty vector gives 0."planner.cpp: define it with aconst Task&loop.test_planner.cpp: assert an empty vector, a mix, and the count aftercomplete_task, before the final success message.main.cpp: printPending: Nafter the remaining minutes.
Testing before printing means a bug shows up as a named failing assertion rather than a wrong number you might not notice in a report. You could instead add a pending field to Summary and fill it in summarize; that is also a sound design, because one pass over the tasks would produce every total. Whichever you choose, the tests stay the same.
Using complete_task
The project also includes complete_task(tasks, index), which returns a bool. To experiment, call it in main.cpp after the input loop but before printing. Check its result before claiming an update succeeded:
if (!complete_task(tasks, 0)) {
std::cerr << "No task 0 to complete\n";
}A valid call changes completed, remaining and pending, while planned stays the same. With empty input, the call returns false and nothing changes. That one experiment exercises the index check, the idempotent update and the summary together.
Practice
Your turn
Write int pending_count(const std::vector<Task>& tasks) for the planner's full Task record. Assert:
- two tasks not done and one done give 2
- an empty vector gives 0
- three done tasks give 0
- after setting one pending task's
doneto true, the count drops by one
Then print Pending: 2 for the first case, the way the planner's report would.
#include <iostream>
#include <string>
#include <vector>
#include <cassert>
struct Task {
std::string title;
int minutes = 0;
bool done = false;
};
int main() {
std::vector<Task> tasks{{"Read", 25, false}, {"Review", 10, true}, {"Practice", 180, false}};
// Write pending_count above main and your assertions here.
return 0;
}Hint 1: the idea
Only records that are not done increment the counter. Minutes play no part.
Hint 2: a first step
Write the four assertions first, with a pending_count that just returns 0, and run. The empty vector and the all-done case pass; the first assertion fails. That tells you which cases actually test the loop. Now write it.
Show one solution
#include <iostream>
#include <string>
#include <vector>
#include <cassert>
struct Task {
std::string title;
int minutes = 0;
bool done = false;
};
int pending_count(const std::vector<Task>& tasks) {
int pending = 0;
for (const Task& task : tasks) {
if (!task.done) {
++pending;
}
}
return pending;
}
int main() {
std::vector<Task> tasks{{"Read", 25, false}, {"Review", 10, true}, {"Practice", 180, false}};
assert(pending_count(tasks) == 2);
assert(pending_count({}) == 0);
assert(pending_count({{"A", 5, true}, {"B", 5, true}, {"C", 5, true}}) == 0);
std::vector<Task> changed = tasks;
changed[0].done = true;
assert(pending_count(changed) == 1);
std::cout << "Pending: " << pending_count(tasks) << "\n";
return 0;
}Pending: 2The const reference guarantees the count cannot alter the plan. The test changes a copy of the vector, so tasks still has two pending tasks when the final line prints, which is value semantics doing useful work in a test. The 180-minute and 25-minute tasks each count once, which is exactly the difference from remaining minutes. The function works on its own; next you connect it to the real project.
Extend the project
Add pending_count to the planner
Make the four changes from "Where the change goes" in the real project below. There are two editors because there are two programs, exactly as in the local build commands further down: the tests (test_planner.cpp with planner.cpp) and the application (main.cpp with planner.cpp). Each has its own main, so they can't be linked into one program. Both editors save your edits in this browser.
- In the tests editor, declare
pending_countinplanner.h, define it inplanner.cpp, and add assertions totest_planner.cppbefore the success message: an empty vector, a mix of done and pending tasks, and the count aftercomplete_task. Run until the tests printPlanner checks passed. - Copy your new
planner.handplanner.cpplines into the same tabs of the application editor, then print the count inmain.cpp, right after theRemaining:line.
With the sample input already loaded into the application editor, the report must end like this:
Remaining: 205 minutes
Pending: 2
Rejected: 1Hint 1: the idea
The header is the promise both programs read; the source file keeps it; the test proves it; main.cpp only displays it. If you print before the tests pass, a wrong count looks just like a right one.
Hint 2: a first step
Add only the declaration to planner.h and one assertion, assert(pending_count({}) == 0);, to test_planner.cpp, then run the tests. A link error naming pending_count means the declaration is right and the definition is still missing. Now add the definition.
Show one solution
The declaration, at the end of planner.h, before #endif:
// Counts tasks that are not done. An empty vector gives 0.
int pending_count(const std::vector<Task>& tasks);The definition in planner.cpp is the function from the exercise above. The new assertions in test_planner.cpp, just before std::cout << "Planner checks passed\n";:
assert(pending_count({}) == 0);
std::vector<Task> mixed{{"Read", 25, false}, {"Review", 10, true}, {"Practice", 180, false}};
assert(pending_count(mixed) == 2);
assert(complete_task(mixed, 0));
assert(pending_count(mixed) == 1);And in main.cpp, after the Remaining: line:
std::cout << "Pending: " << pending_count(tasks) << '\n';The tests print Planner checks passed, and the application's report gains Pending: 2 between Remaining: 205 minutes and Rejected: 1. The complete_task assertion checks the count against a real update rather than a hand-built vector, so the new feature is tested together with the code that changes it.
Optional local setup
Download the C++ study planner. It contains the same maintained source files as the browser tabs, tests, sample input and README. Browser edits are not included: copy your extension from the two editors above into the downloaded files if you want to keep it.
Install a C++20-capable Clang or GCC compiler: macOS Command Line Tools, a Linux compiler package, or Windows MSYS2. In a POSIX terminal in the extracted folder:
c++ --version
c++ -std=c++20 -Wall -Wextra -Wpedantic -Werror main.cpp planner.cpp -o study-planner
./study-planner < sample-input.txt
c++ -std=c++20 -Wall -Wextra -Wpedantic -Werror test_planner.cpp planner.cpp -o test-planner
./test-planner-Wall -Wextra -Wpedantic turn on the common warnings, and -Werror makes any warning stop the build, so a warning cannot scroll past unread.
Compile the application and the tests separately; each has its own main, and building both entry points into one executable would fail at the link stage. Both commands list planner.cpp, because that is where the definitions live. Keep assertions enabled by not defining NDEBUG. The shell's < supplies a file as standard input. On Windows the executable may have an .exe suffix; use the MSYS2 terminal for these commands. Local installation is optional.
The browser supports the C++20 features used in this course, but not exceptions or threads. Those are browser limitations, not missing features of the C++ language. Standard strings and vectors manage their storage automatically; manual allocation is not needed for this project.
Completion is saved in this browser only. It does not measure mastery.