Like Tree1Likes
  • 1 Post By Salem

weird double/int question (very short code)

This is a discussion on weird double/int question (very short code) within the C++ Programming forums, part of the General Programming Boards category; Hi~~~ I'm new to the forums (and C++), and hope someone can help me with a very basic program. I ...

  1. #1
    Registered User
    Join Date
    May 2011
    Posts
    13

    weird double/int question (very short code)

    Hi~~~
    I'm new to the forums (and C++), and hope someone can help me with a very basic program. I apologize if any etiquette has been violated.

    The following is a very basic money sorting programs that works*, in which a given amount is broken up to its maximum amount of dollars, quarters, etc. (I left out nickels and pennies to shorten this post).

    Code:
    /*Money Sorter*/
    #include "iostream"
    #include "cmath"
    using namespace std;
    
    int main()
    {
        double amount;
        cout<<"enter your amount in wallet"<<endl;
        cin>>amount;
        int dollars, quarters, dimes, nickels, pennies;
    
    
        dollars=int(amount);                    //represents amount in full dollars
        quarters=(int)((amount-dollars)/.25);    //leftover quarters
        cout<<dollars<<" "<<quarters<<endl;
       
     
        dimes=(int)((amount-dollars-quarters*.25) /.1 );   //dimes left
        cout<<dimes<<endl;  
     
       system("pause");
       return 0;
     
    }

    *So I believe this program is good with one exception. When I typed in $15.20, it gives you 15 dollars, 0 quarters, and 1 dime. I'm not sure why or how I can fix it to be 2. My best guess is that quarter is ultimately a double, which messes up the intent of the italicized portion.

    To be honest, I'd rather just move on and learn other things, but I thought this was weird enough for me to sign on to the forums and ask for an opinion.

    Cheers!

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,555
    Floating point - Wikipedia, the free encyclopedia
    The first thing you need to know is that floats are approximations of true values.
    It also makes floats useless as a data type for money.

    So whilst you might assign 15.20, it might even print 15.20, but mathematically, you have the nearest approximate value of 15.19999999.

    Normally, the change giving program is best done in integer arithmetic, where you start with 1520 cents (for example).
    CommonTater likes this.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    Try putting a +.5 before casting to an int. The thing with casting to integers is that the rounding is weird. Instead of rounding up for certain values, it conditionally rounds down. ie (int)1.9 == 1

    Code:
    quarters=(int)((amount-dollars)/.25 + .5);    //leftover quarters
    dimes=(int)((amount-dollars-quarters*.25) /.1 + .5);   //dimes left

  4. #4
    Registered User
    Join Date
    May 2011
    Posts
    13
    thank you guys!

    yes, (int) does act like (floor) I think; ah, the +.5 might be a good trick or just using integers will be more practical;

    I will try to redo as best as I can ^^
    Last edited by Minty; 05-13-2011 at 06:59 AM.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Minty View Post
    thank you guys!

    yes, (int) does act like (floor) I think; ah, the +.5 might be a good trick

    I will try to redo as best as I can ^^
    I don't think so. Casting rounds down to zero whereas floor rounds down. Meaning the two do the same for positive numbers, but the opposites for negative numbers.

  6. #6
    Registered User
    Join Date
    May 2011
    Posts
    13
    thanks EVOEx

    yeah, I'm still having trouble with redoing the casting.... oh boy ;/

    I'm just gonna move on since I can't seem to untangle the double from the int, even when I try to set all variables to be integers;

    there are other C++ money sorter programs that rely on while statements, etc., so maybe that would be more appropriate
    Last edited by Minty; 05-13-2011 at 08:00 AM.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    Floating point - Wikipedia, the free encyclopedia
    The first thing you need to know is that floats are approximations of true values.
    It also makes floats useless as a data type for money.

    So whilst you might assign 15.20, it might even print 15.20, but mathematically, you have the nearest approximate value of 15.19999999.

    Normally, the change giving program is best done in integer arithmetic, where you start with 1520 cents (for example).
    Yes! When working with money always work in integer pennies...
    Whole accounting packages are written that way for good reason.

    Floating point math is always an approximation of the actual value. Integers don't guess.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,622
    Casting to int does not round; it truncates (is throws away the bits containing the information of the decimals, regardless of what value they are).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. single swap for char,short,int,float,double
    By Jackie Chan in forum C Programming
    Replies: 15
    Last Post: 05-24-2008, 07:46 AM
  2. [Weird Question] Where to break a line in C++ code?
    By meili100 in forum C++ Programming
    Replies: 13
    Last Post: 11-22-2007, 01:17 PM
  3. Replace double with long double in all my code.
    By boyfarrell in forum C Programming
    Replies: 8
    Last Post: 04-30-2007, 04:17 PM
  4. Rounding error when casting double to short
    By thetinman in forum C Programming
    Replies: 7
    Last Post: 10-25-2006, 12:48 PM
  5. Replies: 1
    Last Post: 01-23-2002, 02:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21