0

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;
}
  • Oh wow I did not even see that question that was pretty much the same as mine. I apologize for the duplicate I am new to this website. – user3377510 Mar 04 '14 at 04:39
  • At least you know for next time. As a general rule though, most beginnerish type problems like this one have been asked at least once (in this case, several times), and before you posted, you ought to have been shown plenty of possibly related questions. – Dennis Meng Mar 04 '14 at 04:42

2 Answers2

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

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • _scanf("%f", Celsius);_ I guess this wont work. Atleast you need & – Digital_Reality Mar 04 '14 at 04:34
  • Thank you so much I knew it was something silly but I could not figure out what it was. Perhaps I should have just multiplied by the decimal created by 5.0/9. Nonetheless, thank you for your help. – user3377510 Mar 04 '14 at 04:35
  • @Abhay `Celsius` has type `float *` in that function. Not good variable naming though, because the same name `Celsius` in `main` has type `float`. – Yu Hao Mar 04 '14 at 04:35
  • @YuHao Thank you for your suggestions and assistance. I am still getting used some of the syntax in C that is differs from what I am used to. As well as not programming for a while. – user3377510 Mar 04 '14 at 04:54
1
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);
Digital_Reality
  • 4,488
  • 1
  • 29
  • 31