Introduction
Task reminder applications are useful tools for keeping track of tasks and ensuring that important activities are not forgotten. By building a task reminder application in C++, users can easily manage and get notified of their upcoming tasks, which can help improve productivity and organization.
Objective
The goal of this tutorial is to guide you in creating a simple task reminder application in C++. This application will allow users to add tasks, set reminders, and display the task list. It will make use of basic concepts such as loops, conditionals, and file handling in C++.
Code for Task Reminder Application
#include #include #include #include #include using namespace std; // Structure to store task details struct Task { string name; string date; string time; }; // Function to display the menu void showMenu() { cout << "\nTask Reminder Application\n"; cout << "1. Add a new task\n"; cout << "2. View tasks\n"; cout << "3. Exit\n"; cout << "Enter your choice: "; } // Function to add a new task void addTask(vector& tasks) { Task newTask; cout << "\nEnter task name: "; cin.ignore(); getline(cin, newTask.name); cout << "Enter task date (DD-MM-YYYY): "; cin >> newTask.date; cout << "Enter task time (HH:MM): "; cin >> newTask.time; tasks.push_back(newTask); ofstream outFile("tasks.txt", ios::app); if (outFile.is_open()) { outFile << newTask.name << ";" << newTask.date << ";" << newTask.time << "\n"; outFile.close(); cout << "Task added successfully!\n"; } else { cout << "Error opening file to save the task.\n"; } } // Function to display all tasks void viewTasks(const vector& tasks) { cout << "\nTask List:\n"; for (const auto& task : tasks) { cout << "Task: " << task.name << "\n"; cout << "Date: " << task.date << " Time: " << task.time << "\n"; cout << "--------------------------\n"; } } // Function to load tasks from file void loadTasks(vector& tasks) { ifstream inFile("tasks.txt"); string line; while (getline(inFile, line)) { Task task; size_t pos = 0; pos = line.find(';'); task.name = line.substr(0, pos); line.erase(0, pos + 1); pos = line.find(';'); task.date = line.substr(0, pos); task.time = line.substr(pos + 1); tasks.push_back(task); } inFile.close(); } int main() { vector tasks; loadTasks(tasks); // Load existing tasks from file int choice; while (true) { showMenu(); cin >> choice; switch (choice) { case 1: addTask(tasks); break; case 2: viewTasks(tasks); break; case 3: cout << "Exiting the application.\n"; return 0; default: cout << "Invalid choice. Please try again.\n"; } } return 0; }
Program Explanation
This task reminder application is designed to manage and store tasks in a text file. Let’s break down the structure:
- Task Structure: The Task structure holds the task’s name, date, and time, which are essential to creating a reminder.
- Menu System: The program displays a simple text-based menu allowing the user to either add a task, view existing tasks, or exit the application.
- Add Task: The user can input the name, date, and time of the task, which is then saved both in memory and in a text file (“tasks.txt”).
- View Tasks: All tasks stored in memory are displayed with their respective details (name, date, and time).
- File Handling: Tasks are saved to and loaded from a text file for persistence. This ensures that even if the program is closed, tasks are not lost.
How to Run the Program
To run the program, follow these steps:
- Ensure you have a C++ compiler installed (such as GCC or Visual C++).
- Create a new C++ source file and paste the above code into the file (e.g.,
task_reminder.cpp
). - Compile the program using the following command (for GCC):
g++ task_reminder.cpp -o task_reminder
- Run the compiled program with the following command:
./task_reminder
- Follow the menu to add or view tasks.