-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
180 lines (166 loc) · 4.46 KB
/
main.cpp
File metadata and controls
180 lines (166 loc) · 4.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
#include <iostream>
#include <vector>
#include <string>
#include <iomanip>
using namespace std;
class Task
{
public:
string description;
bool completed;
string title;
Task(const string &tit, const string &desc) : description(desc), completed(false), title(tit) {}
};
class ToDoList
{
private:
vector<Task> tasks;
string date = __DATE__;
int search(const string &title)
{
int index = -1;
// searchig for index
for (int i = 0; i < tasks.size(); i++)
{
if (tasks[i].title == title)
{
index = i;
break;
}
}
return index;
}
public:
// To add task
void addTask(const string &title, const string &desc)
{
int index = search(title);
if (index == -1)
{
Task newTask(title, desc);
tasks.push_back(newTask);
cout << "Task added!" << endl;
}
else
{
cout << "Title alreay exist try with another task tile" << endl;
}
}
void markTaskAsCompleted(const string &comp_title)
{
// marking as read if index found
int index = search(comp_title);
if (index >= 0 && index < tasks.size())
{
tasks[index].completed = true;
cout << "Task marked as completed!\n";
}
else
{
cout << "Invalid task title.\n";
}
}
void viewTasks()
{
if (tasks.empty())
{
cout << "No tasks to display.\n";
return;
}
cout << "Tasks:\n";
cout << "Date: " << date << endl;
// table
int colWidth = 40;
cout << setfill('*') << setw(3 * colWidth) << "*" << endl;
cout << setfill(' ') << fixed;
cout << setw(3) << "Index" << setw(colWidth) << "Title" << setw(colWidth) << "Description" << endl;
cout << setfill('*') << setw(3 * colWidth) << "*" << endl;
cout << setfill(' ') << fixed;
for (size_t i = 0; i < tasks.size(); ++i)
{
cout << setprecision(0) <<i + 1 << ". ";
if (tasks[i].completed)
{
cout << "[Completed] ";
}
cout << setprecision(4) << setw(colWidth)
<< tasks[i].title << setw(colWidth) << tasks[i].description << endl;
}
}
void removeTask(const string del_title)
{
int index = search(del_title);
if (index >= 0 && index < tasks.size())
{
tasks.erase(tasks.begin() + index);
cout << "Task removed!\n";
}
else
{
cout << "Invalid task title.\n";
}
}
};
int main()
{
ToDoList todo;
while (true)
{
cout << "To-Do List Application" << endl;
;
cout << "1. Add Task\n";
cout << "2. Mark Task as Completed\n";
cout << "3. View Tasks\n";
cout << "4. Remove Task\n";
// cout<<"5. "
cout << "5. Exit\n";
int choice;
cin >> choice;
switch (choice)
{
case 1:
{
cin.ignore(); // Clear newline from buffer
string description;
string title;
cout << "Enter task title: ";
std::getline(cin, title);
cout << "Enter task description: ";
std::getline(cin, description);
todo.addTask(title, description);
break;
}
case 2:
{
// cout << "Enter task index: ";
// int index;
// cin >> index;
// todo.markTaskAsCompleted(index);
cout << "Enter task title which you have completed: ";
string comp_title;
cin >> comp_title;
todo.markTaskAsCompleted(comp_title);
break;
}
case 3:
{
todo.viewTasks();
break;
}
case 4:
{
cout << "Enter task title which you want to delete: ";
string del_title;
cin >> del_title;
todo.removeTask(del_title);
break;
}
case 5:
cout << "Exiting.\n";
return 0;
default:
cout << "Invalid choice.\n";
}
}
return 0;
}