C++ Functions - Error: '0' cannot be used as a function

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));
}

Answers


  1. 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.

  2. 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);
    

((5 / 9)(fahrenheit - 32))
 \_____/\_______________/
    1           2

2 is interpreted as a function call on 1. You forgot the multiplication:

((5 / 9) * (fahrenheit - 32))

You've forgotten the * operator


You should change return ((5 / 9)(fahrenheit - 32)); to

return ((5 / 9)*(fahrenheit - 32));

Add * after (5/9). Because of missing * you are getting the error.


Need Your Help

C#, Windows Forms, and Application.Run()

c# winforms

when I call Application.Run() on a form, this command halts the program until the form is closed, it seems. Is there another command I can use to create the form without halting the program? Thanks