-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsimple_to_do_list.cpp
235 lines (221 loc) · 7.46 KB
/
simple_to_do_list.cpp
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#include <iostream>
#include <fstream>
#include<string>
#include <cstdlib>
using namespace std;
int ID;
//custom type todo which has two fields id and task
struct todo {
int id;
string task;
};
//this method is used to add a new task to the list of tasks
void addtodo() {
system("cls");
cout<<"\t\t\t***********************************************************************"<<endl;
cout<<"\t\t\t WELCOME TO Your ToDo List "<<endl;
cout<<"\t\t\t***********************************************************************"<<endl<<endl<<endl;
todo todo;
cout << "\n\tEnter new task: ";
cin.get();
getline(cin, todo.task); //get user input
ID++; //increment id for the current task
//now write this task to the todo.txt file
ofstream write;
write.open("todo.txt", ios::app);
write << "\n" << ID;
write << "\n" << todo.task ;
write.close();
//write the id to a new file so that we can use this id later to add new task
write.open("id.txt");
write << ID;
write.close();
char ch;
cout<<"Do you want to add more task? y/n"<<endl;
cin>> ch;
//if user wants to add a new task again then call the same function else return
if(ch == 'y'){
addtodo();
}
else{
cout << "\n\tTask has been added successfully";
return;
}
}
// this method is used to print the task on the screen
void print(todo s) {
cout << "\n\tID is : " << s.id;
cout << "\n\tTask is : " << s.task;
}
//This method is used to read data from the todo.txt file and print it on screen
void readData() {
system("cls");
cout<<"\t\t\t***********************************************************************"<<endl;
cout<<"\t\t\t WELCOME TO Your ToDo List "<<endl;
cout<<"\t\t\t***********************************************************************"<<endl<<endl<<endl;
todo todo;
ifstream read;
read.open("todo.txt");
cout << "\n\t------------------Your current Tasks in the list--------------------";
// while we dont reach the end of file keep on printing the data on screen
while (!read.eof()) {
read >> todo.id;
read.ignore();
getline(read, todo.task);
print(todo);
}
read.close();
}
//this method is used to search for a specific task from the todo.txt file
int searchData() {
system("cls");
cout<<"\t\t\t***********************************************************************"<<endl;
cout<<"\t\t\t WELCOME TO Your ToDo List "<<endl;
cout<<"\t\t\t***********************************************************************"<<endl<<endl<<endl;
int id;
cout << "\n\tEnter task id: ";
cin >> id;
todo todo;
ifstream read;
read.open("todo.txt");
//while we dont reach end of file keep or searching for the id to match to the user input id
while (!read.eof()) {
read >> todo.id;
read.ignore();
getline(read, todo.task);
if (todo.id == id) {
print(todo);
return id;
}
}
}
// this method is used to delete the task from the todo.txt file
void deleteData() {
system("cls");
cout<<"\t\t\t***********************************************************************"<<endl;
cout<<"\t\t\t WELCOME TO Your ToDo List "<<endl;
cout<<"\t\t\t***********************************************************************"<<endl<<endl<<endl;
int id = searchData();
cout << "\n\tDo you want to delete this task (y/n) : ";
char choice;
cin >> choice;
if (choice == 'y') {
todo todo;
ofstream tempFile;
tempFile.open("temp.txt");
ifstream read;
read.open("todo.txt");
//while we dont reach the end of file keep on searching for the id to delete the task
while (!read.eof()) {
read >> todo.id;
read.ignore();
getline(read, todo.task);
if (todo.id != id) {
tempFile << "\n" << todo.id;
tempFile << "\n" << todo.task;
}
}
read.close();
tempFile.close();
remove("todo.txt");
rename("temp.txt", "todo.txt");
cout << "\n\tTask deleted successfuly";
}
else {
cout << "\n\tRecord not deleted";
}
}
//this method is used to update the task
//here we create a new temp.txt file and add all the updated data to this file
//once updated we then delete the original todo.txt and then rename this file to todo.txt
void updateData() {
system("cls");
cout<<"\t\t\t***********************************************************************"<<endl;
cout<<"\t\t\t WELCOME TO Your ToDo List "<<endl;
cout<<"\t\t\t***********************************************************************"<<endl<<endl<<endl;
int id = searchData();
cout << "\n\tYou want to update this task (y/n) : ";
char choice;
cin >> choice;
if (choice == 'y') {
todo newData;
cout << "\n\tEnter todo task : ";
cin.get();
getline(cin, newData.task);
todo todo;
ofstream tempFile;
tempFile.open("temp.txt");
ifstream read;
read.open("todo.txt");
//while we dont reach end of file keep on searching for the id and once found update with new data
while (!read.eof()) {
read >> todo.id;
read.ignore();
getline(read, todo.task);
if (todo.id != id) {
tempFile << "\n" << todo.id;
tempFile << "\n" << todo.task;
}
else {
tempFile << "\n"<< todo.id;
tempFile << "\n"<< newData.task;
}
}
read.close();
tempFile.close();
remove("todo.txt");
rename("temp.txt", "todo.txt");
cout << "\n\tTask updated successfuly";
}
else {
cout << "\n\tRecord not deleted";
}
}
int main()
{
system("cls");
system("Color E0"); // "Color XY", X - backgroung color, Y - text color
system("title todoapp @copyassignment");
cout<<"\t\t\t***********************************************************************"<<endl;
cout<<"\t\t\t* *"<<endl;
cout<<"\t\t\t* WELCOME TO Your ToDo List *"<<endl;
cout<<"\t\t\t* *"<<endl;
cout<<"\t\t\t***********************************************************************"<<endl<<endl<<endl<<endl;
ifstream read;
read.open("id.txt");
if (!read.fail()) {
read >> ID;
}
else {
ID = 0;
}
read.close();
while (true) {
cout<<endl<<endl;
cout << "\n\t1.Add student data";
cout << "\n\t2.See student data";
cout << "\n\t3.Search student data";
cout << "\n\t4.Delete student data";
cout << "\n\t5.Update student data";
int choice;
cout << "\n\tEnter choice : ";
cin >> choice;
switch (choice) {
case 1:
addtodo();
break;
case 2:
readData();
break;
case 3:
searchData();
break;
case 4:
deleteData();
break;
case 5:
updateData();
break;
}
}
}