writing a function to return change

how do i write a function that accepts one argument as the change. The function should convert the change in numbers of dollar, quarter, dime, nickel and penny.

for example: if someone pays $5.00 for a total $3.14 purchase, the program should have 1 dollar, 3 quarters, 1 dime and 1 cent as the result.
You would use a struct or class to hold the amount.

e.g.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct Change {
 int Dollars;
 int Cents;
}

Change getChange(double Amount) {

 Change myChange;
 myChange.Dollars =  (int)floor(Amount);
 myCHange.Cents = (int)(Amount % 1.0);
 return myChange;

}

int main() {

 Change myChange = getChange(5.14);

 cout << "Change Was: " << myChange.Dollars << "dollars and " << myChange.Cents << "cents" << endl
}


Now, That most-likely won't compile or run as-is. But you should have a VERY good starting point to work from now.
ok i don't really understand that completely, but i'll show u what i have.
my assignment is to:
Problem: Write a C++ program for a cash register with change machine. The program will read in purchases and payment. It then calculates the purchase total amount and changes which will be dispensed through the change machine. For example, if someone pays $5.00 for a total $3.14 purchase, the program should have 1 dollar, 3 quarters, 1 dime and 1 cent as the result. The program should have the following functions in addition to the main function:

* A function that asks the user for purchased item prices and calculates the total purchase amount. There should no limit on how many items that can be purchased. Assume 5% tax rate. The calculated subtotal, taxes and total purchase amount should be returned as float.
* A function asks the user for the amount of payment. This value should be returned as float.
* A function that accepts two arguments as the total purchase amount and payment amount. The function should return the amount for change.
* A function that accepts one argument as the change. The function should convert the change in numbers of dollar, quarter, dime, nickel and penny.
* A function that prints out the complete transaction in the following format:

SubTotal: xxxxxxxxxx.xx
Taxes (5%): xxx.xx
Purchase Total Amount: xxxxxxxxxx.xx

Payment Amount: xxxxxxxxx.xx

Change: xxxxxxx dollars, x quarters, x dimes, x nickels, x pennies

Input Validation: Do not accept negative dollar amount.

and this is what i have so far

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

double purchasedItems();
float paymentAmount();

int main()
{

	double totalPrice=0.0;
	double salesTax=0.0;
	double totalAmount=0.0;
	float moneyAmount;
	
	cout << "********************************CASH REGISTER********************************"<< endl << endl << endl;
	
	totalPrice = purchasedItems();
	salesTax = totalPrice * .05;
	totalAmount = totalPrice + salesTax;

	cout << "\n\nSubtotal: ";
	cout << fixed << (setprecision(2)) << (setw(67)) << showpoint << totalPrice << right << endl;

	cout << "\nTaxes(5%): ";
	cout << fixed << (setprecision(2)) << (setw(66)) << showpoint << salesTax << right << endl;

	cout << "\nPurchased Total Amount: ";
	cout << fixed << (setprecision(2)) << (setw(53)) << showpoint << totalAmount << right << endl << endl;

	moneyAmount = paymentAmount();

	cout << "\n\nPayment Amount: ";
	cout << fixed << (setprecision(2)) << (setw(61)) << showpoint << moneyAmount << right << endl;

	cout << "\nChange: " << endl << endl << endl;
		
	return 0;
}

double purchasedItems()
{
	double price=0.0;
	double totalPrice=0.0;
	char repeat;

	while(true)
	{
		cout << "Enter Price of Item(xx.xx): ";
		cin >> price;
			
		totalPrice += price;
	
		cout << "Would you like to continue? (Y/N) ";
		cin >> repeat;

		if(repeat!='Y' && repeat!='y')
		{
			break;
		}
	}

	return totalPrice;
}

float paymentAmount()
{
	float moneyAmount;

	cout << "How much are you paying(xx.xx)? ";
	cin >> moneyAmount;

	return moneyAmount;
}


i basically have every done except the change part. where can i insert the "change" so that it'll run?
You need to find ways to represent the 'change'. And you need to find a way to determine the value of the change.

I would agree with Zaita that you need a struct to represent the change if you can only use one function to figure out what the change is.

I don't like the idea of doing your homework for you but if you need help to think it through, think of how you would find how to divide the change yourself. If a customer's change has total value of $1.86, you would see that it is greater than a dollar. Then you would see that after that dollar, it is still greater than a quarter. And after that it would be greater than a quarter again, etc.
I see some tricky C/C++ rounding errors coming up :-)
Topic archived. No new replies allowed.