Adding and subtracting money question

I've been introduced with friend functions and the like and have been trying to fix my code. I can't see what is wrong with it though. Any help will 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
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
 // This program adds money of two different people 
#include<iostream> 
#include<cstdlib> 
using namespace std;
class AltMoney
{
public:
	AltMoney();
	AltMoney(int d, int c);
	void display_money();
	friend AltMoney add(AltMoney m1, AltMoney m2);//changed add function to friend and added it up here in public
	void read_money(int& d, int& c);
	friend AltMoney subtract(AltMoney m1, AltMoney m2);//added a subtract function
private:
	int dollars;
	int cents;
};
int main()
{
	int d, c;
	AltMoney m1, m2, sum, sub;
	sum = AltMoney(0, 0);
	m1.read_money(d, c);
	m1 = AltMoney(d, c);
	cout << "The first money is:";
	m1.display_money();
	m2.read_money(d, c);
	m2 = AltMoney(d, c);
	cout << "The second money is:";
	m2.display_money();
	sum = add(m1, m2);//sum will now be equal to the add function and calls upon it as well 
	cout << "The sum is:";
	sum.display_money();
	sub = subtract(m1, m2);//sub will now be equal to the subtract function and calls upon it as well 
	cout << "The subtraction is:";
	sub.display_money();
	return 0;
}
AltMoney::AltMoney()
{
	dollars = 0;
	cents = 0;
}
AltMoney::AltMoney(int d, int c)
{
	dollars = d;
	cents = c;
}
void AltMoney::display_money()
{
	cout << "$" << dollars << ".";
	if (cents <= 9)
		cout << "0";  //to display a 0 in the left for numbers less than 10 
	cout << cents << endl;
}
AltMoney add(AltMoney m1, AltMoney m2)
{
	int extra = 0;
	AltMoney sum;//local in this block only
	sum.cents = m1.cents + m2.cents;
	if (sum.cents >= 100) {
		sum.cents = sum.cents - 100;
		extra = 1;
	}
	sum.dollars = m1.dollars + m2.dollars + extra;
	return sum;
}
AltMoney subtract(AltMoney m1, AltMoney m2)
{
	int extra = 0;
	AltMoney sub;//local in this block only
	sub.cents = m1.cents + m2.cents;
	if (sub.cents >= 100) {
		sub.cents = sub.cents - 100;
		extra = 1;
	}
	sub.dollars = m1.dollars - m2.dollars - extra;
	return sub;
}
void AltMoney::read_money(int& d, int& c)
{
	cout << "Enter dollar \n";
	cin >> dollars;
	cout << "Enter cents \n";
	cin >> cents;
	if (dollars < 0 || cents < 0)
	{
		cout << "Invalid dollars and cents, negative values\n";
		exit(1);
	}
}



the outcome should be like this:

Enter dollar
5
Enter cents
20
First money is $5.20
Enter dollar
3
Enter Cents
40
Second money is $3.40
Sum is $8.60
Subtraction is $2.60
The problem is that you don't use d and c on line 83/85. If you use
1
2
3
4
	cout << "Enter dollar \n";
	cin >> d; // Note: d
	cout << "Enter cents \n";
	cin >> c; // Note c 
It would work, but it's not really good. If you'd omit this paramter you could also omit line 24/28
Oh! Ok that did the trick. Thank you!
Topic archived. No new replies allowed.