-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy path08-receipt.cpp
More file actions
60 lines (55 loc) · 1.5 KB
/
08-receipt.cpp
File metadata and controls
60 lines (55 loc) · 1.5 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
// 08-receipt.cpp : output a till-receipt from user input
import std;
using namespace std;
struct Entry {
string product;
size_t quantity;
double unit_price;
inline static double total{};
};
Entry add_entry(const string& input) {
Entry e;
istringstream iss{ input };
iss >> e.product >> e.quantity >> e.unit_price;
if (iss.fail()) {
cerr << "Bad entry.\n";
e.quantity = 0;
}
else {
Entry::total += e.quantity * e.unit_price;
}
return e;
}
int main() {
vector<Entry> sales;
cout << "Please enter: PRODUCT QTY PRICE (eg. \'Apple 6 0.50\')\n";
string s;
getline(cin, s);
while(!s.empty()) {
sales.emplace_back(add_entry(s));
cout << "Please enter: PRODUCT QTY PRICE (blank line to finish)\n";
getline(cin, s);
}
cout << "====================\n";
auto f = cout.flags();
auto p = cout.precision(2);
cout.setf(ios_base::fixed, ios_base::floatfield);
for (const auto& line : sales) {
if (line.quantity) {
cout.setf(ios_base::left, ios_base::adjustfield);
cout.width(11);
cout << line.product;
cout.unsetf(ios_base::adjustfield);
cout.width(3);
cout << line.quantity;
cout.width(6);
cout << line.unit_price << '\n';
}
}
cout << "====================\n";
cout << "Total:";
cout.width(14);
cout << Entry::total << '\n';
cout.flags(f);
cout.precision(p);
}