1

I'm trying to make a temperature conversion function for a class assignment, and my function receives all of the arguments correctly but for some reason after it hits the equation to convert the number it just makes the number 0.

Here is the code so you know what I'm talking about:

void convertToCelsius(int farenheitTemperature, int temperatureType){

if (temperatureType == 1)
{

farenheitTemperature = ((5/9) * (farenheitTemperature - 32));
printf("Your temperature in Celsius is %i\n", farenheitTemperature);

}

else
{
    printf("Your temperature is already in celsius!\n");
}
}

the main function puts the arguments in correctly, so I don't think that's the problem.

Thank you very much for any help you can give!

5
  • 1
    don't use int, use float Dec 9, 2017 at 17:13
  • 2
    (5/9) is zero.
    – harold
    Dec 9, 2017 at 17:13
  • Since 5 and 9 are both integers, 5/9 uses integer division, and the result is the integer 0. You probably want to use double instead of int.
    – Tom Karzes
    Dec 9, 2017 at 17:15
  • Thanks everyone, that helped a lot. The problem was in the integer to double conversion of 5/9
    – vp123
    Dec 9, 2017 at 17:18
  • 2
    I wanted to take a moment to recognize that this is a well asked question, especially from a new user. The only thing I would suggest for next time is before asking your question, try to do a little more debugging. By that I mean, try to do each part of your program and print it separately so you can figure out exactly what's going wrong: printing (farenheitTemperature - 32), and (5/9) and each intermediate step separately -- had you done that, you would have seen that (5/9) was zero, and you might have been able to figure out what was going on without even asking here. Good luck in C!
    – Davy M
    Dec 9, 2017 at 17:20

3 Answers 3

3

Kindly check operators precedence & associativity, there is a problem in below statement

farenheitTemperature = ((5/9) * (farenheitTemperature - 32)); 

(5/9) yields in 0 not some decimal fractional digit because coefficient is 0.

If you ant correct result either typecast or replace like

farenheitTemperature = ((5 * (farenheitTemperature - 32)/9);

I hope it helps.

2

Use type double not int for your temperature parameter.

void convertToCelsius(double farenheitTemperature, int temperatureType)
{
    if (temperatureType == 1)
    {
        farenheitTemperature = ((5.0/9.0) * (farenheitTemperature - 32.0));
        printf("Your temperature in Celsius is %f\n", farenheitTemperature);
    }
    else
    {
        printf("Your temperature is already in celsius!\n");
    }
}
6
  • yup bingo! Thank you.
    – vp123
    Dec 9, 2017 at 17:19
  • 1
    @vp123 On Stack Overflow, the proper way to thank someone isn't actually by commenting "Thank you" on their answer, but rather by accepting the answer. Make sure you do that if this answer helped you. Also, once you're around the site a little longer and have more reputation, you will be able to upvote any answer that helps you too, which is another way to say Thank you.
    – Davy M
    Dec 9, 2017 at 17:25
  • @vp123 I am glad to help. Please also read comments regarding devision 'int' by 'int'. That operation will result in int not in a double value. In your case it will result in int value 0
    – sg7
    Dec 9, 2017 at 17:26
  • In integer precision is sufficient as indicated by the original interface, then a floating point solution may be unnecessary, and prohibitive on low-end targets without FPU hardware. Re-ordering the integer expression to multiply before divide will work.
    – Clifford
    Dec 9, 2017 at 17:49
  • @Clifford You are right! However, the OP has not indicated hardware restrictions. We also do not know the required precision for the conversion. I simply tried to make his original code to work with the minimum amount of changes. Thanks!
    – sg7
    Dec 9, 2017 at 18:34
-1

Don't know why are you writing it that way(it's better to use a different variable name on the left-hand side for temperature in Celsius. "Code readability matters") but you are doing wrong when you write,

farenheitTemperature = ((5/9) * (farenheitTemperature - 32));

In C,(5/9) evaluates to 0, which makes the whole expression 0. You should use double/float for fahrenheitTemperature and change (5/9) to (5/9.0) or (5.0/9).

Not the answer you're looking for? Browse other questions tagged or ask your own question.