I’m trying to write a program that would convert celcius to fahrenheit and visa-versa. My program is compiling, but it gives me wrong results. I’m been trying to change it by changing pointers, but it wouldnt work. Can anyone please point out to me what is my problem? It seems to me it’s in declarations and pointers, but i’m not sure.Thanks!
#include<stdio.h>
float f2c(float f);
float c2f(float c);
int main(void)
{
float cel;
float celcius;
float fahren;
float fah;
char ch;
float number;
scanf("%c %f", &ch, &number);
if(&ch == "-f"){
f2c(number);
celcius=f2c(number);
printf("%f", celcius);
}
else{
c2f(number);
fahren = c2f(number);
printf("%f", fahren);
}
return 0;
}
float f2c(float f)
{
float cel = (f - 32) * 5/9;
return cel;
}
float c2f(float c)
{
float fah = (9 * c/5 + 32);
return fah;
}
Use floating-point arithmetic, not integer arithmetic, because in integer arithmetic, 5/9 is 0. So you could make this change to your code (though
floatparameters are unusual):Also as one of the other answers states, the condition
(&ch == "-f")is never true.Here is a complete working example, with error checking.