Skip to content
C++ project #07Beginner

To-Do List Manager

Add, complete, undo, remove, filter and sort tasks by priority — a tidy class built on std::vector and the erase-remove idiom.

Estimated time
~2 hours to build
Files
5 files · 333 lines
Technologies
C++17, <vector>, <algorithm>, <sstream>

Introduction

A to-do list is small enough to finish in an afternoon but touches many everyday C++ skills: an enum class for priorities, a class that hides a vector, std::find_if, the erase-remove idiom and std::stable_sort.

Problem statement

Write a command-driven task manager. Tasks have an id, a description and a priority (high, medium, low). Users can add tasks, mark them done or pending, remove them, list all/pending/done tasks, sort (pending first, then by priority, keeping the original order among equals), clear finished tasks and see statistics.

Objectives

  • Encapsulate a collection behind a clear interface
  • Use std::find_if, std::remove_if and std::count_if
  • Understand why std::stable_sort keeps equal items in order
  • Read the rest of a line with std::getline and std::ws

Features

  • Task ids that never repeat, even after removal
  • Filtering by status
  • Stable priority sort
  • Statistics with a progress percentage

Concepts demonstrated

  • enum class
  • Erase-remove idiom
  • std::stable_sort
  • Lambdas
  • Command parsing

System architecture

Task is a plain struct. TodoList owns a std::vector<Task> and a running id counter. find returns a pointer (or nullptr) using std::find_if; remove and clear_done use std::remove_if followed by erase. sort_by_priority uses std::stable_sort with a comparator that orders by done-flag then priority. main parses each command; for add, the priority is the second word and the rest of the line is the description.

Folder structure

TextProject tree
todo-list-manager/
├── README.md
├── Makefile
├── src/main.cpp
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -O2
SRC := $(wildcard src/*.cpp)
HDR := $(wildcard src/*.h src/*.hpp)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CXX) $(CXXFLAGS) -Isrc $(SRC) -o app
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/main.cpp
Priority helpers, the Task struct, the TodoList class and the command loop in main.
Makefile
Build automation: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
g++ -std=c++17 -Wall -Wextra -Isrc src/*.cpp -o app
# or simply: make

Run

Terminal
./app # Linux / macOS
.\app.exe # Windows

Test against the expected output

Terminal
./app < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# Get-Content tests\sample-input.txt | .\app.exe > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
=== To-Do List ===
Commands: add <high|medium|low> <text> | done <id> | undo <id> | remove <id>
list [all|pending|done] | sort | clear-done | stats | help | quit
> Added #1
> Added #2
> Added #3
> Give a priority: high, medium or low
> The task needs a description
> Added #4
> #2 marked done
> [ ] #1 MED Read chapter 8 (STL)
[x] #2 HIGH Submit lab report
[ ] #3 LOW Clean the desk
[ ] #4 HIGH Revise pointers
> Sorted by status and priority
> [ ] #4 HIGH Revise pointers
[ ] #1 MED Read chapter 8 (STL)
[ ] #3 LOW Clean the desk
[x] #2 HIGH Submit lab report
> [ ] #4 HIGH Revise pointers
[ ] #1 MED Read chapter 8 (STL)
[ ] #3 LOW Clean the desk
> 4 tasks, 1 done, 3 pending (1 high priority)
Progress: 25%
> #2 marked pending
> #3 marked done
> No task #9
> Cleared 1 finished task(s)
> [ ] #4 HIGH Revise pointers
[ ] #1 MED Read chapter 8 (STL)
[ ] #2 HIGH Submit lab report
> 3 tasks, 0 done, 3 pending (2 high priority)
Progress: 0%
>
Bye!
Input
add medium Read chapter 8 (STL)
add high Submit lab report
add low Clean the desk
add urgent Pay fees
add high
add high Revise pointers
done 2
list
sort
list
list pending
stats
undo 2
done 3
remove 9
clear-done
list all
stats
quit

Testing

  • Compare the scripted session with tests/expected-output.txt.
  • After sort, task #4 (high) comes before #1 (medium) and #3 (low); the finished #2 moves to the end.
  • Invalid priorities and empty descriptions are rejected.

Common errors

The description loses its first word

Read the priority with >>, then use std::getline(in >> std::ws, text) for the rest.

Removing inside a for loop skips items or crashes

Don't erase while iterating with a range-for; use the erase-remove idiom.

Possible improvements

  • Save tasks to a file and load them at start-up
  • Add due dates and an overdue filter
  • Add tags and filter by tag

Additional challenges

  1. 1Add an edit <id> <text> command.
  2. 2Support undo of the last command using a stack of snapshots.
  3. 3Print tasks grouped under priority headings.

Built and tested it yourself? Mark it to update your dashboard.