From 1d105b91b9678ffaa8eddea2218ed1789f5be27c Mon Sep 17 00:00:00 2001 From: Oskar Date: Sat, 3 Aug 2024 22:51:15 +0200 Subject: more assignments --- 2.42-v5.cpp | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 2.42-v5.cpp (limited to '2.42-v5.cpp') diff --git a/2.42-v5.cpp b/2.42-v5.cpp new file mode 100644 index 0000000..d28b6b8 --- /dev/null +++ b/2.42-v5.cpp @@ -0,0 +1,41 @@ +#include +#include "sales_data.hpp" + +/* + * + * 2.41 - 1.25 + * + * + */ + +int main() { + + SalesData total; // variable to hold data for the next transaction + double price = 0.0; + if (std::cin >> total.BookNo >> total.UnitsSold >> price) { + total.Revenue = price * total.UnitsSold; + SalesData trans; // variable to hold the running sum + while (std::cin >> trans.BookNo >> trans.UnitsSold >> price) { + trans.Revenue = price * trans.UnitsSold; // We can either keep this line, or comment this line out and uncomment line 34. Same behavior either way, just 2 different ways of doing the same thing. Though i do think that this is the more "correct" way of doing it + if (total.BookNo == trans.BookNo) { + total.UnitsSold += trans.UnitsSold; + total.Revenue += trans.Revenue; + } else { + double average = total.Revenue / total.UnitsSold; + std::cout << total.BookNo << " " << total.UnitsSold << " " + << total.Revenue << " avg: " << average << std::endl; + total = trans; + //total.Revenue = price * total.UnitsSold; + } + } + + double average = total.Revenue / total.UnitsSold; + std::cout << total.BookNo << " " << total.UnitsSold << " " + << total.Revenue << " avg: " << average << std::endl; + } else { + std::cerr << "No data?!" << std::endl; + return -1; + } + + return 0; +} -- cgit v1.2.3