-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

1 Answers1

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,329
  • 21
  • 40