Menu
Coddy logo textTech

Finishing the Tool

Part of the Logic & Flow section of Coddy's C++ journey — lesson 22 of 56.

challenge icon

Challenge

Easy

Complete your task list tool by implementing the main program loop that ties all functionality together. This final lesson creates a fully functional task management application that repeatedly displays the menu and processes user commands until they choose to quit.

The following inputs will be provided:

  • A series of menu choices (integers: 1 for Add Task, 2 for View Tasks, 3 for Remove Task, 4 for Quit)
  • For Add Task (choice 1): a string representing the task description
  • For Remove Task (choice 3): an integer representing the task number to remove (1-based indexing)

Your program should:

  1. Start with the same setup from previous lessons (empty std::vector<std::string> named tasks and welcome message)
  2. Create a main loop that continues until the user chooses to quit
  3. Display the menu and read the user's choice for each iteration
  4. Process each menu option:
    • Choice 1: Read a task description and add it using push_back()
    • Choice 2: Display all tasks in numbered format, or show "No tasks available." if empty
    • Choice 3: Read a task number, validate it, and remove the task using .erase()
    • Choice 4: Exit the program loop
  5. For invalid menu choices, display an error message and continue the loop
  6. Print a goodbye message when the user quits

Use the following exact output format:

Initial setup and menu display:

Welcome to Task List Tool!

Menu Options:
1. Add Task
2. View Tasks
3. Remove Task
4. Quit

Task list system initialized and ready!

For each menu iteration, display:

Choose an option: 

For Add Task (choice 1):

Task "[task description]" added successfully!
Total tasks: [number of tasks]

For View Tasks (choice 2) with tasks:

Your Tasks:
1. [first task]
2. [second task]
...
Total tasks: [number of tasks]

For View Tasks (choice 2) when empty:

No tasks available.

For Remove Task (choice 3) with valid task number:

Task "[removed task description]" removed successfully!
Remaining Tasks:
1. [first remaining task]
...
Total tasks: [updated number of tasks]

For Remove Task (choice 3) with invalid task number:

Error: Invalid task number. Please enter a number between 1 and [total tasks].

For invalid menu choice:

Invalid choice. Please try again.

When user quits (choice 4):

Thank you for using Task List Tool!

Use a while loop that continues until the user enters 4. For the Remove Task functionality, remember to validate that the task number is between 1 and tasks.size() before using tasks.erase(tasks.begin() + index). If the list becomes empty after removal, print "No tasks remaining." instead of the remaining tasks list.

Try it yourself

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

int main() {
    std::vector<std::string> tasks;
    
    std::cout << "Welcome to Task List Tool!" << std::endl;
    std::cout << std::endl;
    std::cout << "Menu Options:" << std::endl;
    std::cout << "1. Add Task" << std::endl;
    std::cout << "2. View Tasks" << std::endl;
    std::cout << "3. Quit" << std::endl;
    std::cout << std::endl;
    std::cout << "Task list system initialized and ready!" << std::endl;
    
    int n;
    std::cin >> n;
    std::cin.ignore();
    
    for (int i = 0; i < n; i++) {
        std::string task;
        std::getline(std::cin, task);
        tasks.push_back(task);
    }
    
    int taskNumber;
    std::cin >> taskNumber;
    
    if (taskNumber < 1 || taskNumber > tasks.size()) {
        std::cout << "Error: Invalid task number. Please enter a number between 1 and " << tasks.size() << "." << std::endl;
    } else {
        std::string removedTask = tasks[taskNumber - 1];
        tasks.erase(tasks.begin() + (taskNumber - 1));
        
        std::cout << "Task \"" << removedTask << "\" removed successfully!" << std::endl;
        
        if (tasks.empty()) {
            std::cout << "No tasks remaining." << std::endl;
        } else {
            std::cout << "Remaining Tasks:" << std::endl;
            for (int i = 0; i < tasks.size(); i++) {
                std::cout << (i + 1) << ". " << tasks[i] << std::endl;
            }
        }
        
        std::cout << "Total tasks: " << tasks.size() << std::endl;
    }
    
    return 0;
}

All lessons in Logic & Flow