I'm looking for some help on a C program. Our professor showed us an example where we would input a temperature in degrees Celsius or Fahrenheit and have it converted to the other. I found it interesting and tried taking it a step further, adding Kelvin.
#include <stdio.h>
int main(void)
{
#define MAXCOUNT 4
float tempConvert(float, char);
int count;
char my_char;
float convert_temp, temp;
for(count = 1; count <= MAXCOUNT; count++)
{
printf("\nEnter a temperature: ");
scanf("%f %c", &temp, &my_char);
convert_temp = tempConvert(temp, my_char);
if (my_char == 'c')
printf("The Fahrenheit equivalent is %5.2f degrees\n"
"The Kelvin equivalent is %5.2f degrees\n",
convert_temp,convert_temp);
else if (my_char == 'f')
printf("The Celsius equivalent is %5.2f degrees\n"
"The Kelvin equivalent is %5.2f degrees\n",
convert_temp,convert_temp);
else if (my_char == 'k')
printf("The The Celsius equivalent is %5.2f degrees\n"
"The Fahrenheit equivalent is %5.2f degrees\n",
convert_temp,convert_temp);
}
return 0;
}
float tempConvert(float inTemp, char ch)
{
float c_temp1, c_temp2;
if (ch == 'c'){
return c_temp1 = ( (5.0/9.0) * (inTemp - 32.0) );
return c_temp2 = ( inTemp + 273.15 );}
else if (ch == 'f'){
return c_temp1 = ( ((9.0/5.0) * inTemp ) + 32.0 );
return c_temp2 = ( (5.0/9.0) * (inTemp + 459.67 ) );}
else if (ch == 'k'){
return c_temp1 = ( inTemp - 273.15 );
return c_temp2 = ( ((9.0/5.0) * inTemp ) - 459.67 );}
}
The program is running in terminal, but the issue is I'm only getting the answer for the first temperature conversion, not the one for second (for second it's just the same as the first). My question is about why the second answer isn't being identified, and how I can fix it?