Menu
Coddy logo textTech

Removing a Task

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

challenge icon

Challenge

Easy

Extend your task list tool by implementing the remove functionality to delete specific tasks from the list. This builds on the previous lesson by adding the ability to remove tasks by their position number.

The following inputs will be provided:

  • An integer n representing the number of tasks to add initially
  • Then n strings representing the task descriptions
  • An integer taskNumber representing the task number to remove (1-based indexing)

Your program should:

  1. Start with the same setup from the previous lesson (empty std::vector<std::string> named tasks and welcome message)
  2. Read the number of tasks with cin >> n, then call cin.ignore() to discard the leftover newline before reading task strings with getline()
  3. Add each task description to the vector using push_back()
  4. Read the task number to remove
  5. Validate that the task number is within the valid range (1 to the size of the vector)
  6. If the task number is invalid, print an error message
  7. If the task number is valid, use the .erase() method to remove the task at the specified position
  8. Print a confirmation message showing which task was removed
  9. Display the remaining tasks in a numbered list format
  10. Show the updated total number of tasks

Important — mixing cin and getline():
When you read an integer with cin >> n, it leaves a newline character ('\n') in the input buffer. If you then call getline(), it will immediately read that leftover newline as an empty string instead of your actual input. To fix this, call cin.ignore() right after cin >> n to discard the leftover newline:

cin >> n;
cin.ignore(); // discard the leftover newline
for (int i = 0; i < n; i++) {
    string task;
    getline(cin, task); // now reads correctly
    tasks.push_back(task);
}

Use the following exact output format:

For invalid task number:

Welcome to Task List Tool!

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

Task list system initialized and ready!
Error: Invalid task number. Please enter a number between 1 and [total tasks].

For valid task removal:

Welcome to Task List Tool!

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

Task list system initialized and ready!
Task "[removed task description]" removed successfully!
Remaining Tasks:
1. [first remaining task]
2. [second remaining task]
...
Total tasks: [updated number of tasks]

Remember that .erase() requires an iterator, so use tasks.erase(tasks.begin() + index) where index is the 0-based position (taskNumber - 1). Validate the input by checking if taskNumber is greater than 0 and less than or equal to tasks.size(). 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;
    
    for (int i = 0; i < n; i++) {
        std::string task;
        std::cin >> task;
        tasks.push_back(task);
    }
    
    if (tasks.empty()) {
        std::cout << "No tasks available." << std::endl;
    } else {
        std::cout << "Your 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