c - 我的函数一直返回 0 而不是实际计算的返回值?

标签 c function

#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;
}

我不明白为什么我的 toCelsius 值总是返回 0。我的 toFarenheit 函数工作得很好。此外,该程序通过让用户输入负整数值来终止;这是家庭作业的一部分,因此请忽略该部分。

最佳答案

尝试使用:

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

通过这一行:double results = toCelsius(double value),该方法应该返回一个 double 值,而不是一个 ineger 值。

当您只使用 95 时,您实际上进行了整数除法,因此它返回整数值 0,而不是正确的 double 值。

关于c - 我的函数一直返回 0 而不是实际计算的返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41056134/

相关文章:

javascript - GraphQL 解析器 - 在 promise 解决后返回我的函数

java - Netty + Protocol Buffers Java <-> C 通信问题

c - PDF内容流中的各种字形如何编码?

c - 在C/C++程序中,如何为参数 vector 内存分配内存?

c++ - 使用 C 头文件错误编译 Cython

jquery - 调用 var 函数

ruby - ruby 功能的最大数量?

c++ - 在 openCV 中将 cvPoint[][] 转换为 cvPoint** 时出现段错误

Php pdo调用函数和访问变量

c++ - 错误 : expected primary-expression before ')' when passing function-pointer to a function