-
March 24th, 2009, 07:25 PM
#1
Function always returns 0
I have an issue with a program I am writing for an assignment, I have been trying to resolve it for the past 2 days. My function keeps returning 0 and I dont know why.
This is my function prototype
Code:
float farToCel ( int ); //Farhenheit to Celcius function prototype using pass by value
This code is from main where I call the function
Code:
case 2:
cout << "Enter the value in Farenheit ";
cin >> value;
a = farToCel(value);
cout << value << " Farenheit is equal to " << a << " Celcius\n" << endl;
break;
And, here is the function definition
Code:
float farToCel(int value)
{
float result;
result = ((5 / 9) * (value - 32));
return result;
}
I have even tried debugging and value is being passed to the function correctly and it still returns a 0 every time. Could someone please let me know what I'm doing wrong here?
-
March 24th, 2009, 07:36 PM
#2
Re: Function always returns 0
5/9 is an integer division. How many 9's in 5, answer 0. 0 * any_num = 0, therefore 0 always gets returned.
try 5.0/9.0
Get Microsoft Visual C++ Express here or CodeBlocks here.
Get STLFilt here to radically improve error messages when using the STL.
Get these two can't live without C++ libraries, BOOST here and Loki here.
Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
Always use [code] code tags [/code] to make code legible and preserve indentation.
Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.
-
March 24th, 2009, 07:40 PM
#3
Re: Function always returns 0
I wanna kick myself right now, I've been struggling with this for two days and all I needed was a .0 at the end.
Thank you very much Russco
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|