Why does parenthesis around a fraction (5/9) break my program? Beginner!
Hey, I just started trying to learn programming so please be gentle. I tried to think of some easy practice programs to make and conversion came to mind so I tried to make a little temperature conversion program. In the conversion equation there's a fraction 5/9 and I put parenthesis around it because I didn't think it would change the result and I thought it made it easier to read. The parenthesis make the result always come out to be zero. When I remove the parenthesis the result is correct. Why is this?
Code:
#include <stdio.h>
#include <stdlib.h>
int main()
{
float fDegrees;
float cDegrees;
char fDegreesString[20];
char cDegreesString[20];
char desiredConversion;
printf("Would you like to convert from Celcius or Farenheit? \nEnter C or F.\n");
desiredConversion = getchar();
getchar();
if (desiredConversion == 'C' || desiredConversion == 'c')
{
printf("What is the temperature in C?\n");
gets(cDegreesString);
cDegrees = atoi(cDegreesString);
fDegrees = (cDegrees * (9 / 5)) + 32;
printf("The temperature in F is %.1f degrees.\n", fDegrees);
}
else if (desiredConversion == 'F' || desiredConversion == 'f')
{
printf("What is the temperature in F?\n");
gets(fDegreesString);
fDegrees = atoi(fDegreesString);
cDegrees = (fDegrees-32)*(5/9);
printf("The temperature in C is %.1f degrees.\n", cDegrees);
}
else
printf("You must type C or F.\n");
return 0;
}
Any other suggestions you have to make this better would be great. GCC shows a warning that I shouldn't use gets. Any suggestion on what I should use instead?
Thanks!