Issue with class calculation functions with OOP

For some reason I keep getting two errors on lines 67 and 73. For some reason my variable "bill_before" is not getting initialized and I'm slightly confused because in the "getTips" function above there doesn't seem to be any issues.

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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
  // Chapter 7 - Programming Challenge 14, Gratuity Calculator
// This program utilizes a Tips class to calculate the gratuity on
// a restaurant meal, using whatever tip percent the patron desires.
#include <iostream>
#include <iomanip>
using namespace std;

class Tips
{
private:
	double tax_rate, bill_after_taxes, tip_rate;

public:
	//------ constructor ---------
	Tips();
	//-------- mutator functions ----------
	void setTax_rate(double);
	void setBill_after_taxes(double);
	void setTip_rate(double);

	//--------- accessor functions -------------
	double getTax_rate();
	double getBill_after_taxes();
	double getTip_rate();

	//--------- class functions that perform calculations / "read-only" properties---------
	double getTip();
	double getBillAmountBeforeTaxes();

	//------ print out class variables -----------
	void print();
};
Tips::Tips()
{
	bill_after_taxes = 100;
	tax_rate = 6.25;
	tip_rate = 15;
}
void Tips::setTax_rate(double _tax_rate)
{
	tax_rate = _tax_rate;
}
void Tips::setBill_after_taxes(double _bill_after_taxes)
{
	bill_after_taxes = _bill_after_taxes;
}
void Tips::setTip_rate(double _tip_rate)
{
	tip_rate = _tip_rate;
}
double Tips::getTax_rate()
{
	return tax_rate;
}
double Tips::getBill_after_taxes()
{
	return bill_after_taxes;
}
double Tips::getTip_rate()
{
	return tip_rate;;
}
//-------- class calculation functions ------
double Tips::getTip()
{
	double tip = (tip_rate / 100.0);
	double bill_before = (bill_after_taxes = bill_before + (bill_before * tax_rate));
	return static_cast <double> (tip) = (bill_before * tip_rate);
}
double Tips::getBillAmountBeforeTaxes()
{
	double bill_before = (bill_after_taxes = bill_before + (bill_before * tax_rate));
	return static_cast <double>(bill_before) = (bill_after_taxes = bill_before + (bill_before * tax_rate));
}

void Tips::print()
{
	cout << setprecision(2) << fixed;
	cout <<
		"\nBill after tax      : " << bill_after_taxes <<
		"\nTax rate            : " << tax_rate <<
		"\nBill Before tax     : " << &Tips::getBillAmountBeforeTaxes <<
		"\nTip rate            : " << tip_rate <<
		"\nTip Amount          : " << &Tips::getTip <<
		"\nTotal Bill with tip : " <<
		"\n------------------------------------------\n\n";

}

//------------- tester program ---------------------------------
int main()
{
	//----------- Tip object variable --------------
	Tips mytips;
	//------- setup Test Case #1 ---------
	mytips.setBill_after_taxes(75.5);
	mytips.setTax_rate(12.5);
	mytips.setTip_rate(15);
	mytips.print();

	//------- setup Test Case #2 ---------
	mytips.setBill_after_taxes(104.56);
	mytips.setTax_rate(6.25);
	mytips.setTip_rate(10);
	mytips.print();

	system("pause");
	return 0;
}
For some reason my variable "bill_before" is not getting initialized and I'm slightly confused because in the "getTips" function above there doesn't seem to be any issues.


The variable is not being initialised because it's not being initialised ....... See how that is a recursive statement? The same thing is happening on lines 67 & 72. Try working out that calculation with pen & paper - that should show why it doesn't work.

Why do you have casting on lines 68 & 73? And why does it have the same calculation as the line before? Get rid of it :+)

Good Luck !!
line 67, 72: Let's remove the arithmetic from the lines.
 
  double bill_before = bill_before;  //  Does that make any sense? 

What value do you think bill_before has?

Lines 82,84: You're printing the address of a function instead of calling the function.
You want:
 
		"\nBill Before tax     : " << Tips::getBillAmountBeforeTaxes() <<

Note the () and no &.




Topic archived. No new replies allowed.