-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.cpp
More file actions
94 lines (79 loc) · 2.43 KB
/
Copy pathtasks.cpp
File metadata and controls
94 lines (79 loc) · 2.43 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
#include <iostream>
#include <fstream>
#include "tasks.h"
using namespace std;
void printMenu() {
// simple menu
cout << "\n--- TODO LIST MANAGER ---" << endl;
cout << "1. Add Task" << endl;
cout << "2. View Tasks" << endl;
cout << "3. Mark Task as Done" << endl;
cout << "4. Save & Exit" << endl;
cout << "Enter choice: ";
}
void addTask(vector<Task>& tasks) {
//using vector of tasks objject(for tasks.txt)
Task newTask;
cout << "Enter task description: ";
cin.ignore();
// get the full line text
getline(cin, newTask.description);
newTask.isCompleted = false;
tasks.push_back(newTask);
cout << "Task added!" << endl;
}
void viewTasks(const vector<Task>& tasks) {
cout << "\nYour Tasks:" << endl;
cout << "--------------------------------" << endl;
if (tasks.empty()) {
cout << "No tasks found." << endl;
return;
}
// loop through all tasks to print them
for (size_t i = 0; i < tasks.size(); ++i) {
string status = tasks[i].isCompleted ? "[X]" : "[ ]";
cout << i + 1 << ". " << status << " " << tasks[i].description << endl;
}
cout << "--------------------------------" << endl;
}
void completeTask(vector<Task>& tasks) {
viewTasks(tasks);
if (tasks.empty()) return;
cout << "Enter number to mark done: ";
int index;
cin >> index;
// check if the number is valid
if (index > 0 && index <= (int)tasks.size()) {
tasks[index - 1].isCompleted = true;
cout << "Done!" << endl;
} else {
cout << "Invalid number." << endl;
}
}
void saveTasks(const vector<Task>& tasks, const string& filename) {
ofstream outFile(filename);
if (outFile.is_open()) {
// write bool then desc
for (const auto& task : tasks) {
outFile << task.isCompleted << endl;
outFile << task.description << endl;
}
outFile.close();
cout << "Saved." << endl;
} else {
cout << "Error saving file." << endl;
}
}
void loadTasks(vector<Task>& tasks, const string& filename) {
ifstream inFile(filename);
if (inFile.is_open()) {
Task tempTask;
// keep reading until end of file
while (inFile >> tempTask.isCompleted) {
inFile.ignore(); // skip newline
getline(inFile, tempTask.description);
tasks.push_back(tempTask);
}
inFile.close();
}
}