-2

I am trying to use a function to convert from Fahrenheit to Celsius. I wish to know where I made a mistake.

#include <stdio.h>

void degrees_Fto_C(float f){
    float c;
    c=(5/9)*(f–(32));
    printf("%f",c);
}

int main(){
    float temp;
    printf("Enter the temperature in Fahrenheit: \n");
    scanf("%f",&temp);
    degrees_Fto_C(temp);
    return 0;
}

The error message is:

C:\Users\Pranavs\Desktop\Untitled1.cpp  In function 'void degrees_Fto_C(float)':
C:\Users\Pranavs\Desktop\Untitled1.cpp  [Error] 'f' cannot be used as a function
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Stark10
  • 1
  • 1

1 Answers1

1

You have the wrong character after the f in line 4. c=(5/9)*(f–(32)) needs to be c=(5.0/9) * (f-(32)). Your minus sign is a unicode character, and you need ASCII. If you backspace it and replace it with a normal minus sign, it will compile.

Also, you're doing integer arithmetic, and you'll always get zero. It will work better if you add a decimal point after the 5 or the 9.

Here's a working version of your program:

#include <stdio.h>
void degrees_Fto_C(float f) {
    float c;
    c = (5.0 / 9) * (f - (32));
    printf("%f", c);
}
int main() {
    float temp;
    printf("Enter the temperature in Fahrenheit: \n");
    scanf("%f", &temp);
    degrees_Fto_C(temp);
    return 0;
}
Daniel Giger
  • 2,023
  • 21
  • 20