Classes

closed account (EwCjE3v7)
Okay, so I have an exercise: Rewrite the program from page 255 to use the istream constructor.

But I do not know how to initialize it from constructions like usually I would do t test = 5, but with this, it gives me errors like int cant be converted to Sales_data...

So the class header file is below

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
53
54
55
56
57
58
59
60
61
62
63
64
#ifndef SALES_DATA_H
#define SALES_DATA_H

#include <iostream>
#include <string>

struct Sales_data {

  Sales_data() = default;

  Sales_data(const std::string &s) : bookNo(s) {}

  Sales_data(const std::string &s, unsigned n, double p) : bookNo(s), units_sold(n), revenue (p*n) {}

  Sales_data(std::istream &);


  std::string isbn() const // funtion to call for the ISBN of the current book
  {
   	 return bookNo; // return the book numbers
  }

  Sales_data& combine (const Sales_data&rhs) // to combine two Objects of Sales_data
  {
  	units_sold += rhs.units_sold; // add the amount of books sold
  	revenue += rhs.revenue;       // for adding the revenue
  	return *this;                 // return the object that the funtion was called on
  }

	double avg_price() const // for calculating the average price
  {
		if (units_sold)
      return revenue/units_sold; // return: devide revenue by the amount of units sold
  	else
  	 return 0;                   // else return nothing
  }

  std::string bookNo;      // for holding our book number/ISBN
  unsigned units_sold = 0; // for holding the amount of books sold
  double revenue = 0.0;    // for holding the price they were sold at
};

std::istream &read(std::istream &is, Sales_data &item) // funtion for getting input into a Sales_data object
{
  double price = 0;                              // for getting our price that the objects were sold at
  is >> item.bookNo >> item.units_sold >> price; // get input
  item.revenue = price * item.units_sold;        // calculate the revenue
  return is;
}

std::ostream &print(std::ostream &os, const Sales_data &item) // funtion for output
{
  os << item.isbn() << " " << item.units_sold << " " << item.revenue << " " << item.avg_price(); // print out values
  return os;        // return the output stream
}

Sales_data add(const Sales_data &lhs, const Sales_data &rhs) // for adding to Sales_data objects
{
  Sales_data sum = lhs; // sum for returning
  sum.combine(rhs);     // combine sum with rhs
  return sum;           // return the combined object
}

#endif // SALES_DATA_H 


And the program from page 255 is here:

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
// Keyboard keys \ |
#include <iostream>
#include "Sales_data.h"
using std::cout; using std::endl; using std::cin;
using std::cerr;

int main()
{
	Sales_data total = cin;
	if (read(cin, total)) { // if we get input into total
		Sales_data trans; // define trans to work more or the same book 

		while (read(cin, trans)) { // while we get input into trans
			if (total.bookNo == trans.bookNo) {     // if we are proccessing the same book
				total.combine(trans);
			} else { // if we get input for another book
				print(cout, total); // print the results of the previous book

				// assign total the value of the new book
				total.bookNo = trans.bookNo;        
				total.units_sold = trans.units_sold;
				total.revenue = trans.revenue;
			}
		}
		print(cout, total); // print the results of the previous book
	}

	return 0;
}
Topic archived. No new replies allowed.