保持0.00作为答案

时间:2016-02-07 18:44:10

标签: c

我最后一直收到0作为答案。请帮忙

#include <stdio.h>
int Fahrenheit = 0;
double Celsius = 0.0;
double main(void)
{
    printf("This program will convert the temperature from fahrenheit to celsius.\n");
    printf("Please type in the temperature in fahrenheit followed by the enter key.\n");
    scanf("%d%",&Fahrenheit);
    Celsius = (5/9) * (Fahrenheit-32) ;
    printf("Your temperature in celsius is %3.2f.\n", Celsius);

    return(0);
}

1 个答案:

答案 0 :(得分:1)

由于整数除法,请将5 / 9更改为5.0 / 9.0。另外,请Fahrenheit double并将scanf()更改为

if (scanf("%lf", &Fahrenheit) == 1)
{
    Celcius = 5.0 * (Fahrenheit - 32.0) / 9.0;
    printf("Your temperature in celsius is %3.2f.\n", Celsius);
}

此外:

  1. 绝对没有理由让你的变量变得全球化。
  2. 我见过许多奇特的main()签名,现在

    double main(void);
    
  3. 忽略scanf()的返回值会导致潜在的未定义行为。如果我有一位老师禁止if陈述但需要scanf()我会退出并找到一位优秀的导师。

    但当然,如果我在学习,我怎么知道忽略scanf()的回报值是不是很糟糕?这是令人悲伤的部分,许多人甚至不知道它返回一个值或者它失败了,例如试试这个

    int value;
    if (scanf("%d", &value) != 1)
        fprintf(stderr, "Error, invalid input\n");
    else
        fprintf(stdout, "Ok, so your input is `%d'\n", value);
    

    键入"abcd"而非数字,会发生什么?

相关问题