I have two questions that are related to each other. The first one is about overloading the addition operator.
I have defined a struct as the following:
I then overloaded the I/O operators so I could print to the screen information related Sales_data.Code:#include <iostream> #include <string> struct Sales_data { std:: string bookNo; unsigned units_sold = 0; double revenue = 0.0; // member function for addition operator rules Sales_data operator + (const Sales_data & data); };
My first issue is with overloading the addition operator. Everyone works correctly except for std::cout << item << std:endl; will no not output the ISBN number only the units_sold and revenue when added together.Code:// overload ostream in order for cout to work std::ostream& operator << (std::ostream & out, const Sales_data & cSales_data) { out << cSales_data.bookNo << ", " << cSales_data.units_sold << ", " << cSales_data.revenue; return out; } // overload istream in order for cin to work std::istream& operator >> (std::istream & in, Sales_data & cSales_data) { in >> cSales_data.bookNo >> cSales_data.units_sold >> cSales_data.revenue; return in; }
Now here is the code in its entiretyCode:// addition operator rules Sales_data Sales_data::operator + (const Sales_data & data2) { units_sold += data2.units_sold; revenue += data2.revenue; return *this; }
Code:#include <iostream> #include <string> // Sales_data structure struct Sales_data { std:: string bookNo; unsigned units_sold = 0; double revenue = 0.0; // member function for addition operator rules Sales_data operator + (const Sales_data & data); }; // addition operator rules Sales_data Sales_data::operator + (const Sales_data & data2) { units_sold += data2.units_sold; revenue += data2.revenue; return *this; } // overload ostream in order for cout to work std::ostream& operator << (std::ostream & out, const Sales_data & cSales_data) { out << cSales_data.bookNo << ", " << cSales_data.units_sold << ", " << cSales_data.revenue; return out; } // overload istream in order for cin to work std::istream& operator >> (std::istream & in, Sales_data & cSales_data) { in >> cSales_data.bookNo >> cSales_data.units_sold >> cSales_data.revenue; return in; } int main(int argc, char *argv[]) { Sales_data item, total; // declare item,total as a Sales_data type // prompt the user for the information to enter std::cout << "Enter ISBN, units sold, and revenue:" << std::endl; while (std::cin >> item) { // allow the user to enter in as many items as they deem necessary // what to put here?? total = total + item; // add each new item to the total } std::cout << "The total units sold and revenue is " << total << std::endl; return 0; }
- After total = total + item;, I would like to print the total for this particular ISBN. However, I only get: blank, total units, total revenue where blank is where the ISBN would go but doesn't print after addition.
My second question has to do with comparing the ISBN's of the books entered during the while loop.
- I would like to do something like
Code:if (item_i.bookNo == item_i+1.bookNo) { total = total + item; } else { std::cerr << "Books entered must have the same ISBNs" << std::endl; }
Unfortunately, I cannot figure out how to set up a comparison of the bookNos. If I used #include <casset> in the overload + rule, it will immediately exit since I have no way to compare the ISBNs.




Reply With Quote
