MCPcopy Create free account
hub / github.com/Apress/beginning-cpp20 / main

Function main

Exercises/NoModules/Chapter 05/Soln5_07.cpp:9–51  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7#include <vector>
8
9int main()
10{
11 std::vector<size_t> product_id;
12 std::vector<size_t> quantity;
13 std::vector<double> unit_cost;
14
15 // Read the records
16 while (true)
17 {
18 std::cout << "Enter a record - product number, quantity, unit cost separated by spaces: ";
19 size_t id {};
20 size_t n {};
21 double cost {};
22 std::cin >> id >> n >> cost;
23
24 product_id.push_back(id);
25 quantity.push_back(n);
26 unit_cost.push_back(cost);
27
28 std::cout << "Do you want to enter another record (Y or N): ";
29 char answer {};
30 std::cin >> answer;
31 if (std::toupper(answer) == 'N') break;
32 }
33
34 // Column headings
35 std::cout << std::format("{:10} {:10} {:10} {:10}\n",
36 "Product", "Quantity", "Unit Price", "Cost");
37
38 double total_cost {};
39 for (size_t i {}; i < product_id.size(); ++i)
40 {
41 const auto cost{ quantity[i] * unit_cost[i] };
42
43 std::cout <<
44 std::format("{:<10} {:<10} ${:<9.2f} ${:<9.2f}\n",
45 product_id[i], quantity[i], unit_cost[i], cost);
46
47 total_cost += cost;
48 }
49 // Note the little trick to add empty space...
50 std::cout << std::format("{:33}${:<9.2f}\n", "", total_cost);
51}

Callers

nothing calls this directly

Calls 2

push_backMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected