Jump to content

Creating a function that returns change.

KungPaoChino

Hey forum , right now I am having trouble with functions. I successfully built functions that calculated tax on an item and tips on a bill. Then in the main function, it will print out all these values onto the console.

The other two functions I have built asks for the user for amount returned  and uses that information to calculate the amount of change the cashier must give back to the customer.

 

The part I do not understand ( is how the function must tell the cashier what kind of bill it should give back to the customer.  (the number of: $20’s, $10’s, $5’s, $1’s, quarters, dimes, nickels and pennies to be given to the customer.)

 

Language below is C++.  The code is still WIP. 

[ code ]

#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std;


float item1, TAX, tip1, tip2, tip3, salesTax, ctip, balance_Due, balance_Return, change;

float CalcSalesTax(float item1, float TAX)
{
	float salesTax;
	salesTax = (item1 * TAX)*.01;
	return salesTax;
}

float CalcTip(float calcTip)
{
	ctip = item1 * calcTip;
	return (ctip);

}

float takeBalance(float balance_Due) {
	float balance_Return;
	cin >> balance_Return;
	return (balance_Return);
}

float returnChange(float balance_return) {
	float change = balance_Return - balance_Due;
	return change;
}

int main()
{


	cout << "Enter the amount the food purchased:" << endl;
	cin >> item1;
	cout << "Enter the value for salesTax between 2.4% and 14%" << endl;
	cin >> TAX;

	cout << " The cost of the item is: " << item1 << endl;
	salesTax = CalcSalesTax(item1, TAX);
	cout << "The sales tax is : $" << std::setprecision(2) << salesTax << endl;
	cout << "The total is: $" << std::setprecision(2) << item1 + salesTax << endl;
	tip1 = CalcTip(0.15);
	tip2 = CalcTip(0.18);      
	tip3 = CalcTip(0.20);
	
	cout << "The recommended tips are 15% 18% and 20%. The values are in that order: " << endl;
	cout << "15% : " << tip1 << endl;
	cout << "18% : " << tip2 << endl;
	cout << "20% : " << tip3 << endl;

	takeBalance(balance_Due);

	system("pause");
	return 0;

}

[ /code ]

 

 

 

 

 

LTT CSGO SERVER! IP 8.12.22.45!~  Connect by connecting on csgo console

Use console command "connect"   --->  connect 8.12.22.45

Link to comment
Share on other sites

Link to post
Share on other sites

return the least amount of different currency. If the change is $60.51 a $50 a $10 two quarters and a penny

 

Set each distinct bill(100, 50, 20, 10, 5, 1, etc) and change (Half dollar, quarter, nickel, dime, penny) as individual variables

 

Here's an example that might help

 

https://answers.yahoo.com/question/index?qid=20120909125542AAcQonO

- ASUS X99 Deluxe - i7 5820k - Nvidia GTX 1080ti SLi - 4x4GB EVGA SSC 2800mhz DDR4 - Samsung SM951 500 - 2x Samsung 850 EVO 512 -

- EK Supremacy EVO CPU Block - EK FC 1080 GPU Blocks - EK XRES 100 DDC - EK Coolstream XE 360 - EK Coolstream XE 240 -

Link to comment
Share on other sites

Link to post
Share on other sites

6 minutes ago, TidaLWaveZ said:

Oh I see.

 

So division is the solution. If the number is divisible to a number, then that is how many bills of that value is taken out from the cashier. Then the remainder is then taken to calculate how much of the smaller bill or coin is needed. I see. This makes total cents.... badumptss Thanks though.

LTT CSGO SERVER! IP 8.12.22.45!~  Connect by connecting on csgo console

Use console command "connect"   --->  connect 8.12.22.45

Link to comment
Share on other sites

Link to post
Share on other sites

Not spoonfeeding you here but I will explain what you have to do to get to what you want to happen

 

Make a variable array that stores what money has been given to the machine.

 

Calculate the cost of the items and subtract the cost of the item from the first array.

 

Calculate Taxes

 

Calculate tip (I saw that in code)

 

Calculate amount of change.

 

Find a way to Calculate how many $20, $10, $5, Quarters, Pennies, eg. Need to be given based on the amount of change left. Using if statements.

 

Dispense Money

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

32 minutes ago, 509Vince said:

Not spoonfeeding you here but I will explain what you have to do to get to what you want to happen

 

Make a variable array that stores what money has been given to the machine.

 

Calculate the cost of the items and subtract the cost of the item from the first array.

 

Calculate Taxes

 

Calculate tip (I saw that in code)

 

Calculate amount of change.

 

Find a way to Calculate how many $20, $10, $5, Quarters, Pennies, eg. Need to be given based on the amount of change left. Using if statements.

 

Dispense Money

 

 

Only asked about how I would calculate how many 20 10 5 1 quarter etc required to give back to the customers. The other parts I understand. As far as arrays. Arrays are not required. 

LTT CSGO SERVER! IP 8.12.22.45!~  Connect by connecting on csgo console

Use console command "connect"   --->  connect 8.12.22.45

Link to comment
Share on other sites

Link to post
Share on other sites

Lemme get this straight :

You know the change that must be given back to the customer.

You want to find a sequence of minimum length of values from a given set which added together make the given value.

 

For coin systems used in most countries, this problem can be solved via a greedy approach of picking the largest denomination of coin which is not greater than the remaining amount to be made. Very simple

 

However, this does not work with all coin systems. Let's say the coin system you use can be represented by the set of coin denominations {1,3,4} and you were supposed to give a sum equal to 6 back to the customer. The greedy algorithm would produce {4,1,1}, while the most optimal solution is {3,3}. In such cases, dynamic programming is used, the problem basically turning into the knapsack problem.

i5 4670k @ 4.2GHz (Coolermaster Hyper 212 Evo); ASrock Z87 EXTREME4; 8GB Kingston HyperX Beast DDR3 RAM @ 2133MHz; Asus DirectCU GTX 560; Super Flower Golden King 550 Platinum PSU;1TB Seagate Barracuda;Corsair 200r case. 

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, Nineshadow said:

 

However, this does not work with all coin systems. Let's say the coin system you use can be represented by the set of coin denominations {1,3,4} and you were supposed to give a sum equal to 6 back to the customer. The greedy algorithm would produce {4,1,1}, while the most optimal solution is {3,3}. In such cases, dynamic programming is used, the problem basically turning into the knapsack problem.

Yeah my program does {4.1.1} rather than {3.3}...
Then again my code isn't suppose to give the optimal solution. 

 

I did this which sort of is what you were talking about. 

Quote

coin_quarter = 0;
				while (change >= 0.25) {
					coin_quarter++;
					change = change - 0.25;
				}

				coin_dime = 0;
				while (change >= 0.10) {
					coin_dime++;
					change = change - 0.10;
				}

				coin_nickle = 0;
				while (change >= 0.05) {
					coin_nickle++;
					change = change - 0.05;
				}

				coin_penny = 0;
				while (change >= 0.01) {
					coin_penny++;
					change = change - 0.01;
				}

 

 

LTT CSGO SERVER! IP 8.12.22.45!~  Connect by connecting on csgo console

Use console command "connect"   --->  connect 8.12.22.45

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×