-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_task.h
More file actions
81 lines (78 loc) · 2.02 KB
/
Copy pathremove_task.h
File metadata and controls
81 lines (78 loc) · 2.02 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
#include<iostream>
#include <fstream>
#include <string>
using namespace std;
class Removetask {
public:
void removeTask();
private:
bool deleteTaskByID(int taskID);
bool deleteTaskByDate(const string& date);
};
void Removetask::removeTask() {
cout << "Remove Task by:\n1. Task ID\n2. Date\nChoose an option (1 or 2): ";
int choice;
cin >> choice;
if (choice == 1) {
int taskID;
cout << "Enter Task ID to remove: ";
cin >> taskID;
if (deleteTaskByID(taskID)) {
cout << "Task with ID " << taskID << " removed successfully.\n";
} else {
cout << "Task with ID " << taskID << " not found.\n";
}
} else if (choice == 2) {
string date;
cout << "Enter Date (YYYY-MM-DD) to remove tasks: ";
cin >> date;
if (deleteTaskByDate(date)) {
cout << "Tasks on date " << date << " removed successfully.\n";
} else {
cout << "No tasks found on date " << date << ".\n";
}
} else {
cout << "Invalid choice. Please select 1 or 2.\n";
}
}
bool Removetask::deleteTaskByID(int taskID){
ifstream fin;
fin.open("Task.txt");
ofstream temp;
temp.open("temp.txt");
string line;
bool found = false;
while(getline(fin, line)){
if(line.find("Task ID:"+to_string(taskID))==string::npos){
temp<<line<<endl;
} else {
found = true;
}
}
temp.close();
fin.close();
remove("Task.txt");
rename("temp.txt","Task.txt");
return found;
}
bool Removetask::deleteTaskByDate(const string& date){
ifstream fin;
fin.open("Task.txt");
ofstream temp;
temp.open("temp.txt");
string line;
bool found = false;
while(getline(fin, line)){
if(line.find("task expiring date: "+date)==string::npos){
temp<<line<<endl;
} else {
found = true;
}
}
cin.ignore();
temp.close();
fin.close();
remove("Task.txt");
rename("temp.txt","Task.txt");
return found;
}