A to-do list is generally used list all the task that a person wants or needs to do and then track his progress on it. This is a simple yet effective way to get the work done. In this article, we will learn how to create a console based C++ program to implement to do list. This application will allow you to add, delete, display, edit, and mark tasks as completed.
Features of the To Do List Program
This to do list provides the following features:
- Add Tasks: Users can add new tasks to the list, specifying details such as task name, description, and due date.
- View Tasks: The program can display all tasks in the list, showing details like task name, description, and status.
- Delete Tasks: Users can remove tasks from the list when they are no longer needed or completed.
- Mark Tasks as Completed: Tasks can be marked as completed, helping users track their progress and distinguish between pending and finished tasks.
- Edit Tasks: Users can modify existing tasks, updating information like the task name, description, or due date
Implementation of the Features
We first created two classes:
- Tasks: This represents a single task and all the required methods of it.
- ToDoList: This represents the to do list that consists of a tasks list and the methods to provide different features of the project.
Now, we implement the to do list features as shown below:
Add Tasks:
- Prompt for task details (name, description, due date).
- Create and add a new Task object to the tasks vector.
- Confirm task addition.
View Tasks:
- Check if tasks vector is empty.
- If not, display each task's details using displayTask().
Delete Tasks:
- Check if tasks vector is empty.
- If not, display task list and prompt for task number to delete.
- Validate and delete the selected task.
Mark Tasks as Completed:
- Check if tasks vector is empty.
- If not, display task list and prompt for task number to mark as completed.
- Validate and mark the selected task as completed.
Edit Tasks:
- Check if tasks vector is empty.
- If not, display task list and prompt for task number to edit.
- Validate and prompt for new task details.
- Update the task with new details.
C++ Program to Implement To Do List
// C++ Program to implement the to do list
#include <iostream>
#include <string>
#include <vector>
using namespace std;
// Define Task class
class Task {
private:
string name; // Task name
string description; // Task description
string dueDate; // Task due date
bool completed; // Task completion status
public:
// Constructor to initialize a task
Task(const string& name, const string& description,
const string& dueDate)
: name(name)
, description(description)
, dueDate(dueDate)
, completed(false)
{
}
// Getter for task name
string getName() const { return name; }
// Getter for task description
string getDescription() const { return description; }
// Getter for task due date
string getDueDate() const { return dueDate; }
// Getter for task completion status
bool isCompleted() const { return completed; }
// Setter for task name
void setName(const string& name) { this->name = name; }
// Setter for task description
void setDescription(const string& description)
{
this->description = description;
}
// Setter for task due date
void setDueDate(const string& dueDate)
{
this->dueDate = dueDate;
}
// Mark the task as completed
void markCompleted() { completed = true; }
// Display task details
void displayTask() const
{
cout << name << " ("
<< (completed ? "Completed" : "Pending")
<< ") - Due: " << dueDate << endl
<< " Description: " << description << endl;
}
};
// Define ToDoList class
class ToDoList {
private:
vector<Task> tasks; // List of tasks
public:
// Display the menu
void displayMenu()
{
cout
<< "\n---------- To-Do List Menu -----------\n";
cout << "1. Add Task\n";
cout << "2. Delete Task\n";
cout << "3. Display Tasks\n";
cout << "4. Mark Task as Completed\n";
cout << "5. Edit Task\n";
cout << "6. Exit\n";
cout << "-----------------------------------------"
"\n";
}
// Add a new task
void addTask()
{
string name, description, dueDate;
cout << "Enter task name: ";
cin.ignore();
getline(cin, name);
cout << "Enter task description: ";
getline(cin, description);
cout << "Enter task due date (YYYY-MM-DD): ";
getline(cin, dueDate);
tasks.emplace_back(name, description, dueDate);
cout << "Task added successfully!" << endl;
}
// Delete a task
void deleteTask()
{
if (tasks.empty()) {
cout << "No tasks to delete!" << endl;
return;
}
cout << "Tasks:" << endl;
for (int i = 0; i < tasks.size(); ++i) {
cout << i + 1 << ". " << tasks[i].getName()
<< endl;
}
cout << "Enter the task number to delete: ";
int taskNumber;
cin >> taskNumber;
if (taskNumber >= 1 && taskNumber <= tasks.size()) {
tasks.erase(tasks.begin() + taskNumber - 1);
cout << "Task deleted successfully!" << endl;
}
else {
cout << "Invalid task number!" << endl;
}
}
// Display all tasks
void displayTasks()
{
if (tasks.empty()) {
cout << "No tasks to display!" << endl;
return;
}
cout << "Tasks:" << endl;
for (int i = 0; i < tasks.size(); ++i) {
cout << i + 1 << ". ";
tasks[i].displayTask();
}
}
// Mark a task as completed
void markTaskCompleted()
{
if (tasks.empty()) {
cout << "No tasks to mark as completed!"
<< endl;
return;
}
cout << "Tasks:" << endl;
for (int i = 0; i < tasks.size(); ++i) {
cout << i + 1 << ". " << tasks[i].getName()
<< endl;
}
cout << "Enter the task number to mark as "
"completed: ";
int taskNumber;
cin >> taskNumber;
if (taskNumber >= 1 && taskNumber <= tasks.size()) {
tasks[taskNumber - 1].markCompleted();
cout << "Task marked as completed!" << endl;
}
else {
cout << "Invalid task number!" << endl;
}
}
// Edit a task
void editTask()
{
if (tasks.empty()) {
cout << "No tasks to edit!" << endl;
return;
}
cout << "Tasks:" << endl;
for (int i = 0; i < tasks.size(); ++i) {
cout << i + 1 << ". " << tasks[i].getName()
<< endl;
}
cout << "Enter the task number to edit: ";
int taskNumber;
cin >> taskNumber;
if (taskNumber >= 1 && taskNumber <= tasks.size()) {
Task& task = tasks[taskNumber - 1];
string name, description, dueDate;
cout << "Enter new task name (current: "
<< task.getName() << "): ";
cin.ignore();
getline(cin, name);
cout << "Enter new task description (current: "
<< task.getDescription() << "): ";
getline(cin, description);
cout << "Enter new task due date (current: "
<< task.getDueDate() << "): ";
getline(cin, dueDate);
task.setName(name);
task.setDescription(description);
task.setDueDate(dueDate);
cout << "Task updated successfully!" << endl;
}
else {
cout << "Invalid task number!" << endl;
}
}
// Run the to-do list application
void run()
{
int choice;
do {
displayMenu();
cout << "Enter your choice: ";
cin >> choice;
switch (choice) {
case 1:
addTask();
break;
case 2:
deleteTask();
break;
case 3:
displayTasks();
break;
case 4:
markTaskCompleted();
break;
case 5:
editTask();
break;
case 6:
cout << "Exiting program. Bye!" << endl;
break;
default:
cout << "Invalid choice. Please try again!"
<< endl;
}
} while (choice != 6);
}
};
// Main function
int main()
{
// Create a ToDoList object and run the application
ToDoList toDoList;
toDoList.run();
return 0;
}
Output