Problem while Type Casting

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Sep 2006
Posts: 80
Reputation: parthiban is an unknown quantity at this point 
Solved Threads: 6
parthiban's Avatar
parthiban parthiban is offline Offline
Junior Poster in Training

Problem while Type Casting

 
0
  #1
May 30th, 2007
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 :

  1. # include<iostream>
  2. using namespace std;
  3.  
  4. int main()
  5. {
  6. float celsius;
  7. cout<<"Enter the temperature in celsius: ";
  8. cin>>celsius;
  9. float fahrenheit =(celsius*static_cast<float>(9/5))+32;
  10. cout<<"Temperature in Fahrenheit = "<<fahrenheit<<endl;
  11. return 0;
  12. }
Please tell me what is wrong with that code with explanation .
Thanks in advance.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Problem while Type Casting

 
0
  #2
May 30th, 2007
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;
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Problem while Type Casting

 
0
  #3
May 30th, 2007
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!
Last edited by vijayan121; May 30th, 2007 at 2:40 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 80
Reputation: parthiban is an unknown quantity at this point 
Solved Threads: 6
parthiban's Avatar
parthiban parthiban is offline Offline
Junior Poster in Training

Re: Problem while Type Casting

 
0
  #4
May 30th, 2007
Thanks Salem and Vijay . I now understood .
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 80
Reputation: parthiban is an unknown quantity at this point 
Solved Threads: 6
parthiban's Avatar
parthiban parthiban is offline Offline
Junior Poster in Training

Re: Problem while Type Casting

 
0
  #5
May 30th, 2007
Originally Posted by vijayan121 View Post
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 :

  1. Enter the temperature in celsius: 32
  2. Temperature in Fahrenheit = 64
what i expect is 64.000000 .

Please tell me actually what is happening .
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 647
Reputation: jaepi is an unknown quantity at this point 
Solved Threads: 4
jaepi's Avatar
jaepi jaepi is offline Offline
Practically a Master Poster

Re: Problem while Type Casting

 
0
  #6
May 30th, 2007
what about this

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

no need to type cast
Last edited by jaepi; May 30th, 2007 at 7:52 am.
Retreat!!!
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,378
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1466
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Problem while Type Casting

 
0
  #7
May 30th, 2007
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:
  1. celsius*static_cast<float>(static_cast<float>(9)/static_cast<float>(5))
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,089
Reputation: vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all vijayan121 is a name known to all 
Solved Threads: 164
vijayan121 vijayan121 is offline Offline
Veteran Poster

Re: Problem while Type Casting

 
0
  #8
May 30th, 2007
Originally Posted by parthiban View Post
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.)
Last edited by vijayan121; May 30th, 2007 at 8:16 am.
Reply With Quote Quick reply to this message  
Join Date: Sep 2006
Posts: 80
Reputation: parthiban is an unknown quantity at this point 
Solved Threads: 6
parthiban's Avatar
parthiban parthiban is offline Offline
Junior Poster in Training

Re: Problem while Type Casting

 
0
  #9
May 30th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC