Im losing a .01 somewhere
Hi this program is for homework and it converts dollars and change into there respective values
but if i enter .02 i get .01 I can not see where its getting lost.
#include <iostream.h>
#include <stdlib.h>
int main()
{
float m;
int a , d , rd , h , rh , q , rq , di , rdi , n , p;
cout << "enter amount to change" << endl;
cin >> m;
a = static_cast<int> (m*100);
d = a/100;
rd = a%100;
h = rd/50;
rh = rd%50;
q = rh/25;
rq = rh%25;
di = rq/10;
rdi = rq%10;
n = rdi/5;
p = rdi%5;
cout << "Dollars " << d << "\nHalf dollars " << h << "\nQuarters " << q <<
"\nDimes " << di << "\nNickles " << n << "\nPennies " << p << endl;
system("pause");
return 0;
}
Re: Im losing a .01 somewhere
You type "0.02" on the command line. Unfortunately, you can't assume that 0.02 is stored in m, because in fact floating point types cannot exactly store this number since it isn't a sum of powers of 2 or 1/2. It's probably storing 0.019999999999 or something like that.
Therefore, when you compute a, you need to be a bit more intelligent about your rounding. Instead of simply casting to int, which always rounds towards 0, you should instead round to the nearest integer.
For positive numbers, this is done as
Code:
int roundpos(double d)
{
return static_cast<int>(d + 0.5);
}
If you want to be able to handle negatives, then you need a signum function (returns -1, 0, or 1 depending on the sign) so that you can subtract 0.5 from negatives and add 0.5 to positives.
Re: Im losing a .01 somewhere
Quote:
Originally Posted by
Lindley
For positive numbers, this is done as
Code:
int roundpos(double d)
{
return static_cast<int>(d + 0.5);
}
If you want to be able to handle negatives, then you need a signum function (returns -1, 0, or 1 depending on the sign) so that you can subtract 0.5 from negatives and add 0.5 to positives.
Or you could use the standard library function nearbyint().
Re: Im losing a .01 somewhere
Interesting. That appears to be in the C99 standard, but not the C++ standard. At any rate, Visual Studio does not appear to support it.
Re: Im losing a .01 somewhere
Quote:
Originally Posted by
cloudzy
Hi this program is for homework
....
#include <iostream.h>
What book or teacher shows you <iostream.h>? The correct standard header is <iostream>, not <iostream.h>.
Any book or teacher showing you <iostream.h> is showing you outdated C++ code which no longer can compile under many compilers. You should then consider getting a modern, updated book, or another teacher.
Here is a "Hello World" program in C++ written using the correct headers:
Code:
#include <iostream>
int main()
{
std::cout << "Hello World";
}
Regards,
Paul McKenzie
Re: Im losing a .01 somewhere
You should know that in most countries, it is actually illegal to write accounting programs that make use of the floating type. Your example is a perfect example of why.
decimal floats may change this, but for know, stick to storing money inside an int representing a defined unit (like pennies). I think there are some libraries out there that even enforce coherent dimension (for example, allows summing dollars and pennies, and the result is an amount of pennies), but I would not be knowledgeable enough to recommend anything.
Re: Im losing a .01 somewhere
Quote:
Originally Posted by
Lindley
You type "0.02" on the command line. Unfortunately, you can't assume that 0.02 is stored in m, because in fact floating point types cannot
exactly store this number since it isn't a sum of powers of 2 or 1/2. It's probably storing 0.019999999999 or something like that.
Therefore, when you compute a, you need to be a bit more intelligent about your rounding. Instead of simply casting to int, which always rounds towards 0, you should instead round to the
nearest integer.
For positive numbers, this is done as
Code:
int roundpos(double d)
{
return static_cast<int>(d + 0.5);
}
If you want to be able to handle negatives, then you need a signum function (returns -1, 0, or 1 depending on the sign) so that you can subtract 0.5 from negatives and add 0.5 to positives.
that worked thanks alot