Comparing parameters in a while loop

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.

Any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#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
bool operator == (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;
}
Last edited on
What has comparing the ISBNs to do with the while loop?
The while loop doesn't store items as item1, item2, etc so there is never a second item to use for
 
if (item.bookNo == item.bookNo) {}

so this approach can't be taken unless there is a way to circumvent this issue.

If you don't see a problem, can you propose a solution?
Well, clearly you'll need to store that information, then.

Can you be more explicit about what it is you actually want your program to do?
@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.
Last edited on
Topic archived. No new replies allowed.