I'm new to functions and trying to understand what I've done wrong. My build message spits out the error: '0' cannot be used as a function and highlights the line return ((5 / 9)(fahrenheit - 32)); within the function. Thanks in advance for any advice.
#include <iostream>
using namespace std;
double celsiusFunction(double fahrenheit);
int main()
{
double fahrenheitTemp;
fahrenheitTemp = celsiusFunction(99);
cout << fahrenheitTemp;
return 0;
}
double celsiusFunction(double fahrenheit)
{
return ((5 / 9)(fahrenheit - 32));
}
5 / 9 is 0, because both are integers and thus it's evaluated in integer arithmetic. Do this instead: 5.0 / 9.0 to get floating results.
You're not multiplying in the return statement, so the compiler interprets the second parentheses as a funciton call (that is, calling 5 / 9 with arguments fahrenheit - 32). This is of course nonsense. Do this:
return (5.0 / 9.0) * (fahrenheit - 32.0);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With