-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
164 lines (125 loc) · 3.63 KB
/
Copy pathmain.cpp
File metadata and controls
164 lines (125 loc) · 3.63 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
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <cctype>
#include <filesystem>
void addItem();
void viewItem();
void deleteItem();
int main(){
std::cout << "SHOPPING LIST" << '\n';
while(true){
std::string choice;
std::cout << '\n';
std::cout << "What would you like to do? add(a), view(v), delete[d], quit[q] >>> ";
std::getline(std::cin, choice);
if(choice=="a" || choice=="add" || choice=="ADD"){
addItem();
}
else if(choice =="v" || choice == "view" || choice == "VIEW"){
viewItem();
}
else if(choice=="d"){
deleteItem();
}
else if(choice=="quit" || choice == "q" || choice == "QUIT"){
break;
}
else{
std::cout << "Invalid Choice." << '\n';
}
}
return 0;
}
void addItem(){
bool validPrice=false;
std::string newItem;
std::string itemPrice;
std::cout << "Enter the item you would like to add >>> ";
std::getline(std::cin,newItem);
while(validPrice==false){
std::cout << "Enter price of " << newItem << " >>> ";
std::getline(std::cin,itemPrice);
bool isNumber = true;
for(char c : itemPrice){
if(!std::isdigit(c)){
isNumber = false;
break;
}
}
if(!isNumber || itemPrice.empty()){
std::cout << "Price should be a number! \n";
}
else{
std::ofstream out("items.dat", std::ios::app);
out << newItem << "|" << itemPrice << '\n';
std::cout << newItem << " added to list. \n";
validPrice=true;
}
}
}
void viewItem(){
std::ifstream in("items.dat");
if(!in){
std::cout << "Shopping list is empty and file is NOT created.\n";
std::cout << "Start by adding things into the shopping list. \n \n";
return;
}
std::string line;
bool foundSomething = false;
int i=1;
while(std::getline(in,line)){
foundSomething=true;
std::istringstream ss(line);
std::string name, price;
std::getline(ss,name,'|');
std::getline(ss,price,'|');
std::cout << i << ") " << name << " - " << price << "$" << '\n';
i++;
}
if(!foundSomething){
std::cout << "List is empty. \n";
}
}
void deleteItem(){
int i=1;
int delChoice;
std::string line;
std::ifstream in("items.dat",std::ios::app);
std::ifstream inTemp("items_temp.dat",std::ios::app);
while(std::getline(in,line)){
std::istringstream ss(line);
std::string name,price;
std::getline(ss,name,'|');
std::getline(ss,price,'|');
std::cout << i << ") " << name << " - " << price << "$" << '\n';
i++;
}
std::cout << "Which one would you like to delete? >>> ";
std::cin >> delChoice;
std::cin.clear();
std::cin.ignore(10000000,'\n');
std::filesystem::copy_file("items.dat","items_temp.dat", std::filesystem::copy_options::overwrite_existing);
std::ofstream out("items.dat",std::ios::trunc);
in.clear();
in.seekg(0);
i=1;
while(std::getline(inTemp,line)){
if(delChoice==i){
i++;
continue;
}
else{
std::istringstream ss(line);
std::string name,price;
std::getline(ss,name,'|');
std::getline(ss,price,'|');
out << name << "|" << price << '\n';
i++;
}
}
inTemp.close();
out.close();
std::filesystem::remove("items_temp.dat");
}