cplusplus
cplusplus

 

Introduction

A To-Do list is a common feature found in many productivity applications. It helps individuals keep track of tasks, manage their time more effectively, and stay organized. In this tutorial, we will create a simple To-Do list application using C++. The application will allow the user to add, view, and delete tasks from the list.

Objective

The objective of this project is to introduce the concept of user input, data structures (in this case, an array or vector), and basic file handling techniques in C++. By the end of this project, you will have a functional To-Do list application that demonstrates basic C++ features like loops, conditionals, and functions.

Code


#include 
#include 
#include 
using namespace std;

class ToDoList {
private:
    vector tasks; // A vector to store the list of tasks

public:
    // Function to add a task to the list
    void addTask(const string& task) {
        tasks.push_back(task);
        cout << "Task added successfully." << endl;
    }

    // Function to view all tasks
    void viewTasks() {
        if (tasks.empty()) {
            cout << "Your To-Do list is empty!" << endl;
            return;
        }

        cout << "\nYour To-Do List:" << endl;
        for (size_t i = 0; i < tasks.size(); ++i) {
            cout << i + 1 << ". " << tasks[i] << endl;
        }
    }

    // Function to delete a task from the list
    void deleteTask(int index) {
        if (index < 1 || index > tasks.size()) {
            cout << "Invalid task number!" << endl;
            return;
        }
        tasks.erase(tasks.begin() + index - 1);
        cout << "Task deleted successfully." << endl;
    }
};

int main() {
    ToDoList myList;
    int choice;
    string task;
    int taskNumber;

    while (true) {
        cout << "\n===== To-Do List Application =====\n";
        cout << "1. Add a task\n";
        cout << "2. View tasks\n";
        cout << "3. Delete a task\n";
        cout << "4. Exit\n";
        cout << "Enter your choice: "; cin >> choice;

        switch (choice) {
            case 1:
                cout << "Enter the task: ";
                cin.ignore(); // To ignore the leftover newline character
                getline(cin, task);
                myList.addTask(task);
                break;

            case 2:
                myList.viewTasks();
                break;

            case 3:
                cout << "Enter the task number to delete: "; cin >> taskNumber;
                myList.deleteTask(taskNumber);
                break;

            case 4:
                cout << "Exiting program. Goodbye!" << endl;
                return 0;

            default:
                cout << "Invalid choice! Please try again." << endl;
        }
    }

    return 0;
}
            

Explanation of Program Structure

The program begins by defining a ToDoList class, which encapsulates the functionality of our To-Do list. The class has a private member tasks that is a vector of strings. This vector will hold the list of tasks.

There are three key functions in the ToDoList class:

  • addTask: Adds a task to the tasks vector.
  • viewTasks: Displays all tasks in the list.
  • deleteTask: Deletes a task from the list by its index.

The main function is the core of the application. It runs a loop that repeatedly presents the user with a menu and asks for their input. Depending on the user’s choice, it calls the appropriate function to add, view, or delete tasks.

Important notes:

  • The program uses a vector to store tasks, which is a dynamic array in C++ and can grow or shrink as tasks are added or removed.
  • The user can interact with the program through a text-based interface in the console.
  • Input validation ensures that the user cannot delete a task with an invalid number.

How to Run the Program

To run this program, follow these steps:

  1. Ensure you have a C++ compiler installed (e.g., GCC or MSVC).
  2. Create a new file with a .cpp extension (e.g., todo.cpp).
  3. Copy and paste the provided code into the file.
  4. Compile the code using a C++ compiler. For example:
    g++ -o todo todo.cpp
  5. Run the compiled program:
    ./todo
  6. The program will display a menu, and you can begin interacting with the To-Do list.
© 2024 Learn Programming. All rights reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)