Add and divide money function

I have a project where i need to get the user to input some dollar amounts and then add them and then get the average, however im having trouble with the add and div function to get the average, could you guys take a look and give any advice
line 75 and 91

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
#include <iostream>
using namespace std;

struct Money
{
	int dollars;
	int cents;
};

Money readMoney();
Money addMoney(Money x, Money y);
Money divMoney (Money x, int n);
void displayMoney (Money x);

int main()
{
	Money x[500], y[500],total;
	int count =0;


	for (int i=0; i <500; i++)
	{
		x[i]=readMoney();

		if (x[i].dollars==0 && x[i].cents==0)
			break;
			
		count ++;
	}
	if (count==0)
		cout << "Your average value is undefined";

	for (int j=0; j<500; j++)

	addMoney(x[j],y[j]);

	
	for (int g=0; g<500; g++)
	{

	total = divMoney (x[g], count);
	}
	
	

	displayMoney(total);
	
	

}

Money readMoney()
{
	char dollarsign,decimal;
	Money input; //Should this be an array?

	

	cout << "Enter and amount of money ($0.00 when done): ";
	cin >> dollarsign >>input.dollars >> decimal >> input.cents;
	cout << endl;

	while (dollarsign != '$' || input.dollars < 0 || decimal != '.' || input.cents< 0)
	{
		cout << "Invalid entry, please re-enter ($dd.cc) : ";
		cin >> dollarsign >>input.dollars >> decimal >> input.cents;
		cout << endl;
	}

	return input;
		

}

Money addMoney (Money x, Money y)
{
	Money sum;
	int dollars=0, total_cents=0;

	total_cents=(x.cents+y.cents)/100;

	dollars=((total_cents) - (total_cents)%100)/100;

	sum.dollars=dollars;

	
	return sum;

}

Money divMoney (Money x, int n)
{
	Money quotient;

	quotient.dollars = x.dollars/n;

	
	return quotient;
}
void displayMoney (Money x)
{
	cout << "The average amoutn of money you entered is  $" << x.dollars <<"." << x.cents;
}
Line 35: addMoney(x[j],y[j]); what value have y[j]?

Line 41: total = divMoney (x[g], count); total are always = to divMoney (x[499], count);
Function Money addMoney (Money x, Money y) is a mess. You return cents only, and lose dollars.
I know its a mess thats why i need help
If you have $4.52 in your right hand, and $6.83 in your left, how much money do you have? How did you do the calculation? This is a good way to figure out what to do when you're stuck on a particular algorithm.

Also, just a thought (you don't need to do this): you might like Money operator+(Money x, Money y); better then "addMoney." Then you just code x + y instead of addMoney(x, y);. Same with divideMoney...
Last edited on
@paper32
I know its a mess thats why i need help

Look at my first post, fix problems I give you and we will continue.

Do not expect full code, it is only for occasionally. This is training web page, no automatic homework web page.

@Mathhead200:
look at his code. Operator overloading is out of his league. At least for now.
Last edited on
Topic archived. No new replies allowed.