Thread: Why aren't my answers having decimal places

  1. #1
    Registered User
    Join Date
    Mar 2010
    Location
    MD
    Posts
    2

    Why aren't my answers having decimal places

    Hi all,

    I am doing the Temperture Converter problem on this site: Temperature Converter Programming Challenge - Cprogramming.com

    I basically did a quick program and used the example numbers giving on the above site (10 for low, 20 high, 4 for the step). I am getting the "right answers" but they are intergers and have no decimal places but 2 of the answers should have decimal places. I declared the varibles with double and tried with float but I am getting the same answer. When I input (10.2 for low, 20 for high, 4 for step) all the answers come out with a .2 at the end so I know it can print decimal places.

    Am I missing something here? like do I have to declare how many decimal places somewhere?

    Here is my program below.

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    void Celsius_to_Fahrenheit(double celsius_low, double celsius_high, int step); 
    
    int main()
    {
        double low_limit, high_limit; 
        int increments;
        char exit;
        
        cout<<"Enter low Celcius  ";
        cin>>low_limit;
        cout<<"\nEnter high Celcius   ";
        cin>>high_limit;
        cout<<"\nEnter the ingrements you would like it to calculate by  ";
        cin>>increments;
        
        
        cout<<"\n\nCelsius          Fahrenheit\n";
        cout<<"_______          __________\n";
        
        Celsius_to_Fahrenheit(low_limit, high_limit, increments);
        
        cout<<"Press any key then enter to exit";
        cin>>exit;
        return 0;
    }
    
    void Celsius_to_Fahrenheit(double celsius_low, double celsius_high, int step)
    {
        
        double Fahrenheit;
     
         while(celsius_high >= celsius_low)     
         {
               Fahrenheit = 9/5 * (celsius_low + 32); 
               
               cout<<celsius_low<<"                "<<Fahrenheit<<"\n";
               
               celsius_low = celsius_low + step;
                                               
         }
         
         
    }

  2. #2
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Code:
    Fahrenheit = 9/5 * (celsius_low + 32);
    Your problem is the red part in this line..
    You'd think 9/5 would be 1.8, but because you are using integers the decimal part gets thrown away and the line above becomes equal to
    Code:
    Fahrenheit = 1 * (celsius_low + 32);
    You could use 9.0/5.0 instead, or just 1.8

    Edit:
    Btw, you are using the wrong formula for the conversion
    Remove the ( ) and with the above fix it should give you the correct answers.
    Last edited by _Mike; 03-13-2010 at 07:25 PM.

  3. #3
    Registered User
    Join Date
    Mar 2010
    Location
    MD
    Posts
    2
    Thanks!!! It worked!!

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    7
    This is a bit off-topic, but I had the exact same problem while trying to complete the same exercise.

  5. #5
    Registered User UltraKing227's Avatar
    Join Date
    Jan 2010
    Location
    USA, New york
    Posts
    123
    definitely off-topic.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Decimal places
    By Gordon in forum Windows Programming
    Replies: 9
    Last Post: 06-08-2008, 09:04 AM
  2. %g doesn't print as many decimal places as %f
    By octoc in forum C Programming
    Replies: 1
    Last Post: 03-31-2008, 12:16 PM
  3. Decimal places
    By Gordon in forum Windows Programming
    Replies: 4
    Last Post: 09-28-2007, 10:03 AM
  4. Decimal places on Floating point number
    By manutdfan in forum C Programming
    Replies: 1
    Last Post: 10-29-2006, 12:56 PM
  5. Replies: 10
    Last Post: 06-12-2002, 03:15 PM