Adding a Task
Part of the Logic & Flow section of Coddy's C++ journey — lesson 19 of 56.
Challenge
EasyExtend your task list tool by implementing the functionality to add new tasks to the system. This builds on the previous lesson's foundation by adding user interaction and task storage capabilities.
The following input will be provided:
- A string representing the task description to add to the list
Your program should:
- Start with the same setup from the previous lesson (empty
std::vector<std::string>namedtasksand welcome message) - Read a task description from the input
- Use
push_back()to add the task description to thetasksvector - Print a confirmation message showing that the task was successfully added
- Display the current number of tasks in the list using the vector's
.size()method
Use the following exact output format:
Welcome to Task List Tool!
Menu Options:
1. Add Task
2. View Tasks
3. Quit
Task list system initialized and ready!
Task "[task description]" added successfully!
Total tasks: [number of tasks]This exercise demonstrates how vectors can dynamically grow as you add elements, and how the .size() method reflects the current number of stored items. The confirmation message provides user feedback that the operation completed successfully.
Try it yourself
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
vector<string> tasks;
cout << "Welcome to Task List Tool!" << endl;
cout << endl;
cout << "Menu Options:" << endl;
cout << "1. Add Task" << endl;
cout << "2. View Tasks" << endl;
cout << "3. Quit" << endl;
cout << endl;
cout << "Task list system initialized and ready!" << endl;
return 0;
}All lessons in Logic & Flow
1Pointers and Memory
What is a Pointer?Address-Of OperatorDereference OperatorNull PointersPointers and ArraysDynamic Memory with 'new'Freeing Memory with 'delete'Recap - Pointer Practice2Vectors (Dynamic Arrays)
Introducing std::vectorCreating a VectorAdding ElementsAccessing ElementsVector SizeIterating with a For LoopRange-Based For LoopRemoving ElementsRecap - Vector Operations5Project: Inventory Tool
Project SetupAdding and Updating Items