-
September 14th, 2010, 01:56 PM
#1
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;
}
-
September 14th, 2010, 02:23 PM
#2
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.
-
September 14th, 2010, 03:55 PM
#3
Re: Im losing a .01 somewhere
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().
Old Unix programmers never die, they just mv to /dev/null
-
September 14th, 2010, 04:01 PM
#4
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.
-
September 14th, 2010, 06:40 PM
#5
Re: Im losing a .01 somewhere
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
-
September 15th, 2010, 02:11 AM
#6
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.
Is your question related to IO?
Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.
-
September 15th, 2010, 02:50 AM
#7
Re: Im losing a .01 somewhere
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|