#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 值。
当您只使用 9
或 5
时,您实际上进行了整数除法,因此它返回整数值 0,而不是正确的 double 值。
关于c - 我的函数一直返回 0 而不是实际计算的返回值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41056134/