Open Side Menu Go to the Top
Register
Noob C++ Change Making Question Noob C++ Change Making Question

10-11-2009 , 08:56 PM
I am working on a C++ app the compiles, and works. (Sort of) The purpose of the app is the user inputs an amount of money such as $200.61. The app tells you how many dollars, quarters, dimes, nickels, and pennies to return. Similar to 200 dollars, 2 quarters, 1 dime, 0 nickels, 1 penny.

Here is my code:
Code:
#include "stdafx.h"
#include <iostream>
using namespace std;
/////////////////////////////////////////////////
struct change //Structure 
{
	float total;
	int dollars;
	int quarters;
	int dimes;
	int nickels;
	int pennies;
};
//////////////////////////////////////////////////////

change makeChange (float money);

int _tmain(int argc, _TCHAR* argv[])
{
	float totalMoney;
	change changeAmount;

	cout << "Enter the amount of money to be returned to the customer:"; 
	cin >> totalMoney; 

	changeAmount = makeChange(totalMoney);
	
	cout << "Give the customer "
		<< changeAmount.dollars << " dollars,"
		<< changeAmount.quarters << " quarters,"
		<< changeAmount.dimes << " dimes,"
		<< changeAmount.nickels << " nickles and"
		<< changeAmount.pennies << " pennies.\n";

	return 0;
}
///////////////////////////////////////////////////////////////////////////////
change makeChange(float money)
{
	change makeChange;
	makeChange.dollars = static_cast<int>(money);

	float remainingMoney = money - makeChange.dollars;

	makeChange.quarters = static_cast <int> (remainingMoney*4);
	float remainingMoney1 = money - makeChange.quarters;

	makeChange.dimes = static_cast <int> (remainingMoney1*10);
	float remainingMoney2 = money - makeChange.dimes;

	makeChange.nickels = static_cast <int> (remainingMoney2*20);
	float remainingMoney3 = money - makeChange.nickels;

	makeChange.pennies = static_cast <int> (remainingMoney3*100);
	float remainingMoney4 = money - makeChange.pennies;

	return(makeChange);







Yes it is ugly when you copy and paste it. Anyhow, if you compile and run you see that I get an output for $200.61 that looks like this.
200 Dollars. 2 quarters, 1986 dimes, -35707 nickels, 3590760 pennies.

The point of the assignment is to use the function and the structure. So those need to stay. I think the problem is coming from within the function.
I'm guess the problem has something to do with the "money" variable. I have tried a few things and they all seem to make the compiler very angry.

Any tips or hints?

Last edited by kerowo; 10-11-2009 at 09:17 PM.
10-11-2009 , 09:13 PM
I'll do your C++ homework if you ship me some $ on Stars..
Ship first though.
10-11-2009 , 09:18 PM
Added the code box and some white space to make it more readable. What compiler are you using? You should be able to find the problem by stepping through the code in the compiler and looking at what is in the variables after each step.
10-11-2009 , 09:19 PM
Quote:
Originally Posted by anfernee
I'll do your C++ homework if you ship me some $ on Stars..
Ship first though.
Asking for money for help is almost as bad as offering money.

Just saying.
10-11-2009 , 09:19 PM
anfernee-I appreciate the offer, and maybe down the road. However, I'll have to decline since I have 99.99% of the code written. I struggle with small errors.

kerowo-Visual C++ Express.

Last edited by sickness_smb; 10-11-2009 at 09:34 PM. Reason: fml....
10-11-2009 , 09:41 PM
as written i believe the issue is that each time after you figure out how many of each coin you are subtracting that amount from the original total change. you find that there are 200 dollars, subtract that from the $200.61 and have 0.61 left. then u find that u need 2 quarters, but at this point u subtract the 2 quarters = 0.50 from the original total of $200.61 instead of the $0.61 that is left. and so on for the other coins

also, i don't think you are accounting for the coins being worth 0.25, 0.10, etc. it looks like you are just subtracting the quantities of the coins

Last edited by joe12286; 10-11-2009 at 09:48 PM.
10-11-2009 , 10:17 PM
Quote:
Originally Posted by joe12286
also, i don't think you are accounting for the coins being worth 0.25, 0.10, etc. it looks like you are just subtracting the quantities of the coins
When I wrote the code using static_cast this is what I pictured the math to look like:

200.61 = 200 (because static_cast eliminates the .61)
200 dollars
200.61 - 200 = .61

.61*4=2.44 (again static_cast eliminates the .44)
2 quarters
.61-.50 = .11

.11*10=1.1 (static_cast eliminates the .1)
1 Dime
.11-.10=.01

.01*20=.2 (static_cast makes this 0)
0 nickels
.01-0=.01

.01*100=1 (static_cast keeps this 1)
1 penny
.01-.01=0


I have a new remainingMoney variable each calculation. So I am guessing I need a new money variable every calculation as well. Would that look something like this?

change makeChange (float money, float money1, etc.......)
10-11-2009 , 10:33 PM
another coding thread. i already responded to one today. but this is fun. maybe i shoulda stayed in school.

your picture of the math looks good. seems like it should work. your coding of it doesnt match your picture. i thought joe was pretty clear with what he said. you should be able to figure it out from that alone. why do you have a new remainingMoney variable for each calc? and from what joe said, how did you conclude that you need a new money variable as well? answer those before reading further.

lets look how your math matches the code:

Quote:
200.61 = 200 (because static_cast eliminates the .61)
200 dollars
200.61 - 200 = .61

makeChange.dollars = static_cast<int>(money);
float remainingMoney = money - makeChange.dollars;
so
makeChange.dollars = 200
remainingMoney = 200.61 - 200 = 0.61, looks ok so far..


Quote:
.61*4=2.44 (again static_cast eliminates the .44)
2 quarters
.61-.50 = .11

makeChange.quarters = static_cast <int> (remainingMoney*4);
float remainingMoney1 = money - makeChange.quarters;
so
makeChange.quarters = 2
remainingMoney1 = 200.61 - 2 = 198.61, not ok..
10-11-2009 , 11:43 PM
You might also take a look at the mod operator.
10-12-2009 , 01:58 AM
Solved!!!!!!
Thanks for the help without making me ship $$ on Stars first.
Closed Thread Subscribe
...

      
m