C Temperature Conversion Program Keeps Outputting

2019-03-04 01:01发布

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

2条回答
混吃等死
2楼-- · 2019-03-04 01:26
cel = (fah-32) * (5/9);

5/9 is int/int and will give you results in int so that is 0.

Change it to

cel = (fah-32) * (5.0/9.0);

or

cel = (fah-32) * ((float)5/(float)9);
查看更多
三岁会撩人
3楼-- · 2019-03-04 01:36
cel = (fah-32) * (5/9);

Here, 5/9 is integer division, its result is 0, change it to 5.0/9


And in several lines, you are using

scanf("%f", &*Celsius);

&* is not necessary, simply scanf("%f", Celsius); would do.

查看更多
登录 后发表回答