|
| 1 | +#include <iostream> |
| 2 | +#include <vector> |
| 3 | + |
| 4 | + |
| 5 | +#include <string> |
| 6 | +#include <limits> |
| 7 | +using namespace std; |
| 8 | + |
| 9 | + |
| 10 | +class Task { |
| 11 | +private: |
| 12 | + string title; |
| 13 | + bool isCompleted; |
| 14 | + |
| 15 | +public: |
| 16 | + |
| 17 | + Task(string t) : title(t), isCompleted(false) {} |
| 18 | + |
| 19 | + void markCompleted() { |
| 20 | + isCompleted = true; |
| 21 | + } |
| 22 | + |
| 23 | + string getTitle() const { |
| 24 | + return title; |
| 25 | + } |
| 26 | + |
| 27 | + bool getStatus() const { |
| 28 | + return isCompleted; |
| 29 | + } |
| 30 | +}; |
| 31 | + |
| 32 | + class ToDoList { |
| 33 | +private: |
| 34 | + vector<Task> tasks; |
| 35 | + |
| 36 | +public: |
| 37 | + |
| 38 | + void addTask(const string& title) { |
| 39 | + if (title.empty()) { |
| 40 | + cout << "❌ Task title cannot be empty!\n"; |
| 41 | + return; |
| 42 | + } |
| 43 | + tasks.push_back(Task(title)); |
| 44 | + cout << " ✅ Task added successfully!\n"; |
| 45 | + } |
| 46 | + |
| 47 | + void showTasks() const { |
| 48 | + if (tasks.empty()) { |
| 49 | + cout << "📭 No tasks available!\n"; |
| 50 | + return; |
| 51 | + } |
| 52 | + |
| 53 | + cout << "\n------ To-Do List ------\n"; |
| 54 | + for (size_t i = 0; i < tasks.size(); ++i) { |
| 55 | + cout << i + 1 << ". " << tasks[i].getTitle() |
| 56 | + << " [" << (tasks[i].getStatus() ? "✅ Done" : "🕓 Pending") << "]\n"; |
| 57 | + } |
| 58 | + cout << "------------------------\n"; |
| 59 | + } |
| 60 | + |
| 61 | + void markTaskCompleted(int index) { |
| 62 | + if (index < 1 || index > (int)tasks.size()) { |
| 63 | + cout << "⚠️ Invalid task number!\n"; |
| 64 | + return; |
| 65 | + } |
| 66 | + tasks[index - 1].markCompleted(); |
| 67 | + cout << "✅ Task marked as completed!\n"; |
| 68 | + } |
| 69 | + |
| 70 | + void deleteTask(int index) { |
| 71 | + if (index < 1 || index > (int)tasks.size()) { |
| 72 | + cout << "⚠️ Invalid task number!\n"; |
| 73 | + return; |
| 74 | + } |
| 75 | + tasks.erase(tasks.begin() + index - 1); |
| 76 | + cout << "🗑️ Task deleted successfully!\n"; |
| 77 | + } |
| 78 | +}; |
| 79 | + |
| 80 | +int getValidInt(const string& prompt) { |
| 81 | + int value; |
| 82 | + while (true) { |
| 83 | + cout << prompt; |
| 84 | + cin >> value; |
| 85 | + if (cin.fail()) { |
| 86 | + cout << "❌ Invalid input! Please enter a number.\n"; |
| 87 | + cin.clear(); |
| 88 | + cin.ignore(numeric_limits<streamsize>::max(), '\n'); |
| 89 | + } else { |
| 90 | + cin.ignore(numeric_limits<streamsize>::max(), '\n'); |
| 91 | + return value; |
| 92 | + } |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +int main() { |
| 97 | + ToDoList todo; |
| 98 | + int choice; |
| 99 | + string title; |
| 100 | + int index; |
| 101 | + |
| 102 | + cout << "\n===== 📝 TO-DO LIST APPLICATION =====\n"; |
| 103 | + cout << "Manage your daily tasks efficiently.\n"; |
| 104 | + |
| 105 | + do { |
| 106 | + cout << "\nMenu:\n"; |
| 107 | + cout << "1. Add Task\n"; |
| 108 | + cout << "2. View Tasks\n"; |
| 109 | + cout << "3. Mark Task as Completed\n"; |
| 110 | + cout << "4. Delete Task\n"; |
| 111 | + cout << "5. Exit\n"; |
| 112 | + choice = getValidInt("Enter your choice: "); |
| 113 | + |
| 114 | + switch (choice) { |
| 115 | + case 1: |
| 116 | + cout << "Enter task title: "; |
| 117 | + getline(cin, title); |
| 118 | + todo.addTask(title); |
| 119 | + break; |
| 120 | + |
| 121 | + case 2: |
| 122 | + todo.showTasks(); |
| 123 | + break; |
| 124 | + |
| 125 | + case 3: |
| 126 | + index = getValidInt("Enter task number to mark completed: "); |
| 127 | + todo.markTaskCompleted(index); |
| 128 | + break; |
| 129 | + |
| 130 | + case 4: |
| 131 | + index = getValidInt("Enter task number to delete: "); |
| 132 | + todo.deleteTask(index); |
| 133 | + break; |
| 134 | + |
| 135 | + case 5: |
| 136 | + cout << "👋 Exiting program. Goodbye!\n"; |
| 137 | + break; |
| 138 | + |
| 139 | + default: |
| 140 | + cout << "⚠️ Invalid choice. Try again!\n"; |
| 141 | + } |
| 142 | + } while (choice != 5); |
| 143 | + |
| 144 | + return 0; |
| 145 | +} |
0 commit comments