Wierd thing in my program or did I do something wrong?

Hello everyone. I managed to complete my program where it tells you how much of each kind of coin you have in a given amount of money. However, theres a problem. When any value between $2.80 to $7.80 is given it gives the wrong amount of nickels, and in turn, the wrong amount of pennies. Yet when given any other value, it seems to work fine. I have looked over my code several times and noticed that where it is taking the remaining value of .05 (if the given value is 7.80) and dividing that by the value of a nickel, .05, it comes out to 0 instead of 1. I cant seem to figure out why that is. Here is my code:

#include <iostream>

using namespace std;

int main()

{

cout << "Hello there, I can tell you how much of each kind of currency is in a given \n";
cout << "amount of money. Please enter the amount now: \n";

double given_value;
cin >> given_value;
cout << "Your entered value was: " << given_value << "\n\n";

const double quarter_value = 0.25;
int amount_of_quarters = (given_value / quarter_value);
cout << amount_of_quarters << endl;

double remaining_value = given_value - (amount_of_quarters * quarter_value);

const double dime_value = 0.10;
int amount_of_dimes = (remaining_value / dime_value);
cout << amount_of_dimes << endl;

remaining_value = remaining_value - (amount_of_dimes * dime_value);

const double nickel_value = 0.05;
int amount_of_nickels = remaining_value / nickel_value; area with trouble
cout << amount_of_nickels << endl;

remaining_value = remaining_value - (amount_of_nickels * nickel_value);

const double penny_value = .01;
int amount_of_pennies = remaining_value / penny_value;
cout << amount_of_pennies << endl;


return 0;

}

What it is suppose to be doing:
1) given value = 7.80, so take 7.80 and divide that by .25 to find out how many quarters are in there. That would be 31.2 quarters. However, I want whole amounts of quarters, so I use the "int" number type so that it rounds down to 31.
2) Take that 31 quarters and multiply it by .25 to get $2.75. Take the given value of 7.80 and subtract that 2.75 to get .05, which is the remaining value.
3) Take that remaining value and divide by that by .10 to see how many dimes are in there. The answer is .50, however, again I want a whole number, so I set it to int number type so that it rounds down to 0. So it will give out 0 amount of dimes.
4) Take 0 amount of dimes and multiply it with .10, which is 0, and subtract that 0 from the remaining value of .05 which is still .05.
5) Take .05 and divide by .05 to find out how many nickels are in there. Now here is the problem. It should be 1, but it keeps coming out as 0, every time the given value is between 2.80 to 7.80.

This in turn messes up the pennies. Thus, can anyone help me figure out what I did wrong? -Thanks, and I'm sorry if my explanations were not very clear.
Hi,

at the line:
int amount_of_nickels = remaining_value/nickel_value

The compiler reads this as 0 divide by 0 because both remaining_value is converted to int value first before it is operated.

Hence, if you replaced int with double:

1
2
double amount_of_nickels = remaining_value/nickel_value;
cout<<(int) amount_of_nickels<<endl;


Having "(int) amount_of_nickels" is to print out the value in int instead of double.
If you want to make amount_if_nickels an integer, probably, you create another variable that is integer and pass the value of amount_of_nickels to the new integer.

int amount_of_nic = amount_of_nickels;

=)
Last edited on
No... since at least one of (remaining_value, nickel_value) is a floating point number, the division is performed using floating point. Because the left-hand side of the assignment is an integer, the resulting decimal is converted to an int.

The real problem is that OP should be performing all math in the integer domain, not floating point domain, since floating point math is not 100% precise.



Replace floating point representations of change, and replace it with integer, so that
1
2
3
int penny = 1;
int nickel = 5;
int dollar = 100;

I tried what Iwtan90 said with the
cout << (int) amount_of_nickels << endl;
and it came out this time where nickels are still 0, and now pennies are at 0 given that the value is 7.80, which still doesn't add up. However, if i do:

1
2
double amount_of_nickels = remaining_value/nickel_value;
cout<< amount_of_nickels<<endl;
. I end up getting the 1 that I am looking for and everything works out, but this wont work for any other inputed money value from the user,

Also, with what jsmith said about :

Because the left-hand side of the assignment is an integer, the resulting decimal is converted to an int.


I want it to come out as an int, but its not coming out as 1 :(, it keeps going down to 0 and I dont understand why, whenever I enter any value between 2.80-7.80. I tried to do:

1
2
int x = 5.0 / 5.0
cout << x;


separately and it comes out as 1, yet it wont go inside my program. Thank you for your input and time though. I hope my reply further clarifies my problem.
You should do like jsmith suggested and work in the integer domain.

I tried your program and entered 3.01. I should have gotten 12 quarters and 1 penny but the answer was only 12 quarters. Why? Well I stepped through with my debugger and C++ was storing 3.01 as 3.0099999999999998. That inaccuracy accounts for the lost penny.

So, what you should do is, if the user enters, say 4.27, store this as the integer value 427 (i.e. 427 pennies) and work with the integer values 1 (penny), 5 (nickel), 10 (dime), and 25 (quarter).
Ohhh ok. That explains alot! Thanks for getting in there with your debugger and finding out.
Topic archived. No new replies allowed.