This question already has an answer here:
My temperature conversion program in C keeps outputting 0 when I attempt to convert Fahrenheit to Celsius. The conversion from Celsius to Fahrenheit seems to work just fine. I have done the exact same thing for both functions and portions but I keep getting 0 for the second conversion. Can someone please help me or tell me what I am doing wrong?
#include <stdio.h>
//Function Declarations
float get_Celsius (float* Celsius); //Gets the Celsius value to be converted.
void to_Fahrenheit (float cel); //Converts the Celsius value to Fahrenheit and prints the new value.
float get_Fahrenheit (float* Fahrenheit); //Gets the Fahrenheit value to be converted.
void to_Celsius (float fah); //Converts the Fahrenheit value to Celsius and prints the new value.
int main (void)
{
//Local Declarations
float Fahrenheit;
float Celsius;
float a;
float b;
//Statements
printf("Please enter a temperature value in Celsius to be converted to Fahrenheit:\n");
a = get_Celsius(&Celsius);
to_Fahrenheit(a);
printf("Please enter a temperature value in Fahrenheit to be converted to Celsius:\n");
b = get_Fahrenheit(&Fahrenheit);
to_Celsius(b);
return 0;
} //main
float get_Celsius (float* Celsius)
{
//Statements
scanf("%f", &*Celsius);
return *Celsius;
}
void to_Fahrenheit (float cel)
{
//Local Declarations
float fah;
//Statements
fah = ((cel*9)/5) + 32;
printf("The temperature in Fahrenheit is: %f\n", fah);
return;
}
float get_Fahrenheit (float* Fahrenheit)
{
//Statements
scanf("%f", &*Fahrenheit);
return *Fahrenheit;
}
void to_Celsius (float fah)
{
//Local Declarations
float cel;
//Statements
cel = (fah-32) * (5/9);
printf("The temperature in Celsius is: %f\n", cel);
return;
}
5/9
isint/int
and will give you results inint
so that is0
.Change it to
or
Here,
5/9
is integer division, its result is0
, change it to5.0/9
And in several lines, you are using
&*
is not necessary, simplyscanf("%f", Celsius);
would do.