Hi all!
I've been messing around trying to learn C. I bought a book from O'Rielly called
Practical C Programming. After a few chapters, I ran into an excercise that suggests writing a program to convert Centigrade to Farenheit, so I said, "How about a program that converts both ways??" Some of it works fine, but when I try to do conversions, the F-to-C conversion always produces -32.000000, and the C-to-F always produces whatever I input + 32. Could anyone help me figure this out??
Here is what i have so far:
Code:
#include <stdio.h>
char type; /* Type of conversion */
float far; /* Temp in Degrees Farenheit */
float cel; /* Temp in Degreen Celcius */
float result; /* Result of calculation */
char line[100]; /* Input Line */
int main()
{
while (1){
printf("Enter Type of Conversion;\n ? for help, q to quit:");
fgets(line, sizeof(line), stdin);
sscanf(line, "%c", &type);
if((type == 'q') || (type == 'Q'))
break;
switch(type) {
case 'f':
printf("Enter temperature in degrees Farenheit: ");
fgets(line, sizeof(line), stdin);
sscanf(line, "%f", &far);
cel = (5 / 9) * (far - 32);
printf("%f Degrees Celcius\n", cel);
break;
case 'c':
printf("Enter temperature in degrees Celcius: ");
fgets(line, sizeof(line), stdin);
sscanf(line, "%f", &cel);
far = ((9 / 5) * cel) + 32;
printf("%f Degrees Farenheit\n", far);
break;
case '?':
printf("Type f for farenheit-celcius conversion;\nType c for celcius-farenheit conversion;\nQ to quit;\n? for help\n");
default:
printf("Unrecognised Command: Type ? for help.\n");
}
}
}
Any help will be appreciated
Ian