| | |
Problem while Type Casting
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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 :
Please tell me what is wrong with that code with explanation .
Thanks in advance.
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 :
C++ Syntax (Toggle Plain Text)
# 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; }
Thanks in advance.
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 :
what i expect is 64.000000 .
Please tell me actually what is happening .
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 :
C++ Syntax (Toggle Plain Text)
Enter the temperature in celsius: 32 Temperature in Fahrenheit = 64
Please tell me actually what is happening .
what about this
no need to type cast
C++ Syntax (Toggle Plain Text)
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!!!
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:
C++ Syntax (Toggle Plain Text)
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.
•
•
Join Date: Dec 2006
Posts: 1,089
Reputation:
Solved Threads: 164
•
•
•
•
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 .
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.
![]() |
Similar Threads
- Type casting and type conversions (C)
- Type casting (C++)
- Problem with a snippet of code in a shell program (C)
Other Threads in the C++ Forum
- Previous Thread: string to int conversion
- Next Thread: im new at C pls help me debug this program.. tnx
| Thread Tools | Search this Thread |
api array based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






