J'Neal
J'Neal

Reputation: 11

Program to convert between Fahrenheit, Celsius and Kelvin

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?

Upvotes: 1

Views: 2252

Answers (1)

Jean-Fran&#231;ois Fabre
Jean-Fran&#231;ois Fabre

Reputation: 140188

You're returning more than once in a branch. Therefore, only the first return statement is executed.

You cannot return a couple of integers from a function. But you can assign an array of output parameter. I would do (why not a switch/case statement ?):

void tempConvert(float inTemp, char ch, float results[2])
{
    switch(ch)
    {
    case 'c':
        results[0] = ( (5.0/9.0) * (inTemp - 32.0) );
        results[1]  = ( inTemp + 273.15 );
        break;
    case 'f':
        results[0] = ( ((9.0/5.0) * inTemp ) + 32.0 );
        results[1]  = ( (5.0/9.0) * (inTemp + 459.67 ) );
        break;
    case 'k':
         results[0]  = ( inTemp - 273.15 );
         results[1]  = ( ((9.0/5.0) * inTemp ) - 459.67 );
         break;
    default:
         results[0] = results[1] = 0; // kind of error code
  }
}

call it like this:

float convert_temp[2]
tempConvert(temp, my_char, convert_temp);

if (my_char == 'c')
    printf("The Fahrenheit equivalent is %5.2f degrees\n"
        "The Kelvin equivalent is %5.2f degrees\n",
        convert_temp[0],convert_temp[1]);

Upvotes: 2

Related Questions