Skip to content

A Record for Each Task

The Course
•
35 min study

A planner task has three facts: a title, a duration and whether it is done. You could keep three vectors, one of titles, one of minutes and one of flags, and trust that position 2 in each describes the same task. That holds until one line of code adds a title without adding its minutes, or removes a flag from the middle. From then on every later task is paired with the wrong data, and nothing in the program notices.

A struct removes the problem by grouping related fields into one record. Our Task has a std::string title, an int number of minutes and a bool done flag. A std::vector<Task> then stores whole records, so a duration can never drift away from its title. Adding, removing or copying a task moves all three facts together.

Predict and run

Predict before running C++
#include <iostream>
#include <string>
#include <vector>

struct Task {
    std::string title;
    int minutes = 0;
    bool done = false;
};

int main() {
    std::vector<Task> tasks{{"Read", 25, false}, {"Review", 10, true}};
    for (const Task& task : tasks) {
        std::cout << task.title << " " << task.minutes << " " << task.done << "\n";
    }
    return 0;
}
Check your prediction
Text
Read 25 0
Review 10 1

Each pair of braces inside the vector's initializer built one Task, supplying the fields in the order they are declared: title, minutes, done. The loop reads each record through const Task&, so no title is copied and nothing can be changed by accident. Change only the second task's flag to false and confirm that only the last number of the second line changes.

Define, build and read a record

The struct definition lists each field with its type and ends with a semicolon after the closing brace. Forgetting that semicolon produces a confusing error on the next line of the file, so check for it first when a struct refuses to compile.

= 0 and = false are default member initializers. They give a field a predictable value whenever you build a Task without supplying that field. A dot reads or writes one field, as in task.minutes.

Defaults and field access C++
#include <iostream>
#include <string>

struct Task {
    std::string title;
    int minutes = 0;
    bool done = false;
};

int main() {
    Task empty;
    Task partial{"Practice"};
    Task full{"Review", 10, true};
    partial.minutes = 30;
    std::cout << "[" << empty.title << "] " << empty.minutes << " " << empty.done << "\n";
    std::cout << partial.title << " " << partial.minutes << " " << partial.done << "\n";
    std::cout << full.title << " " << full.minutes << " " << full.done << "\n";
    return 0;
}
Text
[] 0 0
Practice 30 0
Review 10 1

empty used every default; a std::string with no value is an empty string, which is why the brackets have nothing between them. partial supplied only the title and took the defaults for the rest, then had its minutes assigned. Without the default initializers, Task empty; would leave minutes and done without values, and reading them would be undefined behavior, just like an uninitialized int. Give every plain number and bool field a default.

A record is not printable by itself

std::cout << task; does not compile. C++ does not guess how a Task should look to a reader:

Shell
main.cpp:12:15: error: invalid operands to binary expression ('ostream' (aka 'basic_ostream<char>') and 'Task')

Print the fields you want instead. A small helper keeps the format in one place, so every part of the program shows tasks the same way. That is what the practice below builds, and it is the same format the finished planner uses.

Parallel vectors versus one vector of records

Here is the fragile version the introduction described. Nothing stops the vectors from getting out of step:

C++
std::vector<std::string> titles{"Read", "Review"};
std::vector<int> minutes{25, 10};
titles.push_back("Practice");    // minutes still has only two entries
// minutes[2] now does not exist: reading it is undefined behavior

With std::vector<Task>, the equivalent tasks.push_back({"Practice", 30, false}); adds all three facts in a single step. There is no second vector to forget.

Practice

Exercise

Your turn

Define Task with a title, minutes defaulting to 0 and done defaulting to false. Write void print_task(const Task& task) that prints one line in the planner's format. Store three tasks and print each, then the count:

  • Todo: Read chapter (25 min)
  • Done: Review notes (10 min)
  • Todo: Practice (30 min)
  • Tasks: 3

Add the third task with push_back after building the vector with the first two.

Your workspace C++
#include <iostream>
#include <string>
#include <vector>

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

The helper chooses between two labels based on task.done, then prints the title and minutes. It only reads the task, so it takes a const reference.

Hint 2: a first step

The label can be chosen with if (task.done), or in one expression with (task.done ? "Done: " : "Todo: "), which picks the first text when the condition is true and the second otherwise.

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

struct Task {
    std::string title;
    int minutes = 0;
    bool done = false;
};

void print_task(const Task& task) {
    std::cout << (task.done ? "Done: " : "Todo: ") << task.title
              << " (" << task.minutes << " min)\n";
}

int main() {
    std::vector<Task> tasks{{"Read chapter", 25, false}, {"Review notes", 10, true}};
    tasks.push_back({"Practice", 30, false});
    for (const Task& task : tasks) {
        print_task(task);
    }
    std::cout << "Tasks: " << tasks.size() << "\n";
    return 0;
}
Text
Todo: Read chapter (25 min)
Done: Review notes (10 min)
Todo: Practice (30 min)
Tasks: 3

Each record carries its own title, minutes and flag, so the loop needs no index and cannot mix up data from different tasks. The ?: expression chooses between two values; it needs parentheses here because it sits inside an output chain. print_task owns the format, so changing min to minutes would be a one-line edit. Next you will see exactly what happens when a Task is copied, and how to change the original instead.

Was this article helpful?

Your response is saved on this device.