CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Sep 2010
    Posts
    6

    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;
    }

  2. #2
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  3. #3
    Join Date
    Apr 2004
    Location
    Canada
    Posts
    1,342

    Re: Im losing a .01 somewhere

    Quote Originally Posted by Lindley View Post
    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

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Im losing a .01 somewhere

    Quote Originally Posted by cloudzy View Post
    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

  6. #6
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    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.

  7. #7
    Join Date
    Sep 2010
    Posts
    6

    Re: Im losing a .01 somewhere

    Quote Originally Posted by Lindley View Post
    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
  •  





Click Here to Expand Forum to Full Width

Featured