12.5 — Project: a to-do list app

📘 Chapter 12⏱ about 3 min read✍️ Beginner friendly
Read one box at a time. No rush.

A to-do list that remembers your tasks between runs, by saving them to a file.

Features#

  • add a task
  • list all tasks
  • done a task by number
  • tasks are saved to todo.txt and loaded at start

The full program#

C++▶ Run
#include <fstream>#include <iostream>#include <string>#include <vector> struct Task{    std::string text {};    bool done { false };}; std::vector<Task> load(const std::string& file){    std::vector<Task> tasks {};    std::ifstream in { file };    std::string line {};    while (std::getline(in, line))    {        if (line.size() < 2)            continue;        tasks.push_back({ line.substr(2), line[0] == 'x' });   // "x Buy milk" or "- Buy milk"    }    return tasks;} void save(const std::string& file, const std::vector<Task>& tasks){    std::ofstream out { file };    for (const auto& t : tasks)        out << (t.done ? 'x' : '-') << ' ' << t.text << '\n';} void list(const std::vector<Task>& tasks){    if (tasks.empty())        std::cout << "Nothing to do. Nice!\n";    for (std::size_t i { 0 }; i < tasks.size(); ++i)        std::cout << i + 1 << ". [" << (tasks[i].done ? 'x' : ' ') << "] " << tasks[i].text << '\n';} int main(){    const std::string file { "todo.txt" };    std::vector<Task> tasks { load(file) };    std::cout << "Commands: add <task>, list, done <number>, quit\n";     std::string command {};    while (std::cout << "> " && std::cin >> command && command != "quit")    {        if (command == "add")        {            std::string text {};            std::getline(std::cin >> std::ws, text);            tasks.push_back({ text, false });        }        else if (command == "list")            list(tasks);        else if (command == "done")        {            int n {};            std::cin >> n;            if (n >= 1 && n <= static_cast<int>(tasks.size()))                tasks[n - 1].done = true;            else                std::cout << "No task number " << n << '\n';        }        else            std::cout << "Unknown command\n";        save(file, tasks);    }    return 0;}
Output
Commands: add <task>, list, done <number>, quit> add Learn pointers> add Build a game> done 1> list1. [x] Learn pointers2. [ ] Build a game> quit

How it works#

  • Each task is a Task struct with text and a done flag.
  • The file stores one task per line: x for done, - for not done.
  • load reads the file with std::getline (11.5 — Reading and writing files). If the file doesn't exist yet, it just returns an empty list.
  • After every command, save writes the whole list back.
Tip

Online compilers usually can't keep files between runs. Run this one on your own computer to see the saving work.

Ideas to extend it#

  • A remove <number> command (use tasks.erase).
  • Due dates, or priorities with an enum class.
  • clear to delete all finished tasks.
Finished reading? Tick it off. Your progress is saved in this browser.

Tip: press ← / → to move between lessons, / to search.