Q:

Revise your transaction-processing program from § 7.1.1 (p. 256) to use these members

0

Revise your transaction-processing program from § 7.1.1 (p. 256) to use these members.

Sales_data total; // variable to hold the running sum
if (read(cin, total)) { // read the first transaction
 Sales_data trans; // variable to hold data for the next transaction
 while(read(cin, trans)) { // read the remaining transactions
 if (total.isbn() == trans.isbn()) // check the isbns
 total.combine(trans); // update the running total
 else {
 print(cout, total) << endl; // print the results
 total = trans; // process the next book
 } }
 print(cout, total) << endl; // print the last transaction
} else { // there was no input
 cerr << "No data?!" << endl; // notify the user
}

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

#include <string>
#include <iostream>

struct Sales_data {
  std::string isbn() const { return bookNo; }
  Sales_data &combine(const Sales_data &);

  std::string bookNo;
  unsigned units_sold = 0;
  double revenue = 0.0;
};

Sales_data &Sales_data::combine(const Sales_data &rhs) {
  units_sold += rhs.units_sold;
  revenue += rhs.revenue;
  return *this;
}

int main() {
  Sales_data total;
  double price;
  if (std::cin >> total.bookNo >> total.units_sold >> price) {
    total.revenue = total.units_sold * price;
    Sales_data trans;
    while (std::cin >> trans.bookNo >> trans.units_sold >> price) {
      trans.revenue = trans.units_sold * price;
      if (total.isbn() == trans.isbn()) {
      //if (total.bookNo == trans.bookNo) {
        total.combine(trans);
        //total.units_sold += trans.units_sold;
        //total.revenue += trans.revenue;
      } else {
        std::cout << total.bookNo << " "
                  << total.units_sold << " "
                  << total.revenue << std::endl;
        total = trans;  // Use default copy constructor
        //total.bookNo = trans.bookNo;
        //total.units_sold = trans.units_sold;
        //total.revenue = trans.revenue;
      }
    }
    std::cout << total.bookNo << " "
              << total.units_sold << " "
              << total.revenue << std::endl;
  } else {
    std::cerr << "No data!" << std::endl;
    return -1;
  }

  return 0;
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now