-
Notifications
You must be signed in to change notification settings - Fork 0
/
TASK42.cpp
115 lines (102 loc) · 3.07 KB
/
TASK42.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
#include <iostream>
#include <string>
using namespace std;
class ToDoList {
private:
int MAX_TASKS;
string tasks[50];
bool taskStatus[50];
int nextIndex;
public:
ToDoList(int maxTasks) : MAX_TASKS(maxTasks), nextIndex(0) {}
void AddTask(const string& taskName) {
if(nextIndex < MAX_TASKS){
tasks[nextIndex] = taskName;
taskStatus[nextIndex] = false;
cout << "Task added." << endl;
nextIndex++;
}else{
cout << "Task list is full." << endl;
}
}
void ViewTasks() {
if(nextIndex == 0) {
cout<<"No tasks to display." << endl;
return;
}
cout<<"\nTasks:" << endl;
for(int i = 0; i < nextIndex; ++i) {
cout<<i + 1 << ". " << tasks[i] << " - " << (taskStatus[i] ? "Completed" : "Pending") << endl;
}
}
void MarkTaskAsCompleted(int index) {
if(index>= 1 && index <= nextIndex) {
taskStatus[index - 1] = true;
cout<<"Task marked as completed."<<endl;
} else{
cout<<"Invalid task index."<<endl;
}
}
void RemoveTask(int index){
if (index >= 1 && index<=nextIndex) {
for (int i = index - 1; i < nextIndex - 1; ++i) {
tasks[i] = tasks[i + 1];
taskStatus[i] = taskStatus[i + 1];
}
nextIndex--;
cout<<"Task removed."<<endl;
} else{
cout<< "Invalid task index."<<endl;
}
}
};
int main() {
int maxTasks;
cout<<"Enter the maximum number of tasks: ";
cin>>maxTasks;
ToDoList todoList(maxTasks);
while (true) {
cout<<"\nMenu:" << endl;
cout<<"1. Add Task" << endl;
cout<<"2. View Tasks" << endl;
cout<<"3. Mark Task as Completed" << endl;
cout<<"4. Remove Task" << endl;
cout<<"5. Exit" << endl;
int choice;
cout<<"Enter your choice: ";
cin>>choice;
switch (choice) {
case 1: {
string taskName;
cout<<"Enter task: ";
cin.ignore();
getline(cin, taskName);
todoList.AddTask(taskName);
break;
}
case 2:
todoList.ViewTasks();
break;
case 3: {
int index;
cout<<"Enter task index to mark as completed: ";
cin>>index;
todoList.MarkTaskAsCompleted(index);
break;
}
case 4: {
int index;
cout<<"Enter task index to remove: ";
cin>>index;
todoList.RemoveTask(index);
break;
}
case 5:
cout<<"Exiting program." << endl;
return 0;
default:
cout<<"Invalid choice. Please try again." << endl;
}
}
return 0;
}