Here's one way of doing what I think you're trying to do:
Code:
itsme@dreams:~/C$ cat temperature.c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
enum { TEMP_C = 1, TEMP_F = 2, TEMP_K = 3 };
// To avoid writing 6 functions, convert everything to kelvin first
double deg_to_kelvin(double temp, int scale)
{
double rv;
switch(scale)
{
case TEMP_C: rv = temp+273; break;
case TEMP_F: rv = (temp-32)/1.8+273; break;
case TEMP_K: rv = temp; break;
default:
puts("Unhandled scale!");
exit(EXIT_FAILURE);
}
return rv;
}
double deg_to_celcius(double temp)
{
return temp-273;
}
double deg_to_farenheit(double temp)
{
return (temp-273)*1.8+32;
}
int main(void)
{
char buf[4096];
int scale;
double temp, kelvin_temp;
do
{
printf("Temperature is in:\n 1) Celcius\n 2) Farenheit\n 3) Kelvin\n? ");
fflush(stdout);
fgets(buf, sizeof(buf), stdin);
scale = atoi(buf);
} while(scale < 1 || scale > 3);
printf("And the temperature is: ");
fflush(stdout);
fgets(buf, sizeof(buf), stdin);
temp = atof(buf);
kelvin_temp = deg_to_kelvin(temp, scale);
printf("\nCelcius : %.2f\n", deg_to_celcius(kelvin_temp));
printf("Farenheit: %.2f\n", deg_to_farenheit(kelvin_temp));
printf("Kelvin : %.2f\n", kelvin_temp);
return 0;
}