C++ Mini Projects: Simple to-do List
Hey guys, welcome back to the blog! We're continuing the series with another awesome addition of a simple todo list using your skills by coding in C++.
In case you're not sure, a todo list is a list of things that you have in mind to do. This list helps you keep a track.
Programmatically our task is to create a functionality like this:
First you add new tasks to your list
Second you display the tasks according to their respective numbers
Third you have ability to delete a particular task according to its number
Fourth you exit the app
Skills you will learn throughout:
Vectors
Loops
Menu management system
Vectors: To manage task list dynamically
Loops.... for interaction of menu continuously
Menu management system....an interface for selecting options in a user friendly way
So lets get started on coding!
The code to build the desired project:
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main() {
vector<string> todoList; // Vector to store tasks
int choice; // Menu choice
string task; // Task input by user
int taskNumber; // Task number to delete
do {
// Display menu
cout << "\nSimple To-Do List\n";
cout << "-----------------\n";
cout << "1. Add Task\n";
cout << "2. View Tasks\n";
cout << "3. Delete Task\n";
cout << "4. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
// Add a task
cin.ignore(); // Clear input buffer
cout << "Enter the task: ";
getline(cin, task); // Get full task input
todoList.push_back(task); // Add task to vector
cout << "Task added successfully.\n";
break;
case 2:
// View tasks
if (todoList.empty()) {
cout << "Your to-do list is empty.\n";
} else {
cout << "Your To-Do List:\n";
for (size_t i = 0; i < todoList.size(); ++i) {
cout << i + 1 << ". " << todoList[i] << endl;
}
}
break;
case 3:
// Delete a task
if (todoList.empty()) {
cout << "Your to-do list is empty. Nothing to delete.\n";
} else {
cout << "Enter the task number to delete: ";
cin >> taskNumber;
if (taskNumber > 0 && taskNumber <= static_cast<int>(todoList.size())) {
todoList.erase(todoList.begin() + taskNumber - 1); // Delete the task
cout << "Task deleted successfully.\n";
} else {
cout << "Invalid task number!\n";
}
}
break;
case 4:
// Exit the program
cout << "Exiting the to-do list. Goodbye!\n";
break;
default:
cout << "Invalid choice. Please try again.\n";
}
} while (choice != 4); // Continue until user chooses to exit
return 0;
}
How the code works...
Vectors:
vector<string> todoList;
A vector works like an array that grows and then shrinks when you run code.
In the vector all tasks can be stored as strings
Menu display
cout << "1. Add Task\n";
cout << "2. View Tasks\n";
cout << "3. Delete Task\n";
cout << "4. Exit\n";
cin >> choice;
Menu system is used to guide the user
Selection of user is stored in choice
Add a task
cin.ignore();
cout << "Enter the task: ";
getline(cin, task);
todoList.push_back(task);
Cin.ignore() is used for clearing the input buffer so that getline has no issues
Getline (cin,task)
Fully reads the input tasks with all spaces
Push_back (task)
Its for adding the tasks to vectors
View a task
for (size_t i = 0; i < todoList.size(); ++i) {
cout << i + 1 << ". " << todoList[i] << endl;
}
Loops through vectors for displaying all task with their numbers
Delete tasks
if (taskNumber > 0 && taskNumber <= static_cast<int>(todoList.size())) {
todoList.erase(todoList.begin() + taskNumber - 1);
}
Validates the input
Makes sure that task that is entered is correct and valid
Erase.... delete a task
Quitting the program
while (choice != 4);
Program keeps looping till user chooses option no. 4
Hope you enjoyed and learned something new from this blog.
See you next time!
Comments
Post a Comment