1

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?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
J'Neal
  • 11
  • 3
  • 2
    `printf("The Fahrenheit equivalent is %5.2f degrees\n" "The Kelvin equivalent is %5.2f degrees\n", convert_temp,convert_temp);` strange you're printing the same value twice, with 2 different legends. – Jean-François Fabre Mar 02 '18 at 03:30
  • 2
    argggh you're calling `return` twice. only the first one is executed. C cannot return several values – Jean-François Fabre Mar 02 '18 at 03:31
  • 2
    This would be the perfect opportunity to learn to use a debugger to step through the code. It would allow you to solve this sort of simple logic error quickly and easily, and in less time than it took you to post here. A debugger is probably the most powerful tool a programmer has, and it's never too soon to learn to use it. – Ken White Mar 02 '18 at 03:33
  • A function can only return one value. – user253751 Mar 02 '18 at 04:22
  • See [**How to debug small programs**](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/) -- it is a very good link. – David C. Rankin Mar 03 '18 at 15:49

1 Answers1

2

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]);
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219