-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
317 lines (282 loc) · 7.72 KB
/
main.cpp
File metadata and controls
317 lines (282 loc) · 7.72 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
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
// ToDoList.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <iostream>
#include <fstream>
#include <cstring>
using namespace std;
struct ToDo {
char type;
char* task;
int month;
int day;
int priority;
};
const int MAX = 20;
void findSize(int& size, ifstream& fin) {
fin.open("todo.txt");
char temp = fin.get();
if (temp != '\n') {
while (fin.get(temp)) {
if (temp == '\n') {
size++;
}
}
}
fin.close();
}
void ReadList(ToDo list[MAX], int& size, ifstream &fin) {
fin.open("todo.txt");
for (int i = 0;i < size; i++) {
fin >> list[i].type;
fin.ignore();
list[i].task = new char[80];
fin.getline(list[i].task, 80, '\0');
fin >> list[i].month;
fin.ignore();
fin >> list[i].day;
fin.ignore();
fin >> list[i].priority;
}
fin.close();
}
void AddTask(ToDo list[MAX], int& size, char* word) {
cin >> list[size].type;
list[size].task = new char[80];
cin.ignore();
cin.getline(list[size].task,80, '0');
cin >> list[size].month;
cin >> list[size].day;
cin >> list[size].priority;
cout << endl;
size++;
}
bool DeleteTask(ToDo list[MAX], int& size, char* word) {
cout << "enter the word to delete: " << endl;
cin >> word;
for (int i = 0; i < size; i++) {
if (strcmp(list[i].task, word) == 0) {
for (; i < size; i++) {
list[i].type = list[i + 1].type;
list[i].task = list[i + 1].task;
list[i].month = list[i + 1].month;
list[i].day = list[i + 1].day;
list[i].priority = list[i + 1].priority;
}
--size;
return true;
}
}
return false;
}
void DeleteLast(ToDo list[MAX], int& size) {
for (int i = 0; i < size - 1; i++) {
list[i+1].type = list[i].type;
list[i+1].task = list[i].task;
list[i+1].month = list[i].month;
list[i+1].day = list[i].day;
list[i+1].priority = list[i].priority;
}
--size;
}
void DeleteFirst(ToDo list[MAX], int& size) {
for (int i = 0; i < size; i++) {
list[i].type = list[i + 1].type;
list[i].task = list[i + 1].task;
list[i].month = list[i + 1].month;
list[i].day = list[i + 1].day;
list[i].priority = list[i + 1].priority;
}
--size;
}
// called once within printall
int LongestTask(ToDo list[MAX], int size) {
int longest = 0;
for (int i = 0; i < size; i++) {
longest = (strlen(list[i].task) > longest) ? strlen(list[i].task) : longest;
}
return longest;
}
void consistentSpacing(ToDo list[MAX], int size, int stringlength, int longest) {
int addSpaces = stringlength;
while (addSpaces < longest) {
cout << ' ';
addSpaces++;
}
}
void PrintAll(ToDo list[MAX], int& size) {
if (size == 0) {
return;
}
int longest = LongestTask(list, size);
cout << "Type " << '\t' << " Task";
consistentSpacing(list, size, 5, longest);
cout << " Month" << " " << "Day" << ' ' << "Priority" << endl;
for (int i = 0; i < size; i++) {
// print type
if (list[i].type == 'p') {
cout << "Personal ";
}
else if (list[i].type == 's') {
cout << "School ";
}
else {
cout << "Other ";
}
// print task
cout << list[i].task;
consistentSpacing(list, size, strlen(list[i].task), longest);
cout << ' ';
// print month
if (list[i].month == 1) {
cout << "January ";
}
else if (list[i].month == 2) {
cout << "February ";
}
else if (list[i].month == 3) {
cout <<"March ";
}
else if (list[i].month == 4) {
cout << "April ";
}
else if (list[i].month == 5) {
cout << "May ";
}
else if (list[i].month == 6) {
cout << "June ";
}
else if (list[i].month == 7) {
cout << "July ";
}
else if (list[i].month == 8) {
cout << "August ";
}
else if (list[i].month == 9) {
cout << "September ";
}
else if (list[i].month == 10) {
cout << "October ";
}
else if (list[i].month == 11) {
cout << "November ";
}
else if (list[i].month == 12) {
cout << "December ";
}
else {
cout << "Error ";
}
//print day
cout << list[i].day;
cout << ' ';
//prints priority
if (list[i].priority == 1) {
cout << "Low ";
}
else if (list[i].priority == 2) {
cout << "Medium-Low ";
}
else if (list[i].priority == 3) {
cout << "Medium ";
}
else if (list[i].priority == 4) {
cout << "Medium-High ";
}
else if (list[i].priority == 5) {
cout << "High ";
}
else {
cout << ' ';
}
//newline between each entry
cout << endl;
}
//newline before new command
cout << endl;
}
void WriteAll(ToDo list[MAX],int size, ofstream& fout){
fout.open("todo.txt");
for (int i = 0; i < size; i++) {
if (list[i].type != 'p' && list[i].type != 's') {
fout << 'o';
}
else {
fout << list[i].type;
}
fout << '\t';
fout << list[i].task;
fout << '\0' << list[i].month;
fout << '\t';
fout << list[i].day;
fout << '\t';
fout << list[i].priority << endl;
}
fout.close();
}
void EraseAll(ofstream& fout) {
fout.open("todo.txt");
fout << '\0';
fout.close();
}
enum choices { ADD, DELETE, DELETEFIRST, DELETELAST, PRINTALL, EXIT };
int main() {
ToDo list[MAX];
int choice;
char word[80];
int size = 0;
ifstream fin;
ofstream fout;
findSize(size, fin);
ReadList(list, size, fin);
cout << "enter 0 to add, enter 1,2,3 to delete, enter 4 to print, enter 5 to exit" << endl;
PrintAll(list, size);
do {
cout << "enter 0-4" << endl;
cin >> choice;
switch (choice) {
case ADD: // adds a new word
if (size < MAX) {
AddTask(list, size, word);
WriteAll(list, size, fout);
}
else {
cout << "List Full. Press 1 to Delete" << endl;
}
PrintAll(list, size);
break;
case DELETE: // deletes a word
if (size > 0) {
if (DeleteTask(list, size, word) == false) {
cout << "Not Found" << endl << endl;
}
else {
WriteAll(list, size, fout);
}
}
else {
cout << "list empty" << endl;
}
PrintAll(list, size);
break;
case DELETEFIRST:
DeleteFirst(list, size);
PrintAll(list, size);
WriteAll(list, size, fout);
break;
case DELETELAST:
DeleteLast(list, size);
PrintAll(list, size);
WriteAll(list, size, fout);
break;
case PRINTALL: // prints all
PrintAll(list, size);
break;
}
} while (choice < 4); // 4 exits the program
if (choice == 15) {
EraseAll(fout);
}
return 0;
}
/*
*/