To Do List In C++ - C++ Programming Tutorial
C++ Course / Dynamic Programming / To Do List In C++

To Do List In C++

BLUF: Mastering To Do List In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: To Do List In C++

C++ is renowned for its efficiency. Learn how To Do List In C++ enables low-level control and high-performance computing in the tutorial below.

Given the current rapid pace of daily life, maintaining organization is crucial for managing both professional and personal projects effectively. One of the most effective ways to enhance organization and ensure timely completion of tasks is by utilizing a To-Do List. While there are many existing applications available for this purpose, developing our own To-Do List application can serve as an engaging and rewarding project that enhances our programming abilities and deepens our understanding of C++.

C++ is a robust and high velocity computer programming language that is used to implement system software, real time applications such as game development etc. Being versatile and offering such wonderful features, it's perfectly suitable for the development of different applications, including a To-Do List. With this project we will be able to introduce some of the key functionalities of a To-Do list application as well as grasp data structures, Object-Oriented programming paradigm, and GUI framework.

  • Hence, the to-do list application will aim mainly at facilitating the management of tasks. Users should be able to enter new data under the tasks tab, check off completed data, and delete data that is no longer required. These above actions make up the basic actions in any To-Do List application, and these are the main actions we are going to begin with while designing our application. Later on, we can also expand to such other options as priority of the task, date by which the task should be completed, and even options of alert.
  • We will start by outlining the factors that have informed the application's design. It involves the decision of what sort of data structures to use for the tasks and what direction in the user interface to take. To keep our first version relatively uncomplicated, we'll implement a text-based environment and let the users control the application via commands. Besides its advantages of condensing the development procedures, the layered structuring concentrates on the logic of the application.
  • Now, we will proceed to the measures that will be taken to work on the project, with detailed description of the work plan. We make classes to manage Individual tasks and the to-do list; methods involving the creation of new tasks, deletion, and updating of task status combine these elements into one working application. Where specified, an explanation of the process followed will be given together with code samples to make the understanding easier.
  • Last of all, we will look at the testing, taking a middle note that our application functions in a desirable manner as well as handles the most extreme conditions. We will also indicate possible growth identification so that their user base increases and their To-Do List application becomes more multifunctional.
  • By the end of today's post, we will have the knowledge of how to write a To-Do List application in C++ along with the coding skills and ideas of software development. Regardless of whether we are a first-time programmer specializing in C++ or an experienced person wanting to find a new and exciting project, this guide will prove useful, and the end result will be well worth our effort.
  • Basic Functionality of a To-Do List Application in C++

Creating an application for crafting Task Lists involves developing several fundamental components that empower users to efficiently manage their tasks. The following outlines the functionalities our application will offer at its core and explains the implementation of each feature using C++ within the application software's conceptual framework.

1. Adding Tasks

This is why the primary function of a To-Do List application is the ability to add tasks. Users can effectively outline each task along with details like the due date or priority level. These details will be encapsulated in a separate class called Task, while the functionality for listing tasks will reside in another class named ToDoList. Users can input a task description as a string list or vector to create new entries in the list.

2. Viewing Tasks

Users should have the capability to view a comprehensive list of the tasks they have inputted. The effectiveness of the application hinges on its capacity to display all tasks, including descriptions and status (whether they are marked as completed or not). This functionality can be accomplished by passing the array of objects contained in our ToDoList class and subsequently utilizing a for-each loop to display the specifics of each task. Additionally, this feature can be leveraged to generate a separate list for tasks that are pending, distinct from those that have been completed.

3. Marking Tasks as Complete

One key benefit is the ability to mark a task as completed once it is finished, effectively changing its status. Users can simply input an index number to indicate a specific task is done, triggering an automatic status update. This feature streamlines progress tracking for users, offering a clear view of pending tasks.

4. Deleting Tasks

Occasionally, users may want to remove particular tasks when the application becomes too cluttered. Within our system, users can remove a task by specifying its position in the displayed list. Upon selection, the application will promptly eliminate the chosen task. This functionality helps avoid unnecessary build-up of clutter within the Pivotal Tracker task list.

5. Integrating the Functionalities:

These fundamental features are integrated into a single comprehensive To-Do List application to ensure its functionality. The main component consists of a user interface that presents a variety of functions, such as adding and managing tasks, viewing and modifying task lists, marking tasks as completed, and removing tasks from the list.

6. User Interaction:

The program will feature a command-line interface, allowing users to input commands for interacting with the application. This not only improves the development process but also maintains a clear focus on the core behavioral logic of the application.

7. Ensuring Robustness:

When integrating these features, we will prioritize the robustness of the applications and guarantee they handle various scenarios effectively. For instance, users should have the capability to designate a task as 'Completed' or 'Deleted' if it has not been previously rendered, shown, or included in a list. Appropriate error messages must be shown in such cases.

Implementation Steps for a To-Do List Application in C++

To develop a task management application, it is essential to outline its design, construction approach, functionality, and methods for ensuring its reliability. In the following sections, we will dissect the procedure into easily achievable steps:

Step 1: Explain the Task Class

Begin by establishing the Task class, which serves as a representation of a task within the To-Do List system. This class will include information such as:

  • Task Description: This will outline the specifics of the task that needs to be accomplished and completed.
  • Key Components:

  • Task description (string)
  • Completion status (boolean)
  • Step 2: Define the ToDoList Class

  • Now let describe ToDoList class, which will contain a list of tasks as Task objects. This class will consist of functions to create, read, update, and delete tasks.
  • Editing a Task Description: Some users need to change the description of an existing task to a different one.
  • Setting Task Priority: Enable users to set due dates and deadlines, which in turn create prioritization of the numerous tasks.
  • Sorting Tasks by Priority: For efficiency, display the tasks according to the priority.
  • Key Components:

  • A collection (e.g., vector) to store Task objects.
  • Instructions on how to create a new task, see all tasks, mark a task as complete, and delete the task.
  • Step 3: Include the Functionality of Adding a New Task

  • Implement the function for inserting a fresh task. This function will take in a task description as an argument, generate a new Task object, and append it to the task list.

Display the code to present all tasks to the user. This function will showcase each task along with its completion status within the task list as a group.

Step 5: Provide the Functionality for Complete Task

  • When working with PHP, create a script that marks a task as finished. This function takes an index as a parameter and validates the index before updating the task's completion status.

Specify the procedure for eliminating a specific task from the list by providing an index as a parameter. The function is designed to validate whether the provided argument is an integer within the permissible range before removing the designated task from the list.

Added the editTaskDescription function within the ToDoList class to simplify the task of modifying a task's description within the list.

Step 8: Setting Task Priority:

  • This element added a new attribute in the Task class This element implemented a method in the Task class.
  • Implemented a new method in the ToDoList class named setTaskPriority which takes a priority value and assigns it to a certain task.
  • Step 9: Sorting Tasks by Priority:

  • Include the sortTasksByPriority switch to the ToDoList class to sort the tasks according to the priority.
  • Used std::sort using a specific comparison predicate to arrange the tasks in a way related to priority.

A basic text-based interface needs to be created for users to engage with the application. This interface will present users with a menu of choices, allowing them to perform actions such as adding an item, displaying items, marking items as done, removing items, or exiting the program.

Code:

Here is the code for the aforementioned implementation along with the correct interface and elucidation.

Example

#include <iostream>
#include <vector>
#include <string>
#include <algorithm> // For std::sort
 
// Step 1: Define the Task class
class Task {
public:
    std::string description;
    bool isCompleted;
    int priority; // 1 = High, 2 = Medium, 3 = Low
 
    Task(const std::string& desc, int prio = 3) : description(desc), isCompleted(false), priority(prio) {}
};
 
// Step 2: Define the ToDoList class
class ToDoList {
private:
    std::vector<Task> tasks;
 
    // Helper function to sort tasks by priority
    static bool comparePriority(const Task& t1, const Task& t2) {
        return t1.priority < t2.priority;
    }
 
public:
    void addTask(const std::string& description, int priority = 3);
    void viewTasks() const;
    void completeTask(size_t index);
    void deleteTask(size_t index);
    void editTaskDescription(size_t index, const std::string& newDescription);
    void setTaskPriority(size_t index, int priority);
    void sortTasksByPriority();
};
 
// Step 3: Implement the Add Task functionality
void ToDoList::addTask(const std::string& description, int priority) {
    tasks.push_back(Task(description, priority));
}
 
// Step 4: Implement the View Tasks functionality
void ToDoList::viewTasks() const {
    for (size_t i = 0; i < tasks.size(); ++i) {
        std::cout << i + 1 << ". " << tasks[i].description
                  << (tasks[i].isCompleted ? " [Completed]" : " [Pending]")
                  << " [Priority: " << tasks[i].priority << "]" << std::endl;
    }
}
 
// Step 5: Implement the Complete Task functionality
void ToDoList::completeTask(size_t index) {
    if (index < 1 || index > tasks.size()) {
        std::cerr << "Invalid task number." << std::endl;
        return;
    }
    tasks[index - 1].isCompleted = true;
}
 
// Step 6: Implement the Delete Task functionality
void ToDoList::deleteTask(size_t index) {
    if (index < 1 || index > tasks.size()) {
        std::cerr << "Invalid task number." << std::endl;
        return;
    }
    tasks.erase(tasks.begin() + (index - 1));
}
 
// Step 7: Implement the Edit Task Description functionality
void ToDoList::editTaskDescription(size_t index, const std::string& newDescription) {
    if (index < 1 || index > tasks.size()) {
        std::cerr << "Invalid task number." << std::endl;
        return;
    }
    tasks[index - 1].description = newDescription;
}
 
// Step 8: Implement the Set Task Priority functionality
void ToDoList::setTaskPriority(size_t index, int priority) {
    if (index < 1 || index > tasks.size() || priority < 1 || priority > 3) {
        std::cerr << "Invalid input." << std::endl;
        return;
    }
    tasks[index - 1].priority = priority;
}
 
// Step 9: Implement the Sort Tasks by Priority functionality
void ToDoList::sortTasksByPriority() {
    std::sort(tasks.begin(), tasks.end(), comparePriority);
}
 
// Step 10: Create the User Interface
int main() {
    ToDoList todoList;
    int choice;
    std::string description;
    size_t index;
    int priority;
 
    do {
        std::cout << "1. Add Task\n2. View Tasks\n3. Complete Task\n4. Delete Task\n";
        std::cout << "5. Edit Task Description\n6. Set Task Priority\n7. Sort Tasks by Priority\n8. Exit\n";
        std::cin >> choice;
 
        switch (choice) {
            case 1:
                // Add a new task
                std::cout << "Enter task description: ";
                std::cin.ignore(); // Ignore the newline character left in the input buffer
                std::getline(std::cin, description);
                std::cout << "Enter priority (1 = High, 2 = Medium, 3 = Low): ";
                std::cin >> priority;
                todoList.addTask(description, priority);
                break;
            case 2:
                // View all tasks
                todoList.viewTasks();
                break;
            case 3:
                // Complete a task
                std::cout << "Enter task number to complete: ";
                std::cin >> index;
                todoList.completeTask(index);
                break;
            case 4:
                // Delete a task
                std::cout << "Enter task number to delete: ";
                std::cin >> index;
                todoList.deleteTask(index);
                break;
            case 5:
                // Edit task description
                std::cout << "Enter task number to edit: ";
                std::cin >> index;
                std::cout << "Enter new task description: ";
                std::cin.ignore();
                std::getline(std::cin, description);
                todoList.editTaskDescription(index, description);
                break;
            case 6:
                // Set task priority
                std::cout << "Enter task number to set priority: ";
                std::cin >> index;
                std::cout << "Enter priority (1 = High, 2 = Medium, 3 = Low): ";
                std::cin >> priority;
                todoList.setTaskPriority(index, priority);
                break;
            case 7:
                // Sort tasks by priority
                todoList.sortTasksByPriority();
                std::cout << "Tasks sorted by priority." << std::endl;
                break;
        }
    } while (choice != 8); // Exit the loop when the user chooses to exit
 
    return 0;
}

Output:

Output

1. Add Task
2. View Tasks
3. Complete Task
4. Delete Task
5. Edit Task Description
6. Set Task Priority
7. Sort Tasks by Priority
8. Exit
1
Enter task description: Buy groceries
Enter priority (1 = High, 2 = Medium, 3 = Low): 2
 
1. Add Task
2. View Tasks
3. Complete Task
4. Delete Task
5. Edit Task Description
6. Set Task Priority
7. Sort Tasks by Priority
8. Exit
1
Enter task description: Pay bills
Enter priority (1 = High, 2 = Medium, 3 = Low): 1
 
1. Add Task
2. View Tasks
3. Complete Task
4. Delete Task
5. Edit Task Description
6. Set Task Priority
7. Sort Tasks by Priority
8. Exit
1
Enter task description: Read a book
Enter priority (1 = High, 2 = Medium, 3 = Low): 3
 
1. Add Task
2. View Tasks
3. Complete Task
4. Delete Task
5. Edit Task Description
6. Set Task Priority
7. Sort Tasks by Priority
8. Exit
2
1. Buy groceries [Pending] [Priority: 2]
2. Pay bills [Pending] [Priority: 1]
3. Read a book [Pending] [Priority: 3]
 
1. Add Task
2. View Tasks
3. Complete Task
4. Delete Task
5. Edit Task Description
6. Set Task Priority
7. Sort Tasks by Priority
8. Exit
3
Enter task number to complete: 2
 
1. Add Task
2. View Tasks
3. Complete Task
4. Delete Task
5. Edit Task Description
6. Set Task Priority
7. Sort Tasks by Priority
8. Exit
2
1. Buy groceries [Pending] [Priority: 2]
2. Pay bills [Completed] [Priority: 1]
3. Read a book [Pending] [Priority: 3]
 
1. Add Task
2. View Tasks
3. Complete Task
4. Delete Task
5. Edit Task Description
6. Set Task Priority
7. Sort Tasks by Priority
8. Exit
5
Enter task number to edit: 1
Enter new task description: Buy vegetables
 
1. Add Task
2. View Tasks
3. Complete Task
4. Delete Task
5. Edit Task Description
6. Set Task Priority
7. Sort Tasks by Priority
8. Exit
2
1. Buy vegetables [Pending] [Priority: 2]
2. Pay bills [Completed] [Priority: 1]
3. Read a book [Pending] [Priority: 3]
 
1. Add Task
2. View Tasks
3. Complete Task
4. Delete Task
5. Edit Task Description
6. Set Task Priority
7. Sort Tasks by Priority
8. Exit
6
Enter task number to set priority: 3
Enter priority (1 = High, 2 = Medium, 3 = Low): 1
 
1. Add Task
2. View Tasks
3. Complete Task
4. Delete Task
5. Edit Task Description
6. Set Task Priority
7. Sort Tasks by Priority
8. Exit
2
1. Buy vegetables [Pending] [Priority: 2]
2. Pay bills [Completed] [Priority: 1]
3. Read a book [Pending] [Priority: 1]
 
1. Add Task
2. View Tasks
3. Complete Task
4. Delete Task
5. Edit Task Description
6. Set Task Priority
7. Sort Tasks by Priority
8. Exit
7
Tasks sorted by priority.
 
1. Add Task
2. View Tasks
3. Complete Task
4. Delete Task
5. Edit Task Description
6. Set Task Priority
7. Sort Tasks by Priority
8. Exit
2
1. Pay bills [Completed] [Priority: 1]
2. Read a book [Pending] [Priority: 1]
3. Buy vegetables [Pending] [Priority: 2]
 
1. Add Task
2. View Tasks
3. Complete Task
4. Delete Task
5. Edit Task Description
6. Set Task Priority
7. Sort Tasks by Priority
8. Exit
4
Enter task number to delete: 1
 
1. Add Task
2. View Tasks
3. Complete Task
4. Delete Task
5. Edit Task Description
6. Set Task Priority
7. Sort Tasks by Priority
8. Exit
2
1. Read a book [Pending] [Priority: 1]
2. Buy vegetables [Pending] [Priority: 2]
 
1. Add Task
2. View Tasks
3. Complete Task
4. Delete Task
5. Edit Task Description
6. Set Task Priority
7. Sort Tasks by Priority
8. Exit
8
=== Code Execution Successful ===

Pros:

  • User-Friendly Interface: The UI, particularly that consisting the command line interface (CLI), is comprehensible and, thus, makes it possible for potential users to freely and easily use the application. The MU provides the possibility for users to immediately select their actions without knowing specific commands.
  • Essential Task Management Features: This application is equipped with all necessary and sufficient features needed for general usage like creating, viewing, completing, deleting, and editing tasks. These are requirements that are just so important when it comes to possessing the ability to manage a list of tasks effectively.
  • Task Prioritization: The possible requirements for organizing and prioritizing tasks in order to allow users to prioritize important tasks as recommended for improving performance. This feature will be highly beneficial in working with several assignments, some of which are more critical than others.
  • Customization: Users can edit task descriptions, which means that if the details of a task change, users will be able to make changes to them. This helps make the to-do list dynamic and not become a mere chore done away in the corner.
  • Educational Value: This application also helps beginners learning C++ to have some solid projects they could work on. It also starts with concepts like class, vectors, and basic I/O operations, which form the backbone of Object-Oriented Programming.
  • Lack of Persistence: The current implementation of the to-do list does not have the capability of storing the tasks done for the day when it gets called the next time. It is important to note that whenever the application is closed, all the work accomplished disappears. Adding file I/O capabilities for writing and loading tasks would be an enhancement. It could be added that implementing the saving and loading for tasks would be beneficial.
  • Limited User Interface: Despite being operational, it is still a CLI interface and devoid of the friendliness and interactivity observed in GUIs. Some of the elements present in modern applications are less innovative, which may be viewed as a minus by users who prefer rich graphical interfaces.
  • Scalability Issues: Due to limited concurrent threads, increasing the number of tasks can affect the application's performance. The program could be better optimized to handle larger amounts of stored data, and the inclusion of scalable structures would increase efficiency.
  • Error Handling: Scrolling on keyboards is not smooth, and there is smaller number of error messages, which are not very elaborate. For instance, if a user enters an invalid task number, perhaps a number that has been input wrongly, then the response is quite general. There is much room for improvement regarding the informative character of presented errors and interpretations of inputs.
  • No Task Categorization: To repeat some points mentioned above, the authors failed to provide the application with options for filing tasks into projects/themes. To enhance the tool, it was suggested that the available categories or tags be expanded; these additions would prove useful in organizing tasks if the user works on multiple projects.
  • No Deadline Management: There is no functionality for deadlines and reminders to complete tasks according to the planned time. Incorporating the deadline management part will further enhance the application's utility for managing time-sensitive tasks.
  • Advanced Features to Enhance the Application

Although the current To-Do List application provides the essential means to track tasks, it still lacks some features that might enhance the application's performance and usage. Here are a few enhancements to consider:

  • Data Persistence: Evaluating the list of features for this application, data persistence is probably the most important one to be added. This would implicate storing these tasks in a file so that the data is not lost when the application has been closed. With the help of the read and write functions, the user can continue completing the tasks list because the progress has been saved in the file.
  • Graphical User Interface (GUI): Another improvement could be transitioning from a command-line interface to a graphical one. A GUI is more graphical, and users can find their way and interact with it better, hence attracting more people. For the creation of GUIs on the cross-platform, libraries available in the market, such as Qt or wxWidgets, can be used.
  • Task Categorization and Tags: Extension or adding categories or tags can enhance the ease with which the various tasks can be sorted. It was possible to club the tasks according to the projects that needed to be done, the areas of specialization, or the themes that were in a certain theme and area of interest.
  • Deadline and Reminder Management: It is suggested that the integration of dead and reminders would, however, go a long way in making the application more useful. People could assign specific timestamps to the tasks, and messages about the subsequent CC due times would appear. This feature is very advantageous, especially when it comes to activities that may require timely needle-like work to be done, and it makes the user very conscious of his duties.
  • Recurring Tasks: Some are repetitive, which means that they are done cyclically; for example, every week may involve a weekly meeting, while daily may involve daily chores. Introducing the ability to set repeating tasks can be a useful addition because they will be qualified as tasks that repeat at some time intervals, enabling saving time and avoiding the creation of the same recurring task repeatedly.
  • Collaboration Features: Another feature that can be implemented to make it more superior and highly utilized is the collaboration feature. This may involve assignment, sharing and managing workloads, and overall monitoring of a team's activities. I imagine that such features would make the application suitable for both work and private life, with the ability to collaborate on tasks within a team.
  • Conclusion

In summary, the C++ To-Do List program serves as a basic tool for managing tasks efficiently by enabling users to add, view, complete, delete, and edit tasks with precision. This project, although straightforward, offers valuable learning opportunities, making it ideal for C++ beginners. However, to enhance its utility and transform it into a comprehensive productivity solution, several enhancements are necessary. Implementing data persistence, for instance, would prevent task loss between sessions and significantly enhance usability. Furthermore, incorporating a graphical user interface could not only improve the app's aesthetics but also enhance its usability. Additional features like subtasks, due dates, recurrences, and collaboration capabilities could cater to a wider range of task management needs. By addressing these limitations and integrating these functionalities, the application can become more practical, efficient, and user-friendly. By continually refining and adapting the app to user requirements, developers can meet users' task management needs effectively, promoting organization and efficiency in their daily tasks.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience