-
Need some help
I am learning C++, working through the books recommended by this website. I'm trying to code Exercise 6-3 of Practical C++ Programming. Here's the problem:
Given an amount less than $1.00, compute the number of quarters, dimes, nickels, and pennies needed.
Here's my code:
Code:
#include <iostream>
int main(){
float money; // amount of money
int quarters, dimes, nickels, pennies; // Stores the amount of each coin needed.
while (true){
std::cout<< "Enter a non-negative amount of money " // Prompt user for money
<< "less than $1.00, or -1 to stop: "; // amount
std::cin >> money;
if (money == -1) // Loop until user enters -1
break;
if (money < -1 || money >= 1.0) // Start loop over if money
continue; // amount is invalid.
std::cout << "$" << money << " in change is ";
quarters = (money / 0.25); // Stores the amount of quarters needed, then subtracts
money -= (quarters * 0.25); // that amount in cents from the money total.
dimes = (money / 0.1); // Stores the amount of dimes needed, then subtracts
money -= (dimes * 0.1); // that amount in cents from the money total.
nickels = (money / 0.05); // Stores the amount of nickels needed, then subtracts
money -= (nickels * 0.05); // that amount in cents from the money total.
pennies = (money * 100); // Money is in cents, so it is multiplied by 100 to convert
// it to the amount of pennies needed.
std::cout << quarters << " quarters, " << dimes << " dimes, "
<< nickels << " nickels, " << " and " << pennies << " pennies.\n";
}
return 0;
}
For some reason, it's not calculating the right amount. For example, if I run the program and put in 0.35, when it gets to this part Code:
dimes = (money / 0.1)
, I know money is equal to 0.1 at that point, but dimes is not being set equal to 1. I put in Code:
std::cout << money;
just to make sure that money equals 0.1 at that point, which it does. I also tried Code:
std::cout << (money / 0.1)
to see that money, when it is 0.1, divided by 0.1 does indeed equal 1, which it does. But it won't set dimes equal to 1. If I run the program at 0.36, it calculates everything right. What am I doing wrong?
-
It has to do with the fact that computers cant handle floating point ("real numbers") perfectly. To see this, do: Code:
#include <iostream>
int main(void)
{
float foo = 0.1;
if ( foo == 0.1)
std::cout << "you would think its equal" << std::endl;
else
std::cout << "but its not equal" << std::endl;
}
Since "money" can be considered a discrete quantity, you could just work with pennies, as "ints", and after you get the money as a float just multiply it by 100, so you only work with pennies (discrete quantities). Later just divide by 100. This will ensure correct comparisons, since your working with ints.
-
Ok, I tried working with pennies. I did this after getting money as a float from user: Code:
pennies = (money * 100); // stores money as pennies
It stores the wrong amount sometimes. If I put in 0.35, it stores 34 in pennies. Some numbers work, such as 0.36 or 0.37 (0.38 stores as 37).
-
So if you do Code:
float f = 0.35;
int i = 100*f;
cout << i;
it prints "34"? Or even by doing "cin >> f" instead of hardcoding it, you get 34?
EDIT: Compilers are probably a factor too, what are you using?
-
Yeah. Using exactly this code: Code:
#include <iostream>
int main(){
float f;
int i;
f = 0.35;
i = f * 100;
std::cout << i << std::endl;
return 0;
}
I got this:
34
Press any key to continue
I'm using Microsoft Visual Studio .NET 2003.
-
Try forcing an int by casting i.e.:I get 34 when its interpreted as a float, i.e.:and forcing it to be an int I get 35.
-
I tried that, and it still didn't work. However, I was getting the warning "truncation from double to float" on this lineI tried changing it to a double, and it works now. Given that the double f equals 0.35, (f * 100) stored into an integer gives me 35.
-
if you do "f = (float) 0.35;", and leave f as a float, do you later get integer 35?
-
No, it's still 34. I tried this though Code:
#include <iostream>
int main(){
float f;
int i;
f = 0.35;
f *= 100;
i = f;
std::cout << i << std::endl;
return 0;
}
This prints out 35.
-
Prefer double over float.
To create a float, you must do x.yf. Notice the "f" at the end. That will make the compiler treat the number as a float rather than a double.
Nevertheless, I would still urge you to use integer math.