Skip to content

The Study Planner Brief

The Course
•
25 min study

Every lesson this week built one piece: values and arithmetic, decisions and loops, functions with tested contracts, vectors and records, copies and lifetimes, and line-based parsing. Today those pieces become one program, a study planner that reads a list of tasks and reports planned, completed and remaining study time. Before writing or reading any of it, you need a precise brief. A vague brief such as "read some tasks" cannot be tested; a precise one tells you, for every possible line, what the program must do.

Input is one task per line: a duration, a 0/1 completion flag, and a title. The rules:

  • Durations are 1 through 180 minutes.
  • The flag is exactly 0 (to do) or 1 (done), separated from the minutes and the title by a space or tab.
  • The title is non-empty ASCII text of at most 40 characters after leading whitespace is removed, and not made only of spaces.
  • At most 100 tasks are stored.
  • Invalid lines, and valid lines beyond the 100th task, count as rejected. Later valid lines must still be processed.

Predict and run

Predict before running C++
#include <iostream>

int main() {
    int planned = 35;
    int completed = 10;
    std::cout << "Remaining: " << planned - completed << " minutes\n";
    return 0;
}
Check your prediction
Text
Remaining: 25 minutes

Hard-coded totals like these are useful as expected results: they are what you write down before running the real program. They cannot implement a planner, because the planner must calculate the same numbers from whatever input arrives.

The acceptance example

This input has three valid tasks and one bad line:

  • 25 0 Read chapter
  • 10 1 Review notes
  • bad line
  • 180 0 Practice

The planner lists the three accepted tasks in input order, as Todo: Read chapter (25 min), Done: Review notes (10 min) and Todo: Practice (180 min), then reports 3 tasks, 215 planned minutes, 10 completed, 205 remaining and 1 rejected. Work those numbers out yourself from the four lines before you trust them. Empty input must give zero tasks and zero for every total, not an error. Marking a task complete twice must not count its minutes twice.

Why these rules

Each rule exists for a reason you have already met:

  • The 1 to 180 range rejects zero and negative durations, which would make totals meaningless, and bounds each value.
  • The 100-task limit bounds the arithmetic. The largest possible planned total is 100 times 180, or 18,000 minutes, far below the largest int. Signed overflow is undefined behavior, so the brief makes it impossible rather than hoping it never happens.
  • Rejecting instead of fixing keeps the program honest. Clamping 500 to 180 would invent data the person never typed. Counting the line as rejected tells them something needs attention.
  • One line per task means one bad line costs one task. The rest of the input is still read.

Boundary cases the brief decides

A brief should settle the awkward cases before code does. The stream-based integer parser accepts signed notation, so +25 is a valid 25, while -25 is a number outside the range and is rejected. Leading title whitespace is removed, but trailing spaces count toward the 40-character limit. A line pasted from Windows may end in a carriage return; the application removes one trailing carriage return so those lines are not rejected. This is a small, defined text format, not a general CSV parser: commas and quotes have no special meaning.

Practice

Exercise

Your turn

The report's summary block is pure formatting: it receives numbers that were already calculated. Define struct Summary with planned, completed and remaining, each defaulting to 0. Write void print_report(const Summary& summary, int tasks, int rejected) that prints five lines in this exact format, shown for the acceptance example:

  • Tasks: 3
  • Planned: 215 minutes
  • Completed: 10 minutes
  • Remaining: 205 minutes
  • Rejected: 1

Call it for the acceptance example, then for an empty plan using a default Summary with 0 tasks and 0 rejected, so the output is ten lines.

Your workspace C++
#include <iostream>

int main() {
    // Define Summary and print_report above main.
    return 0;
}
Hint 1: the idea

Formatting receives already calculated totals. print_report does no arithmetic; it only prints the values it is given.

Hint 2: a first step

Build the first summary with Summary sample{215, 10, 205}; and the empty one with Summary empty;, which uses the defaults.

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

struct Summary {
    int planned = 0;
    int completed = 0;
    int remaining = 0;
};

void print_report(const Summary& summary, int tasks, int rejected) {
    std::cout << "Tasks: " << tasks << "\n";
    std::cout << "Planned: " << summary.planned << " minutes\n";
    std::cout << "Completed: " << summary.completed << " minutes\n";
    std::cout << "Remaining: " << summary.remaining << " minutes\n";
    std::cout << "Rejected: " << rejected << "\n";
}

int main() {
    Summary sample{215, 10, 205};
    print_report(sample, 3, 1);
    Summary empty;
    print_report(empty, 0, 0);
    return 0;
}
Text
Tasks: 3
Planned: 215 minutes
Completed: 10 minutes
Remaining: 205 minutes
Rejected: 1
Tasks: 0
Planned: 0 minutes
Completed: 0 minutes
Remaining: 0 minutes
Rejected: 0

Because the function only formats, both calls go through the same code, and the empty plan needed no special case: the defaults in Summary already describe "nothing planned." The real planner prints exactly these five lines after the task list. In the next lesson you will connect the input loop, the parser and the summary that produce these numbers.

Was this article helpful?

Your response is saved on this device.