Todo Application in C++

1. Introduction

Building a To-Do App is a classic project for improving your programming skills, understanding basic data structures, and learning how to manipulate them. In this tutorial, we'll develop a console-based To-Do application in C++ that will enable users to add tasks, view all tasks, update existing tasks, delete tasks, and search for tasks by name.
Todo Application in CPP

2. Program Steps

1. Define a Task structure with properties like ID, name, and description.

2. Implement functions for adding, displaying, updating, deleting, and searching tasks.

3. Create a vector to store tasks.

4. Develop a user-friendly console menu to interact with the application.

3. Code Program

#include <iostream>
#include <vector>
#include <algorithm>
#include <string>

using namespace std;

struct Task {
    int id;
    string name;
    string description;
};

vector<Task> tasks;
int currentId = 1;

void addTask() {
    Task task;
    task.id = currentId++;
    cout << "Enter task name: ";
    getline(cin, task.name);
    cout << "Enter task description: ";
    getline(cin, task.description);
    tasks.push_back(task);
    cout << "Task added successfully.\n";
}

void displayTasks() {
    for (const auto& task : tasks) {
        cout << "ID: " << task.id << ", Name: " << task.name << ", Description: " << task.description << endl;
    }
}

void updateTask() {
    int id;
    cout << "Enter task ID to update: ";
    cin >> id;
    auto it = find_if(tasks.begin(), tasks.end(), [&](const Task& task) { return task.id == id; });
    if (it != tasks.end()) {
        cout << "Enter new name: ";
        cin.ignore();
        getline(cin, it->name);
        cout << "Enter new description: ";
        getline(cin, it->description);
        cout << "Task updated successfully.\n";
    } else {
        cout << "Task not found.\n";
    }
}

void deleteTask() {
    int id;
    cout << "Enter task ID to delete: ";
    cin >> id;
    auto it = remove_if(tasks.begin(), tasks.end(), [&](const Task& task) { return task.id == id; });
    if (it != tasks.end()) {
        tasks.erase(it, tasks.end());
        cout << "Task deleted successfully.\n";
    } else {
        cout << "Task not found.\n";
    }
}

void searchTask() {
    string query;
    cout << "Enter task name to search: ";
    // cin.ignore() removed from here
    getline(cin, query);
    auto it = find_if(tasks.begin(), tasks.end(), [&](const Task& task) {
        return task.name == query;
    });
    if (it != tasks.end()) {
        cout << "ID: " << it->id << ", Name: " << it->name << ", Description: " << it->description << endl;
    } else {
        cout << "Task not found.\n";
    }
}


int main() {
    int choice;
    do {
        cout << "\nTodo App\n";
        cout << "1. Add Task\n";
        cout << "2. View Tasks\n";
        cout << "3. Update Task\n";
        cout << "4. Delete Task\n";
        cout << "5. Search Task\n";
        cout << "6. Exit\n";
        cout << "Enter your choice: ";
        cin >> choice;
        cin.ignore();

        switch(choice) {
            case 1: addTask(); break;
            case 2: displayTasks(); break;
            case 3: updateTask(); break;
            case 4: deleteTask(); break;
            case 5: searchTask(); break;
            case 6: cout << "Exiting...\n"; break;
            default: cout << "Invalid choice.\n";
        }
    } while(choice != 6);

    return 0;
}

Output:

/tmp/LT8FV23Eyg.o
Todo App
1. Add Task
2. View Tasks
3. Update Task
4. Delete Task
5. Search Task
6. Exit
Enter your choice: 1
Enter task name: Learn C++
Enter task description: Learn C++ with Examples
Task added successfully.
Todo App
1. Add Task
2. View Tasks
3. Update Task
4. Delete Task
5. Search Task
6. Exit
Enter your choice: 1
Enter task name: Build Projects using C++
Enter task description: Build Projects using C++
Task added successfully.
Todo App
1. Add Task
2. View Tasks
3. Update Task
4. Delete Task
5. Search Task
6. Exit
Enter your choice: 2
ID: 1, Name: Learn C++, Description: Learn C++ with Examples
ID: 2, Name: Build Projects using C++, Description: Build Projects using C++
Todo App
1. Add Task
2. View Tasks
3. Update Task
4. Delete Task
5. Search Task
6. Exit
Enter your choice: 3
Enter task ID to update: 1
Enter new name: Learn C++ / CPP
Enter new description: Learn C++ with Examples
Task updated successfully.
Todo App
1. Add Task
2. View Tasks
3. Update Task
4. Delete Task
5. Search Task
6. Exit
Enter your choice: 4
Enter task ID to delete: 1
Task deleted successfully.
Todo App
1. Add Task
2. View Tasks
3. Update Task
4. Delete Task
5. Search Task
6. Exit
Enter your choice: 5
Enter task name to search: Build Projects using C++
ID: 2, Name: Build Projects using C++, Description: Build Projects using C++
Todo App
1. Add Task
2. View Tasks
3. Update Task
4. Delete Task
5. Search Task
6. Exit
Enter your choice: 6
Exiting...

Explanation:

1. The Task structure is defined to hold each task's information.

2. A vector named tasks serves as the database to store tasks.

3. Functions for each CRUD operation manipulate the tasks vector to add, display, update, and delete tasks. The search operation finds a task by name.

4. The main function presents a menu-driven interface, enabling users to select actions like adding or deleting tasks. 

5. This simple To-Do application in C++ showcases basic programming concepts such as data structures, control flow, and standard library usage.

Comments