Viewing Tasks
Part of the Logic & Flow section of Coddy's C++ journey — lesson 20 of 56.
Challenge
EasyExtend your task list tool by implementing the view functionality to display all tasks in a numbered list format. This builds on the previous lesson by adding the ability to see what tasks have been stored in the system.
The following inputs will be provided:
- An integer
nrepresenting the number of tasks to add to the list - Then
nstrings representing the task descriptions
Your program should:
- Start with the same setup from the previous lesson (empty
std::vector<std::string>namedtasksand welcome message) - Read the number of tasks and add each task description to the vector using
push_back() - Check if the tasks vector is empty using the
.empty()method - If the vector is empty, print a message indicating there are no tasks
- If the vector contains tasks, iterate through it using a traditional for loop with an index
- Print each task as a numbered list, starting from 1 (not 0)
- Display the total number of tasks at the end
Use the following exact output format:
For empty task list:
Welcome to Task List Tool!
Menu Options:
1. Add Task
2. View Tasks
3. Quit
Task list system initialized and ready!
No tasks available.For non-empty task list:
Welcome to Task List Tool!
Menu Options:
1. Add Task
2. View Tasks
3. Quit
Task list system initialized and ready!
Your Tasks:
1. [first task]
2. [second task]
3. [third task]
...
Total tasks: [number of tasks]Use a traditional for loop with i starting from 0 and going to tasks.size(). When printing, display i + 1 to show task numbers starting from 1, and access each task using tasks[i].
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;
string taskDescription;
getline(cin, taskDescription);
tasks.push_back(taskDescription);
cout << "Task \"" << taskDescription << "\" added successfully!" << endl;
cout << "Total tasks: " << tasks.size() << 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