forked from NBs21/TO-DO-LIST
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTO DO LIST.cpp
137 lines (136 loc) · 4.03 KB
/
TO DO LIST.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
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
vector<string> pullFile(string fileName);
void pushFile(vector<string> totalList, string fileName);
bool ifExists(string fileName);
void printList(vector<string> totalList, string fileName);
int main()
{
string trash;
cout << "\033[2J\033[1;1H";
string fileName;
vector<string> totalList;
vector<string> names;
if (ifExists("lists.txt")){
names = pullFile("lists.txt");
}
cout<<"File List: \n";
if(names.size() <= 1){
cout<<"No lists found.\n";
}
else{
for(int c = 0; c < names.size(); c++){
cout<<(c+1)<<". "<<names[c]<<"\n";
}
}
bool flag = true;
while (flag){
cout<<"\nEnter the number of the list you would like to load or Enter file name for a new list.\n >", cin>>fileName, cout<<"\n";
if(isdigit(fileName[0])){
int x = fileName[0] - '0';
if(x > 0 && x < names.size()){
fileName = names[x-1];
flag = false;
}
else{
cout<<"Not a list number.\n";
}
}
else{
fileName = fileName + ".txt";
names.push_back(fileName);
flag = false;
}
}
pushFile(names, "lists.txt");
if (ifExists(fileName)){
totalList = pullFile(fileName);
}
bool cont = true;
while (cont){
printList(totalList, fileName);
int choice = 0;
cout<<"What would you like to do?\n";
cout<<"1. Add item.\n";
cout<<"2. Remove item.\n";
cout<<"3. Quit.\n";
cout<<">", cin>>choice, cout<<"\n";
while(cin.fail()){
cin.ignore();
cin.clear();
rewind(stdin);
cout<<">", cin>>choice, cout<<"\n";
}
if(choice == 1){
string newItem;
cout<<"Enter new item: ", cin>>newItem, cout<<"\n";
if(cin.fail()){
cin.ignore();
cin.clear();
rewind(stdin);
cout<<"Enter new item: ", cin>>newItem, cout<<"\n";
}
totalList.push_back(newItem);
}
else if(choice == 2){
int itemNumber = 0;
cout<<"Enter item number to delete: ", cin>>itemNumber, cout<<"\n";
while(cin.fail() || itemNumber < 1 || itemNumber > totalList.size()){
cin.ignore();
cin.clear();
rewind(stdin);
cout<<"Enter item number to delete: ", cin>>itemNumber, cout<<"\n";
}
totalList.erase(totalList.begin() + (itemNumber-1));
}
else if(choice == 3){
cont = false;
pushFile(totalList, fileName);
cout<<"Exiting.\n";
}
else{
cout<<"Invalid Input!\n";
cin.clear();
cin.ignore();
}
}
}
vector<string> pullFile(string fileName){
vector<string> totalList;
fstream ioFile;
ioFile.open(fileName.c_str(), fstream::in);
string bullet;
while (!ioFile.eof()){
getline(ioFile, bullet);
if(!bullet.empty()){
totalList.push_back(bullet);
}
}
ioFile.close();
return totalList;
}
void pushFile(vector<string> totalList, string fileName){
fstream ioFile;
ioFile.open(fileName.c_str(), fstream::out);
for(int c = 0; c < totalList.size(); c++){
if(!totalList[c].empty()){
ioFile<<totalList[c]<<"\n";
}
}
ioFile.close();
}
bool ifExists(string fileName){
ifstream infile(fileName.c_str());
return infile.good();
}
void printList(vector<string> totalList, string fileName){
cout << "\033[2J\033[1;1H";
cout<<"List: "<<fileName<<"...\n";
for(int c = 0; c < totalList.size(); c++)
{
cout<<(c+1)<<". "<<totalList[c]<<"\n";
}
cout<<"\n\n\n\n\n";
}