0

The program runs but I can't figure out why the function within the third else if statement doesn't return an accurate value. Thank you for the help in
advance.

finaltemp = newtemp(t, choice);

printf("\nThe converted temperature is: %f", finaltemp);

return(0);
}

double newtemp(double a, double b)
{
double result;
if (b==1)
  {
    result = (a-(273.15));
  }
if (b==2)
  {
    result = (a+(273.15));
  }
if (b==3)
  {
    result = (((5/9)*(a))-32);
  }
if (b==4)
  {
    result = (((9/5)*(a))+32);
  }
if (b==5)
  {
    result = (((9/5)*(a))-459.67);
  }
if (b==6)
  {
    result = ((a+459.67)*(9/5));
  }

 return(result);
 }
gwiz_kid
  • 63
  • 6
  • 1
    Where is " the third else if statement"? I see no `else` in your posted code. – MikeCAT Aug 07 '16 at 03:05
  • @MikeCAT I didn't format it in the post correctly so the 'else' part of the statement was cut off. Anyway the mistake I made was using int division rather than floating point division. Thanks for the response. – gwiz_kid Aug 07 '16 at 04:25

1 Answers1

3

You're performing integer division, not floating point division.

The expression (5/9) divides two int values, so the result is an int, specifically 0. You need to use floating point constants to force floating point division.

if (b==3)
  {
    result = (((5.0/9.0)*(a))-32);
  }
if (b==4)
  {
    result = (((9.0/5.0)*(a))+32);
  }
if (b==5)
  {
    result = (((9.0/5.0)*(a))-459.67);
  }
if (b==6)
  {
    result = ((a+459.67)*(9.0/5.0));
  }
dbush
  • 205,898
  • 23
  • 218
  • 273
  • Thanks for that. I actually realized what I was doing wrong after staring at it for 30 minutes. Such a silly oversight. Thanks for the explanation as well! – gwiz_kid Aug 07 '16 at 04:20