CodeBlanks Logo Icon CodeBlanks

Business logic

Write business rules with functions and conditional logic.

addTask and listTasks functions

We will move the add and list operations into functions. The addTask function creates a new Task and adds it to the tasks vector. The listTasks function uses a range-based for loop to print every task.

Giving functions parameters makes the code flexible; you can call the same function with different titles and statuses. A vector<Task>& reference parameter lets you modify the vector without copying it. In this lesson we will define both functions and use them in main.

Listing with a range for

Inside the listTasks function, the for (Task t : tasks) loop copies each task in turn into t. In the loop body we access the task's details with t.title and t.status.

For example, for (Task t : tasks) { cout << t.title << " - " << t.status << endl; } prints each task on a new line. The range for is a safe and short way to walk through all tasks without knowing the vector's length.

Practice Add addTask and listTasks

Define void addTask(vector<Task>& tasks, string title, string status); inside, create a new Task, assign title and status to its members, then add with tasks.push_back(t);. Define void listTasks(const vector<Task>& tasks); with a range for print each task with cout << t.title << " - " << t.status << endl;. In main, after printMenu();, add calls addTask(tasks, "Homework", "pending");, addTask(tasks, "Sport", "pending"); and listTasks(tasks);.

Starter code

main.cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct Task {
    string title;
    string status;
};

void printMenu() {
    cout << "1 - Add" << endl;
    cout << "2 - List" << endl;
    cout << "3 - Exit" << endl;
}

// Define the addTask and listTasks functions

int main() {
    printMenu();
    vector<Task> tasks;
    // add addTask and listTasks calls
    return 0;
}

Sample solution

main.cpp
#include <iostream>
#include <string>
#include <vector>
using namespace std;

struct Task {
    string title;
    string status;
};

void printMenu() {
    cout << "1 - Add" << endl;
    cout << "2 - List" << endl;
    cout << "3 - Exit" << endl;
}

void addTask(vector<Task>& tasks, string title, string status) {
    Task t;
    t.title = title;
    t.status = status;
    tasks.push_back(t);
}

void listTasks(const vector<Task>& tasks) {
    for (Task t : tasks) {
        cout << t.title << " - " << t.status << endl;
    }
}

int main() {
    printMenu();
    vector<Task> tasks;
    addTask(tasks, "Homework", "pending");
    addTask(tasks, "Sport", "pending");
    listTasks(tasks);
    return 0;
}

Expected output

1 - Add
2 - List
3 - Exit
Homework - pending
Sport - pending

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 How is the vector parameter of addTask declared?

  • vector<Task> (by value)
  • vector<Task>* (pointer)
  • vector<Task>& (reference) Correct answer
  • Task& (reference)

Explanation: A vector<Task>& reference avoids copying.

2 Fill in the blank: for loop

Question
for (Task t : ____) {
    cout << t.title << " - " << t.status << endl;
}
Answer
for (Task t : tasks) {
    cout << t.title << " - " << t.status << endl;
}

Explanation: The range for walks the tasks vector.

3 Match each function with its job

Term Meaning
addTask adds a task
listTasks prints tasks
for (Task t : tasks) iterates the list

All lessons in Beginner