Const Reference [Updated] NEED HELP!

I need my program to do this:
Write a program that asks a user how much change is to be given. Pass the amount to a function via const reference. Figure out what the breakdown in change will be using $1 bills, quarters, dimes, nickels and pennies. Use pointer notation to return the monetary values from the function called Change. The program should properly compute the change to be given no matter what the amount entered by the user.


Now I need helping changing my code so that it is passed via const reference.
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
#include "stdafx.h"

//Prototype
void Change(double* money, int* bills, int* quarters, int* dimes, int* nickels, int* pennies, int* newmoney);

int _tmain(int argc, _TCHAR* argv[])
{

	double money;
	//Assigns pointer to hold value of money
	double* pMoney = &money;

	//Assigns pointers to hold the values of bills, quarters, dimes, nickels, pennies, and newmoney
	int bills, quarters, dimes, nickels, pennies, newmoney;

	int* pBills = &bills;
	int* pQuarters = &quarters;
	int* pDimes = &dimes;
	int* pNickels = &nickels;
	int* pPennies = &pennies;
	int* pNewmoney = &newmoney;

	//User Input
	cout << "Please enter the amount of change to give: $";
	cin >> *pMoney;
	
	//Calls Change function and prints output 
	Change(pMoney, pBills, pQuarters, pDimes, pNickels, pPennies, pNewmoney);
	cout << endl;
	cout << "Please disperse " << bills << " - $1 bills";
	cout << endl;
	cout << setw(17) << *pQuarters << " - quarters";
	cout << endl;
	cout << setw(17) << *pDimes << " - dimes";
	cout << endl;
	cout << setw(17) << *pNickels << " - nickels";
	cout << endl;
	cout << setw(17) << *pPennies << " - pennies";
	cout << endl << endl;
	
	system("PAUSE");
	return 0;
}

//Function that calculates the change based off user input
void Change(double* money, int* bills, int* quarters, int* dimes, int* nickels, int* pennies, int* newmoney)
{
	*newmoney = *money * 100;
	*bills = *newmoney / 100;
	*newmoney = *newmoney % 100;
	*quarters = *newmoney / 25;
	*newmoney = *newmoney % 25;
	*dimes = *newmoney / 10;
	*newmoney = *newmoney % 10;
	*nickels = *newmoney / 5;
	*newmoney = *newmoney % 5;
	*pennies = *newmoney / 1;
}

Last edited on
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
#include "stdafx.h"

void Change(const double &money, int *bills, int *quarters, int *dimes, int *nickels, int *pennies);

int _tmain(int argc, _TCHAR* argv[])
{
	double money;
	int bills, quarters, dimes, nickels, pennies, newmoney;
	cout << "Please enter the amount of change to give: $";
	cin >> money;
	Change(money, &bills, &quarters, &dimes, &nickels, &pennies);
	cout << endl;
	cout << "Please disperse " << bills << " - $1 bills";
	cout << endl;
	cout << setw(17) << quarters << " - quarters";
	cout << endl;
	cout << setw(17) << dimes << " - dimes";
	cout << endl;
	cout << setw(17) << nickels << " - nickels";
	cout << endl;
	cout << setw(17) << pennies << " - pennies";
	
	system("PAUSE");
	return 0;
}

void Change(const double &money, int *bills, int *quarters, int *dimes, int *nickels, int *pennies)
{
	int newmoney = money * 100;
	*bills = newmoney / 100;
	newmoney = newmoney % 100;
	*quarters = newmoney / 25;
	newmoney = newmoney % 25;
	*dimes = newmoney / 10;
	newmoney = newmoney % 10;
	*nickels = newmoney / 5;
	*pennies = newmoney % 5;
}
Thank you thank you! :)

Is there any way that I can avoid the "possible loss of data" ?
You can't, not as the code stands.

newmoney is an int and money is a double.

If "money * 100" was the biggest possible value for a double, it would be much larger than the biggest possible value for an int.

Since in that case you would end up with the final value of newmoney being less than "money * 100", you would have lost some data.

This is called a narrowing conversion.

In your case, you're not losing any data, but the compiler warns you about the possibility.

If you just don't want to see the warnings, you could probably add some flags that tell the compiler to ignore the potential problem. You'd have to check the documentation for your compiler though.

One way to avoid it is to use double throughout instead of int. In that case, the current code won't work, because "%" is an integer operation. You'd have to use a different scheme to calculate the change, I'm sure you can come up with one easily.
There is one problem: due to rounding errors, it's possible for newmoney to be off by a penny. For example, you might enter 123.45, but it might be stored as 123.44999999999. Then int newmoney = money*100 will give you 12344 instead of 12345. To fix this, add 0.5 to make the calculation round the number off instead of truncate it. (or use round()):
int newmoney = money*100+0.5;
Topic archived. No new replies allowed.