The goal of this program is to add the units_sold and revenue for books with the same ISBN. Unfortunately, for this program, I haven't figure out a way to compare the ISBNs. I overloaded the == operator just in case but don't see how to use it since I am using a while loop to enter in the books' data. I have also thought about adding #include <cassert>, but if write assert(bookN0 == data2.bookNo); in my overloaded addition operator, it wont work as well.
#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);
};
// logic to determine if sales data have the same ISBN
// may or may not be used depends on solution
booloperator == (const Sales_data & data1, const Sales_data & data2) {
// boolen comparision to produce equal
return data1.bookNo == data2.bookNo;
}
// 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;
}
@Mikey, In my first sentence, I discuss the scope of the program. It adds the units_sold and revenue for books with the same ISBN.
When a book is entered with a different ISBN, I will use cerr to issue an error message but I don't need help with constructing a cerr output.
Also, I have tried with other while loops to index such entries but was never able to do it. By this I mean, I tried to write while loops that went item_i where i would be 1,2,... and item would still be of type Sales_data but I couldn't figure that out.