Converting data types

This is a discussion on Converting data types within the C++ Programming forums, part of the General Programming Boards category; I am trying to convert a float to an int. I'm making a change calculator where the user enters an ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    23

    Converting data types

    I am trying to convert a float to an int. I'm making a change calculator where the user enters an amount. I didn't want the result to be off because I've had results from floats come back 1 away from the correct answer. So this is what I have so far.

    Code:
    /*
      FILE:         Change_Calculator.cpp
      PROGRAMMER:   Stephen Michael Croy
      DATE:         01-07-09
      LAB:          1
      
      This program calculates the change from an amount input by the user
    */
      
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
        //the following are the constants for this program
        const int HUNDRED   = 10000;
        const int FIFTY     = 5000;
        const int TWENTY    = 2000;
        const int TEN       = 1000;
        const int FIVE      = 500;
        const int ONE       = 100;
        const int QUARTER   = 25;
        const int DIME      = 10;
        const int NICKEL    = 5;
        const int PENNY     = 1;
        
        //entered by user
        float amount;
        float payment; 
        int intAmount;
        int intPayment;
        int total;
        
        cout << "Enter the amount of purchase: "; //Prompt for data
        cin >> amount; //read information from user into amount
        
        cout << "Enter payment toward purchase: ";
        cin >> payment; //read information from user into payment
        
        intAmount = amount * 100;
        intPayment = payment * 100;
        
        if (intAmount > intPayment)
           total = intAmount - intPayment;
           cout << total;
           
        
        
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }

    I know the program isn't finished, I'm just hung up on this conversion.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    So if you multiplied the amount and payment by 100, then you must divide the total by 100.
    Also check out alternatives to system("pause"): http://apps.sourceforge.net/mediawik...=Pause_console
    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.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    In order to avoid issues with truncation, a quick fix would be

    Code:
        intAmount = amount * 100 + 0.5;
        intPayment = payment * 100 + 0.5;
    The reason is that a float * 100 may end up just a tiny bit below the actual value due to approximate nature of floating point values, e.g 0.11 * 100 = 10.999996 and if you truncate it to int it will become 10.

    However, I think normally you should just avoid floating point values where the approximation errors are unacceptable. E.g take the input as a string, parse it to two integers, multiply the first part by 100 and add the second part as needed (avoid errors where user enters 10.5 or 2.345).

    Code:
        const int HUNDRED   = 10000;
        const int FIFTY     = 5000;
        const int TWENTY    = 2000;
        const int TEN       = 1000;
        const int FIVE      = 500;
        const int ONE       = 100;
        const int QUARTER   = 25;
        const int DIME      = 10;
        const int NICKEL    = 5;
        const int PENNY     = 1;
    When you continue you'll probably find that this will be better off as an array (perhaps even a map of numeric values and string names).
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    23
    Thank you for the suggestions. Although they were helpful, they weren't exactly what I was looking for. I just want to know how to convert the float into an int.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,918
    But you have been given suggestions on how to do it...
    Floats are not exact either, so there may be precision errors when converting to/from floats.
    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.

  6. #6
    Or working on it anyways mramazing's Avatar
    Join Date
    Dec 2005
    Location
    Lehi, UT
    Posts
    121
    Elysia is right. There are a few ways to do it but the results may not be exactly what you want. You can use bit the bit-wise operators to get values. There is a lot of info on the net about that. Other than that you can:

    float x = 5.2;
    static_cast<int>(x);

    that will just give you 5 because it is going to drop everything after the decimal. But it will convert the float into an int.
    -- Will you show me how to c++?

  7. #7
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    There are ways to store money as integers by storing the values as BCDs.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extending basic data types.
    By nempo in forum C++ Programming
    Replies: 23
    Last Post: 09-25-2007, 04:28 PM
  2. What are abstract data types
    By bhagwat_maimt in forum C++ Programming
    Replies: 4
    Last Post: 01-04-2007, 10:43 AM
  3. Replies: 16
    Last Post: 10-29-2006, 05:04 AM
  4. Replies: 4
    Last Post: 06-14-2005, 06:45 AM
  5. Using enumerated data types
    By SXO in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2001, 07:26 PM

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