cppbeginner10 minutes

Build a Simple To-Do List Application in C++

Create a beginner-friendly console-based to-do list app where users can add, view, and remove tasks.

Challenge prompt

Write a C++ program that allows a user to manage a simple to-do list. The program should repeatedly prompt the user to choose an action: add a new task, list all tasks, remove a task by number, or exit the program. Tasks should be stored in a dynamic structure, and the user can perform multiple actions until they choose to exit.

Guidance

  • Use a vector to store the list of tasks dynamically.
  • Implement a loop to continuously ask for user input until they want to exit.
  • Use functions to separate adding, listing, and removing tasks.

Hints

  • Consider using std::getline() to read the full task string from the user.
  • When removing a task, make sure to validate the user's input to avoid out-of-range errors.
  • Use a simple menu system with numbered options to interact with the user.

Starter code

#include <iostream>
#include <vector>
#include <string>

int main() {
    std::vector<std::string> tasks;
    int choice;
    while (true) {
        std::cout << "\nTo-Do List Menu:\n1. Add Task\n2. List Tasks\n3. Remove Task\n4. Exit\nChoose an option: ";
        std::cin >> choice;
        std::cin.ignore(); // Clear newline from input buffer

        if (choice == 1) {
            // Add task
        } else if (choice == 2) {
            // List tasks
        } else if (choice == 3) {
            // Remove task
        } else if (choice == 4) {
            std::cout << "Exiting program.\n";
            break;
        } else {
            std::cout << "Invalid option. Please try again.\n";
        }
    }
    return 0;
}

Expected output

User selects 1, adds "Buy groceries". User selects 2, sees: 1. Buy groceries User selects 1, adds "Finish homework". User selects 2, sees: 1. Buy groceries 2. Finish homework User selects 3, enters 1. User selects 2, sees: 1. Finish homework User selects 4, program exits.

Core concepts

vectorsloopsinput/outputfunctions

Challenge a Friend

Send this duel to someone else and see if they can solve it.