converting double to long int

This is a discussion on converting double to long int within the C++ Programming forums, part of the General Programming Boards category; I'm trying to convert a double to a long int, but i keep getting a warning message saying : 34 ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    12

    converting double to long int

    I'm trying to convert a double to a long int, but i keep getting a warning message saying :

    34 C:\Dev-Cpp\PROG1B.cpp [Warning] converting to `long int' from `double'

    How do i ignore the warning so i can compile it?

    Here's how i started setting up the code:

    Code:
    #include <iostream>
    #include <math.h>
    #include <cmath>
    
    using namespace std;
    
    int main()
    {
    
    //Declaring Variables
    
    double tendered;
    double purchase;
    double chg;
    long change;
    int fives;
    int ones;
    int half;
    int quarters;
    int dimes;
    int nickels;
    int pennies;
        
        cout << "Enter purchase amount: ";
        cin >> purchase;
        
        cout << "Enter tendered amount: ";
        cin >> tendered;
        
        //Calculating change amount
        chg = tendered - purchase;
        
        //Converting double to long 
        change = ceil(chg * 100);
        
        
        //Display error message if change is more than 19.99
        
        If (chg > 19.99)
           cout << "Amount to return is: " << chg << "--Sorry just dont have that amount of change";
        getch();
        return 0;
        
        fives = change / 5;
        ones = (change - 5) / 1;
        
        cout << "Fives" << fives; 
    
    return 0;
    }

  2. #2
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    typecast it.....long(expression);

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Quote Originally Posted by gunshipolitico
    How do i ignore the warning so i can compile it?
    How do you ignore a warning? You ignore it by... ignoring it. A warning does not hinder the compilation of code to object code, it's the errors you gotta watch out for. For the most part however you should pay attention to the warnings your compiler spits out and do your best to understand them and the effect they may have on the program.

    Code:
    #include <math.h>
    #include <cmath>
    You only need one of those, lose the first, keep the second.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    12
    ok. I have an error that I think is the main problem:

    38 C:\Dev-Cpp\PROG1B.cpp `If' undeclared (first use this function)
    (Each undeclared identifier is reported only once for each function it appears in.)

    I thought that i did the if statement correctly, but apparently not.

    Code:
    #include <iostream>
    #include <cmath>
    
    using namespace std;
    
    int main()
    {
    
    //Declaring Variables
    
    double tendered;
    double purchase;
    double chg;
    long change;
    int fives;
    int ones;
    int half;
    int quarters;
    int dimes;
    int nickels;
    int pennies;
        
        cout << "Enter purchase amount: ";
        cin >> purchase;
        
        cout << "Enter tendered amount: ";
        cin >> tendered;
        
        //Calculating change amount
        chg = tendered - purchase;
        
        //Converting double to long 
        change = ceil(chg * 100);
        
        
        //Display error message if change is more than 19.99
        
        If (change > 1999.00)
           cout << "Amount to return is: " ;
           cout << chg << " --Sorry, I dont have that amount of change" << endl;
    
        //change = ceil(chg*100);            
        
        fives = change / 500;
            
        cout << "Fives" << fives; 
    
    return 0;
    }

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    Remember C++ is a case-sensitive language. If and if are two completely different things.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Nov 2005
    Posts
    12
    Quote Originally Posted by hk_mp5kpdw
    Remember C++ is a case-sensitive language. If and if are two completely different things.

    oops....well the fixed everything. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Copying 2-d arrays
    By Holtzy in forum C++ Programming
    Replies: 11
    Last Post: 03-14-2008, 03:44 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  4. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 04:26 PM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 09:48 AM

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