cppbeginner10 minutes

Simple Task Tracker Mini-Project in C++

Build a basic console-based task tracker app in C++ that allows users to add, view, and remove tasks. Practice working with vectors, loops, and conditionals while creating a helpful daily tool.

Challenge prompt

Create a simple task tracker program in C++ with the following features: 1. Display a menu to the user with options to add a task, remove a task by its number, view all tasks, and exit. 2. Use a vector<string> to store the tasks. 3. After adding or removing a task, show the updated list. 4. The program should keep running until the user chooses to exit. Your program should handle invalid input gracefully and provide clear prompts to the user.

Guidance

  • Use a vector to store the tasks dynamically as the user adds them.
  • Use a loop to repeatedly show the menu and process user choices.
  • Remember to check if the task number for removal is valid before deleting.

Hints

  • Use std::getline to read the whole line for task descriptions.
  • You can use vector::erase(tasks.begin() + index) to remove a task at a specific position.
  • Displaying the tasks with their indices helps users know which number to remove.

Starter code

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

int main() {
    std::vector<std::string> tasks;
    int choice;
    do {
        std::cout << "Task Tracker Menu:\n";
        std::cout << "1. Add Task\n";
        std::cout << "2. Remove Task\n";
        std::cout << "3. View All Tasks\n";
        std::cout << "4. Exit\n";
        std::cout << "Choose an option: ";
        std::cin >> choice;
        std::cin.ignore(); // to ignore newline after number input

        if (choice == 1) {
            // Add task logic
        } else if (choice == 2) {
            // Remove task logic
        } else if (choice == 3) {
            // View tasks logic
        }
    } while (choice != 4);
    std::cout << "Goodbye!" << std::endl;
    return 0;
}

Expected output

Task Tracker Menu: 1. Add Task 2. Remove Task 3. View All Tasks 4. Exit Choose an option: 1 Enter a new task: Buy groceries Task added. Task Tracker Menu: 1. Add Task 2. Remove Task 3. View All Tasks 4. Exit Choose an option: 3 Tasks: 1. Buy groceries Task Tracker Menu: 1. Add Task 2. Remove Task 3. View All Tasks 4. Exit Choose an option: 4 Goodbye!

Core concepts

vectorsloopsconditionalsinput/output

Challenge a Friend

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