diff options
Diffstat (limited to '2.42-v5.cpp')
-rw-r--r-- | 2.42-v5.cpp | 41 |
1 files changed, 41 insertions, 0 deletions
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 <iostream> +#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; +} |