Skip to content

Extend and Download Your Planner

The Course
•
40 min study

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

Predict before running C++
#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
Text
2

Change 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:

  1. planner.h: declare int 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."
  2. planner.cpp: define it with a const Task& loop.
  3. test_planner.cpp: assert an empty vector, a mix, and the count after complete_task, before the final success message.
  4. main.cpp: print Pending: N after 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:

C++
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

Exercise

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 done to true, the count drops by one

Then print Pending: 2 for the first case, the way the planner's report would.

Your workspace C++
#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
Run the solution C++
#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;
}
Text
Pending: 2

The 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

Exercise

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.

  1. In the tests editor, declare pending_count in planner.h, define it in planner.cpp, and add assertions to test_planner.cpp before the success message: an empty vector, a mix of done and pending tasks, and the count after complete_task. Run until the tests print Planner checks passed.
  2. Copy your new planner.h and planner.cpp lines into the same tabs of the application editor, then print the count in main.cpp, right after the Remaining: line.

With the sample input already loaded into the application editor, the report must end like this:

Text
Remaining: 205 minutes
Pending: 2
Rejected: 1
Tests: planner.h, planner.cpp, test_planner.cpp C++
#include "planner.h" #include <cassert> #include <iostream> int main() { Task out{"Keep me", 77, true}; const std::vector<std::string> bad{ "", "0 0 Zero", "181 0 Long", "25 2 Flag", "25 0", "25 0 ", "no 0 Text", "25x 0 Text", "25 0Title", "25 1.5 Title", "25+0 Title", "25-0 Title", "99999999999999999999999999 0 Huge", "25 0 " + std::string(41, 'x'), "25 0 Title\nextra" }; for (const std::string& line : bad) { assert(!parse_task(line, out)); assert(out.title == "Keep me" && out.minutes == 77 && out.done); } assert(parse_task("1 0 Short", out) && out.minutes == 1 && !out.done); assert(parse_task("180 1 " + std::string(40, 'x'), out)); assert(out.minutes == 180 && out.done && out.title.size() == 40); std::vector<Task> tasks; Summary result = summarize(tasks); assert(result.planned == 0 && result.completed == 0 && result.remaining == 0); assert(!complete_task(tasks, 0)); Task original{"Read", 25, false}; assert(add_task(tasks, original)); original.title = "Changed"; assert(tasks[0].title == "Read"); assert(add_task(tasks, Task{"Review", 10, true})); assert(!add_task(tasks, Task{"Invalid", 0, false}) && tasks.size() == 2); assert(!add_task(tasks, Task{"", 25, false})); result = summarize(tasks); assert(result.planned == 35 && result.completed == 10 && result.remaining == 25); assert(!complete_task(tasks, tasks.size())); assert(!tasks[0].done); assert(complete_task(tasks, 0)); assert(complete_task(tasks, 0)); result = summarize(tasks); assert(result.planned == 35 && result.completed == 35 && result.remaining == 0); std::vector<Task> full; for (int i = 0; i < 100; ++i) { assert(add_task(full, Task{"Focus", 180, false})); } assert(!add_task(full, Task{"Extra", 1, false}) && full.size() == 100); assert(summarize(full).planned == 18000); std::cout << "Planner checks passed\n"; return 0; } #include "planner.h" #include <sstream> bool valid_task(const Task& task) { return task.minutes >= 1 && task.minutes <= 180 && !task.title.empty() && task.title.size() <= 40 && task.title.find_first_not_of(" \t") != std::string::npos && task.title.find_first_of("\r\n") == std::string::npos; } bool parse_task(const std::string& line, Task& out) { if (line.find_first_of("\r\n") != std::string::npos) { return false; } std::istringstream input(line); Task candidate; int done = 0; char separator = '\0'; if (!(input >> candidate.minutes) || !input.get(separator) || (separator != ' ' && separator != '\t')) { return false; } if (!(input >> done) || (done != 0 && done != 1)) { return false; } // Require whitespace between the done flag and the title. if (!input.get(separator) || (separator != ' ' && separator != '\t')) { return false; } std::getline(input >> std::ws, candidate.title); candidate.done = done == 1; if (!valid_task(candidate)) { return false; } out = candidate; return true; } bool add_task(std::vector<Task>& tasks, const Task& task) { if (tasks.size() >= 100 || !valid_task(task)) { return false; } tasks.push_back(task); return true; } Summary summarize(const std::vector<Task>& tasks) { Summary result; for (const Task& task : tasks) { result.planned += task.minutes; if (task.done) { result.completed += task.minutes; } else { result.remaining += task.minutes; } } return result; } bool complete_task(std::vector<Task>& tasks, std::size_t index) { if (index >= tasks.size()) { return false; } tasks[index].done = true; return true; } #ifndef STUDY_PLANNER_H #define STUDY_PLANNER_H #include <string> #include <vector> struct Task { std::string title; int minutes = 0; bool done = false; }; struct Summary { int planned = 0; int completed = 0; int remaining = 0; }; // One line: minutes, 0/1 done flag, then a title of 1..40 characters. // Durations are 1..180. Failure leaves out unchanged. bool parse_task(const std::string& line, Task& out); // At most 100 tasks. Rejection leaves the vector unchanged. bool add_task(std::vector<Task>& tasks, const Task& task); // Caller supplies tasks accepted by add_task. Summary summarize(const std::vector<Task>& tasks); // Invalid indexes fail without changing any task. Repeating completion is safe. bool complete_task(std::vector<Task>& tasks, std::size_t index); #endif
Application: main.cpp C++
#include "planner.h" #include <iostream> int main() { std::vector<Task> tasks; std::string line; int rejected = 0; while (std::getline(std::cin, line)) { // Accept both LF and CRLF text pasted from a local file. if (!line.empty() && line.back() == '\r') { line.pop_back(); } Task task; if (!parse_task(line, task) || !add_task(tasks, task)) { ++rejected; } } if (std::cin.bad()) { std::cerr << "Could not read input\n"; return 1; } for (const Task& task : tasks) { std::cout << (task.done ? "Done: " : "Todo: ") << task.title << " (" << task.minutes << " min)\n"; } const Summary result = summarize(tasks); std::cout << "Tasks: " << tasks.size() << '\n'; std::cout << "Planned: " << result.planned << " minutes\n"; std::cout << "Completed: " << result.completed << " minutes\n"; std::cout << "Remaining: " << result.remaining << " minutes\n"; std::cout << "Rejected: " << rejected << '\n'; return 0; } #include "planner.h" #include <sstream> bool valid_task(const Task& task) { return task.minutes >= 1 && task.minutes <= 180 && !task.title.empty() && task.title.size() <= 40 && task.title.find_first_not_of(" \t") != std::string::npos && task.title.find_first_of("\r\n") == std::string::npos; } bool parse_task(const std::string& line, Task& out) { if (line.find_first_of("\r\n") != std::string::npos) { return false; } std::istringstream input(line); Task candidate; int done = 0; char separator = '\0'; if (!(input >> candidate.minutes) || !input.get(separator) || (separator != ' ' && separator != '\t')) { return false; } if (!(input >> done) || (done != 0 && done != 1)) { return false; } // Require whitespace between the done flag and the title. if (!input.get(separator) || (separator != ' ' && separator != '\t')) { return false; } std::getline(input >> std::ws, candidate.title); candidate.done = done == 1; if (!valid_task(candidate)) { return false; } out = candidate; return true; } bool add_task(std::vector<Task>& tasks, const Task& task) { if (tasks.size() >= 100 || !valid_task(task)) { return false; } tasks.push_back(task); return true; } Summary summarize(const std::vector<Task>& tasks) { Summary result; for (const Task& task : tasks) { result.planned += task.minutes; if (task.done) { result.completed += task.minutes; } else { result.remaining += task.minutes; } } return result; } bool complete_task(std::vector<Task>& tasks, std::size_t index) { if (index >= tasks.size()) { return false; } tasks[index].done = true; return true; } #ifndef STUDY_PLANNER_H #define STUDY_PLANNER_H #include <string> #include <vector> struct Task { std::string title; int minutes = 0; bool done = false; }; struct Summary { int planned = 0; int completed = 0; int remaining = 0; }; // One line: minutes, 0/1 done flag, then a title of 1..40 characters. // Durations are 1..180. Failure leaves out unchanged. bool parse_task(const std::string& line, Task& out); // At most 100 tasks. Rejection leaves the vector unchanged. bool add_task(std::vector<Task>& tasks, const Task& task); // Caller supplies tasks accepted by add_task. Summary summarize(const std::vector<Task>& tasks); // Invalid indexes fail without changing any task. Repeating completion is safe. bool complete_task(std::vector<Task>& tasks, std::size_t index); #endif 25 0 Read chapter 10 1 Review notes bad line 180 0 Practice
Hint 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:

C++
// 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";:

C++
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:

C++
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:

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

Was this article helpful?

Your response is saved on this device.