-2
#include <stdio.h>

//function prototypes
const double fiveNinths = 5 / 9;

int toCelsius(double value) 
{
    return ((value - 32) * (fiveNinths));
}

int toFarenheit(double value) 
{
    return (value * 1.8) + 32;
}


int main (void) 
{
    int intValue, menuSelect; 
    double results;
    intValue = 1;

    while (intValue > 0) 
    {
        printf( "enter a positive integer: \n");
        scanf("%d", &intValue);

        if (intValue > 0) 
        {
            printf("Enter 1 to convert to Farenheit, enter 2 to convert to Celsius: \n");
            scanf("%d", &menuSelect);

            if (menuSelect == 1)
            {
                results = toFarenheit(intValue);
                printf("%d degrees to Farenheit is %d\n", intValue, results);
            }
            else if (menuSelect == 2)
            {
                results = toCelsius(intValue);
                printf("%d degrees to Celsius is %d\n", intValue, results);

            }
            else
            {
                printf("invalid menu item, please input only 1 or 2\n");
            }
        }
    }

    return 0;
}

I can't figure out why my toCelsius value always returns 0. my toFarenheit function works just fine. Also, this program terminates by having the user enter in a negative integer value; it's part of the homework assignment so disregard that part.

Kanji Viroja
  • 493
  • 1
  • 7
  • 17
crim
  • 27
  • 4

2 Answers2

0

Try to using:

const double fiveNinths = 5.0 / 9.0;
double toCelsius(double value) {
    return ((value - 32.0) * (fiveNinths));
}

By this line : double results = toCelsius(double value), the method should return a double value, not an ineger value.

When you just use 9 or 5 you actually do integer division, So it returns the integer value 0, neither the right double value.

inverted_index
  • 2,211
  • 17
  • 35
-1

In the declaration of your function it says int toCelsius(double value) , but you are opening the function in your main with results = toCelsius(intValue); . The calculate operations are then used with an integer and 5/9 is propably seen as a 0, which would always make the return value 0 as well. Try to swap 5/9 with just 1, just to see if you get different values than 0. If this is the case you have found the error.

Pedro
  • 1
  • 1
  • 3
  • This is not Python, the types inside a function don't change depending on what you call it with. – Quentin Dec 09 '16 at 09:59
  • true @quentin this is where the problem comes from as i mentioned in my post if you would have red it properly. – Pedro Dec 09 '16 at 11:20
  • You did come close to the answer: `5/9` is an integer division because both `5` and `9` are `int`s. But that won't ever have anything to do with the type of the argument you pass to the function. – Quentin Dec 09 '16 at 12:24