-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathto do list.cpp
157 lines (153 loc) · 3.83 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <iostream>
#include <vector>
#include <fstream>
using namespace std;
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<<"List: "<<fileName<<"...\n";
for(int c = 0; c < totalList.size(); c++)
{
cout<<(c+1)<<". "<<totalList[c]<<"\n";
}
cout<<"\n\n\n\n\n";
}
int main()
{
string trash;
string fileName;
vector<string> totalList;
vector<string> names;
if (ifExists("lists.txt"))
{
names = pullFile("lists.txt");
}
cout<<"File List: \n\n";
if(names.size() == 0)
{
cout<<"No lists found.\n";
}
else
{
for(int c = 0; c < names.size(); c++)
{
cout<<(c+1)<<". "<<names[c]<<"\n";
}
}
while (true)
{
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);
}
while (true)
{
printList(totalList, fileName);
int choice = 0;
cout<<"What would you like to do?\n";
cout<<"1. Add Data.\n";
cout<<"2. Remove Data.\n";
cout<<"3. Exit.\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 Insert new Data: ", cin>>newItem, cout<<"\n";
if(cin.fail())
{
cin.ignore();
cin.clear();
rewind(stdin);
cout<<"Enter Insert new Data: ", cin>>newItem, cout<<"\n";
}
totalList.push_back(newItem);
}
else if(choice == 2)
{
int itemNumber = 0;
cout<<"Enter Data number to Remove: ", cin>>itemNumber, cout<<"\n";
while(cin.fail() || itemNumber < 1 || itemNumber > totalList.size())
{
cin.ignore();
cin.clear();
rewind(stdin);
cout<<"Enter Data number to Remove: ", 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();
}
}
}