-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo.cpp
More file actions
69 lines (58 loc) · 1.66 KB
/
todo.cpp
File metadata and controls
69 lines (58 loc) · 1.66 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
#include <iostream>
#include <vector>
#include <string>
struct Task {
std::string description;
bool done;
Task(const std::string& desc) : description(desc), done(false) {}
};
class TodoList {
std::vector<Task> tasks;
public:
void add(const std::string& task) {
tasks.emplace_back(task);
std::cout << "Added: " << task << "\n";
}
void list() const {
if (tasks.empty()) {
std::cout << "No tasks in the list.\n";
return;
}
for (size_t i = 0; i < tasks.size(); i++) {
std::cout << i + 1 << ". [" << (tasks[i].done ? 'x' : ' ') << "] " << tasks[i].description << "\n";
}
}
void mark_done(size_t index) {
if (index == 0 || index > tasks.size()) {
std::cout << "Invalid task number.\n";
return;
}
tasks[index - 1].done = true;
std::cout << "Task " << index << " marked as done.\n";
}
};
int main() {
TodoList todo;
std::string command;
std::cout << "Simple Todo List\nCommands: add <task>, list, done <task number>, quit\n";
while (true) {
std::cout << "> ";
getline(std::cin, command);
if (command == "quit") break;
else if (command.substr(0, 4) == "add ") {
todo.add(command.substr(4));
}
else if (command == "list") {
todo.list();
}
else if (command.substr(0, 5) == "done ") {
size_t index = std::stoi(command.substr(5));
todo.mark_done(index);
}
else {
std::cout << "Unknown command.\n";
}
}
std::cout << "Goodbye!\n";
return 0;
}