Hi all,

I'm a beginner and I tried to write a program to convert celsius into Fahrenheit but i have a problem due to type casting operation.

That is even though i explicitly converted an integer to float number I'm getting only integer value .

Here's the program :

# include<iostream>
using namespace std;

int main()
{
    float celsius;
    cout<<"Enter the temperature in celsius: ";
    cin>>celsius;
    float fahrenheit =(celsius*static_cast<float>(9/5))+32;
    cout<<"Temperature in Fahrenheit = "<<fahrenheit<<endl;
    return 0;
}

Please tell me what is wrong with that code with explanation .
Thanks in advance.

Recommended Answers

All 8 Replies

The cast is in the wrong place. The sub-expression 9/5 has already happened, and produced an integer result, before the cast happens.

Why not just remove it and type celsius * 9.0f / 5.0f + 32.0f;

9/5 is an integer expression (==1). use
float fahrenheit = celsius * float(9)/5 + 32 ;
it would be better to use doubles instead of floats unless there is an externally imposed constraint.
salem, didn't see your post!

Thanks Salem and Vijay . I now understood .

9/5 is an integer expression (==1).

Hi vijay, one more question as you said that expression yields integer " 1" but I'm casting that " 1" to float (I'm not sure just I'm thinking) as
celsius*static_cast<float>(9/5).

so it now becomes float (I think) .

now float * float+32 = float .

What i expected is the result should be a floating point number but why it gives integer number ?

Sample interaction :

Enter the temperature in celsius: 32
Temperature in Fahrenheit = 64

what i expect is 64.000000 .

Please tell me actually what is happening .

what about this

float fahrenheit =(celsius*(9.0/5.0))+32;

no need to type cast

The result does not have decimals because 9 and 5 are both integers and 9/5 returns an integer. you are typcasting the result of the division which is 1 to a float. Read Salem's example for the correct way to do what you are attempting, unless you want to get really brillent and appear to be super-smart and do something like this: :cool:

celsius*static_cast<float>(static_cast<float>(9)/static_cast<float>(5))

What i expected is the result should be a floating point number but why it gives integer number ?
Please tell me actually what is happening .

stream output in c++ by default omits the decimal point if there are no significant digits after it. so,
cout << 64.0 << '\n' ; // prints just 64
cout << showpoint << 64.0 << '\n' ; // prints 64.000000

(the showpoint manipualator makes the stream insert a decimal point even when there are no significant digits after it.)

Thanks a lot vijayan for spending time to answer my question. now i really understood what happened beyond the scenes .

Once again thanks for spending your valuable time for me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.