-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
170 lines (141 loc) · 3.52 KB
/
main.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
#include <iostream>
#include <fstream>
#include <string>
#include <list>
const std::string CURRENT_VERSION = "1.1.0";
const std::string FILE_NAME = "list.txt";
enum Commands {
newTodo = 'n',
deleteTodo = 'd',
showTodos = 's',
clearAll = 'c',
};
void startUp();
void requestOnNewCommand(char& _command);
void stateUpdate(std::list<std::string>& todos);
void _newTodo(std::list<std::string>& todos) {
std::cin.clear();
std::string todo = "";
std::cout << "Enter your todo: ";
if (todo.length() != 0) {
std::cout << "Error!" << std::endl;
return;
}
std::cin >> todo;
std::cout << std::endl;
while (std::cin.get() != '\n') {
std::string tmp = "";
std::cin >> tmp;
todo += " " + tmp;
}
todos.push_back(todo);
stateUpdate(todos);
}
void _deleteTodo(std::list<std::string>& todos) {
unsigned int number = 0;
std::cout << "Enter number of your todo: ";
std::cin >> number;
if (std::cin.fail()) {
std::cin.clear();
std::cin.ignore();
std::cout << "Not num." << std::endl;
return;
}
if (number - 1 < 0 || number - 1 >= todos.size()) {
std::cout << "Incorrect position." << std::endl;
return;
}
std::list<std::string>::iterator position = todos.begin();
std::advance(position, number - 1);
todos.erase(position);
stateUpdate(todos);
}
void _showTodos(std::list<std::string>& todos) {
unsigned int counter = 0;
if (todos.empty()) {
std::cout << "You haven't any todos." << std::endl;
return;
}
std::cout << std::endl;
for (auto& todo: todos)
std::cout << ++counter << ") " << todo << std::endl;
std::cout << std::endl;
}
void _clearAll(std::list<std::string>& todos) {
todos.clear();
std::cout << "Cleared all todos." << std::endl;
stateUpdate(todos);
}
int main() {
setlocale(LC_ALL, "eng");
std::ifstream listIn(FILE_NAME);
if (!listIn.is_open()) {
std::cout << "Have no needed file in this directory." << std::endl;
system("pause");
exit(100);
}
std::list<std::string> todos = {};
std::string text;
while (getline(listIn, text))
todos.push_back(text);
listIn.close();
startUp();
char command;
while (true) {
requestOnNewCommand(command);
switch (command){
case Commands::newTodo:
_newTodo(todos);
continue;
case Commands::deleteTodo:
_deleteTodo(todos);
continue;
case Commands::showTodos:
_showTodos(todos);
continue;
case Commands::clearAll:
_clearAll(todos);
continue;
}
}
return 0;
}
void startUp() {
std::cout << "You are welcome in todos application (" << CURRENT_VERSION << ")." << std::endl << std::endl;
std::cout << "You can: " << std::endl;
std::cout << "create [n]ew todo." << std::endl;
std::cout << "[d]elete todo." << std::endl;
std::cout << "[s]how todos." << std::endl;
std::cout << "[c]lear all todos." << std::endl;
std::cout << std::endl << std::endl;
}
void requestOnNewCommand(char& _command) {
std::string command;
std::cout << "Enter command: ";
std::cin >> command;
while (std::cin.get() != '\n') {
std::string tmp = "";
std::cin >> tmp;
command += " " + tmp;
}
if (command.length() > 1 || (command[0] != Commands::clearAll &&
command[0] != Commands::deleteTodo &&
command[0] != Commands::newTodo &&
command[0] != Commands::showTodos)) {
std::cout << "Error command!!! Try again." << std::endl << std::endl;
_command = ' ';
return;
}
_command = command[0];
}
void stateUpdate(std::list<std::string>& todos) {
std::ofstream listOut(FILE_NAME);
if (!listOut.is_open()) {
std::cout << "Something went wrong.";
exit(200);
}
for(auto& todo: todos) {
listOut << todo << '\n';
}
listOut.close();
}