Skip to content

Commit cea0574

Browse files
fix: input validation and empty task title check in To-Do List
1 parent 51a8f34 commit cea0574

File tree

2 files changed

+43
-37
lines changed

2 files changed

+43
-37
lines changed

Src/To-Do-List/todo.cpp

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,105 @@
11
#include <iostream>
22
#include <vector>
3+
4+
35
#include <string>
6+
#include <limits>
47
using namespace std;
58

6-
// Class representing a single Task
9+
710
class Task {
811
private:
912
string title;
10-
bool isCompleted;
13+
bool isCompleted;
1114

1215
public:
13-
// Constructor
14-
Task(string t) {
15-
title = t;
16-
isCompleted = false;
17-
}
1816

19-
// Mark task as completed
17+
Task(string t) : title(t), isCompleted(false) {}
18+
2019
void markCompleted() {
2120
isCompleted = true;
2221
}
2322

24-
// Get task title
2523
string getTitle() const {
2624
return title;
2725
}
2826

29-
// Check completion status
30-
bool getStatus() const {
27+
bool getStatus() const {
3128
return isCompleted;
3229
}
3330
};
3431

35-
// Class representing the To-Do List
36-
class ToDoList {
32+
class ToDoList {
3733
private:
3834
vector<Task> tasks;
3935

4036
public:
41-
// Add a new task
37+
4238
void addTask(const string& title) {
39+
if (title.empty()) {
40+
cout << "❌ Task title cannot be empty!\n";
41+
return;
42+
}
4343
tasks.push_back(Task(title));
44-
cout << "Task added successfully!\n";
44+
cout << "Task added successfully!\n";
4545
}
4646

47-
// Display all tasks
4847
void showTasks() const {
4948
if (tasks.empty()) {
50-
cout << "No tasks available!\n";
49+
cout << "📭 No tasks available!\n";
5150
return;
5251
}
5352

5453
cout << "\n------ To-Do List ------\n";
5554
for (size_t i = 0; i < tasks.size(); ++i) {
5655
cout << i + 1 << ". " << tasks[i].getTitle()
57-
<< " [" << (tasks[i].getStatus() ? "Done" : "Pending") << "]\n";
56+
<< " [" << (tasks[i].getStatus() ? "Done" : "🕓 Pending") << "]\n";
5857
}
5958
cout << "------------------------\n";
6059
}
6160

62-
// Mark a task as completed
6361
void markTaskCompleted(int index) {
6462
if (index < 1 || index > (int)tasks.size()) {
65-
cout << "Invalid task number!\n";
63+
cout << "⚠️ Invalid task number!\n";
6664
return;
6765
}
6866
tasks[index - 1].markCompleted();
69-
cout << "Task marked as completed!\n";
67+
cout << "Task marked as completed!\n";
7068
}
7169

72-
// Delete a task
7370
void deleteTask(int index) {
7471
if (index < 1 || index > (int)tasks.size()) {
75-
cout << "Invalid task number!\n";
72+
cout << "⚠️ Invalid task number!\n";
7673
return;
7774
}
7875
tasks.erase(tasks.begin() + index - 1);
79-
cout << "Task deleted successfully!\n";
76+
cout << "🗑️ Task deleted successfully!\n";
8077
}
8178
};
8279

83-
// Main program
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+
8496
int main() {
8597
ToDoList todo;
8698
int choice;
8799
string title;
88100
int index;
89101

90-
cout << "\n===== TO-DO LIST APPLICATION =====\n";
102+
cout << "\n===== 📝 TO-DO LIST APPLICATION =====\n";
91103
cout << "Manage your daily tasks efficiently.\n";
92104

93105
do {
@@ -97,9 +109,7 @@ int main() {
97109
cout << "3. Mark Task as Completed\n";
98110
cout << "4. Delete Task\n";
99111
cout << "5. Exit\n";
100-
cout << "Enter your choice: ";
101-
cin >> choice;
102-
cin.ignore(); // To handle newline
112+
choice = getValidInt("Enter your choice: ");
103113

104114
switch (choice) {
105115
case 1:
@@ -113,27 +123,23 @@ int main() {
113123
break;
114124

115125
case 3:
116-
cout << "Enter task number to mark completed: ";
117-
cin >> index;
126+
index = getValidInt("Enter task number to mark completed: ");
118127
todo.markTaskCompleted(index);
119128
break;
120129

121130
case 4:
122-
cout << "Enter task number to delete: ";
123-
cin >> index;
131+
index = getValidInt("Enter task number to delete: ");
124132
todo.deleteTask(index);
125133
break;
126134

127135
case 5:
128-
cout << "Exiting program. Goodbye!\n";
136+
cout << "👋 Exiting program. Goodbye!\n";
129137
break;
130138

131139
default:
132-
cout << "Invalid choice. Try again!\n";
140+
cout << "⚠️ Invalid choice. Try again!\n";
133141
}
134142
} while (choice != 5);
135143

136144
return 0;
137145
}
138-
139-

Src/To-Do-List/todo.exe

2.03 KB
Binary file not shown.

0 commit comments

Comments
 (0)