# Thread: Money Counter Program

1. ## Money Counter Program

Hey,

My homework assignment for my C++ class was to write a program that can find the minimum amount of coins necessary for change for a purchase.

Here's the code I came up with:
Code:
```#include <iostream>
#include <string>

using namespace std;

void calculate ( double change, int * coins );
void getValues ( double &change );

int main ( int argc, char * argv[] )
{
string coinTypes[6] = { "Dollars", "Half-Dollars", "Quarters", "Dimes", "Nickels", "Pennies" };
int coinValues[6] = { 0, 0, 0, 0, 0, 0 };
double change = 0.0;

getValues ( change );
calculate ( change, coinValues );

for ( int i = 0; i < 6; i++ )
{
cout << coinTypes[i] << " = " << coinValues[i] << endl;
}

system("pause");
return 0;
}

//
//Function to get the values from the user
//
void getValues ( double &change )
{
double total, paid;

cout << "Enter total cost: ";
cin >> total;
cout << "Enter amount paid: ";
cin >> paid;

change = static_cast<double>(paid - total);
}

//
//function to calculate how many coins are needed
//
void calculate ( double change, int * coins )
{
double div[] = { 1, .5, .25, .1, .05, .01 };
double result;

//Figire out the number of coins needed;
for ( int i = 0; i < 6; i++ )
{
result = static_cast<double>( change / div[i] ); //see how many of the coin value go into the change amount
change -= static_cast<double>( (int)result * div[i] ); //subtract the value of the coin * the amount of it from the change
coins[i] = static_cast<int>( result ); //take the number of coins and store in the necessary variable
}
}```

Here's the program's output
Code:
```Enter total cost: 56.78
Enter amount paid: 100
Dollars = 43
Half-Dollars = 0
Quarters = 0
Dimes = 2
Nickels = 0
Pennies = 1
Press any key to continue . . .```
Everything is good except there should be 2 pennies, not 1...

I think there's some sort of precision error or something in the numbers.

Any help would be greatly appreciated.

Thanks,

Matt N

2. If I add the amount owed to the change, I get 99.99 which is fairly close to 100. I think this inaccuracy has something to do with 0.(9) = 1. In other words, the result it gave you is as close to 100 as can be represented by floating point.

So if you want to be exact with the till all the time, calculate in pennies using whole number types. You would still be dividing by denominations like you have in the original algorithm so it isn't that different.

3. Code:
`result = static_cast<double>( change / div[i] )`
since div[i] and change are already double variables.

In fact, the only cast you need in the whole program is this one:
Code:
`coins[i] = static_cast<int>( result )`

Edit: Of course, this is quite pointless, as the right solution is to count money in an integer value as pennies.

--
Mats

4. void calculate ( double change, int * coins );
void getValues ( double &change );
Did your teacher show you to do it like that (the text in red)?

5. Originally Posted by Elysia
Did your teacher show you to do it like that (the text in red)?
No, but my teacher likes us using pass-by-reference variables (the double& part). I did the int * part (extra credit for using pointers/arrays, since we just started doing basic functions). I know int * coins is a pretty cheesy way of doing this (asking for some sort of a buffer overflow error) but for this program it isn't too dangerous.

I'm just confused why i don't get 2 pennies instead of 1... =\

6. I mean the way you place it.
double &var is what you wrote, not double& var or double & var.
And then you write int * var, not int* var or int *var.
Two different styles, even.

7. I dunno.

I've always used the & sign directly next to a variable like double &abc, or if it's just a declaration, i do double&. For pointers it would be like double * var, or for a declaration double*.

Is that bad or something?

8. Well, no.
But every C++ programmer chooses a style based upon his or her preferences.
Either the * or & binds to the name (typical C) or to the type (typical C++) or in the middle.
I just want you to be aware that there are 3 different styles and that you should choose one for yourself (not pick up one because your teacher said so).
Some people say it's good to be consistent and in some situations and jobs, you will have to be, so it might also be a good idea to stick to one style.
I like to put it next to the type to emphasis that it's part of the type (because int* var and float* var aren't the same type - something that int *var and float *var doesn't give the impression of).

As I've stated before, the change plus the amount owed comes to 99.99, a precision error on the part of the computer. I recommend reading about rounding errors and 0.(9) being equal to 1, both of which may be playing a role here in your program. Rounding errors occur because the range of a floating point must be expressed in a finite number of bits, so approximations of numbers like 100 or 0.2 or 0.1 will appear, to the detriment of your calculations. Most real-world financial software uses whole number math, in small denominations like pennies, to represent currency at some stage for these reasons.

So, you have my suggestion -- use integer math to get exact change every time. If you cannot do that (assignment restrictions?) then I recommend a comment stating what you have learned about the problem and your teacher will likely give you credit.

10. Originally Posted by citizen
At the very least, I feel it is important that everyone knows about style and do not blindly follow what the teacher uses or says.
Besides, an answer to the problem was given, no?

11. >> do not blindly follow what the teacher uses or says.

What in blue blazes gave you that impression?

12. Just checking.
Many people just blindly do that. I've witnessed it before.

13. So... if I take the input as a string, find the '.' character and split everything from the left to an int called dollars, and the right to another int called change, it would work better? Since I'm only doing integer division...

I never fully understood what the answer was in the first place. How can you store a number like 56.78 in an int...

14. >> Just checking.

No I mean, really, what gave you that impression? You just started talking about it, and his code didn't have readability problems. You just assumed his teacher told him how to write it like that and reacted.

>> How can you store a number like 56.78 in an int
Convert the dollar amount into cents by multiplying 100. The result can then be truncated to a long.

15. Originally Posted by citizen
No I mean, really, what gave you that impression? You just started talking about it, and his code didn't have readability problems. You just assumed his teacher told him how to write it like that and reacted.
Because
1) OP was using double &var
2) OP was using int * var

Two different styles, plus using the 1st, which according to some, should be uncommon in C++.

Popular pages Recent additions