我写的很快,每次执行程序时,我最终都会得到32华氏度和0.00摄氏度,我不知道这里有什么问题。
#include <stdio.h>
int main(void)
{
double celsius=0, fahrenheit=0;
printf("Enter a temperature in degrees Celsius: ");
scanf("%f", &celsius);
fahrenheit =(5.0/9.0)*celsius + 32;
printf("That is %.2f Fahrenheit \n", fahrenheit);
printf("Enter a temperature in degrees fahrenheit: ");
scanf("%f", &fahrenheit);
celsius = (fahrenheit - 32) * (5.0/9.0);
printf("That is %.2f Celsius \n", celsius);
return(0);
}
答案 0 :(得分:3)
double
的格式说明符错误,应为"%lf"
。但这还不够,你还应该检查scanf()
成功读取读取值,就像这样
#include <stdio.h>
int report_error(const char *const message)
{
// TODO: add message formatting capabilities to this function
fprintf(stderr, "error: %s\n", message);
return EXIT_FAILURE;
}
int main(void)
{
double celsius = 0;
double fahrenheit = 0;
printf("Enter a temperature in degrees Celsius: ");
if (scanf("%lf", &celsius) != 1)
return report_error("Invalid Input");
fahrenheit = (5.0 / 9.0) * celsius + 32;
// ^ Define this as a constant?
printf("That is %.2f Fahrenheit \n", fahrenheit);
printf("Enter a temperature in degrees fahrenheit: ");
if (scanf("%lf", &fahrenheit) != 1)
return report_error("Invalid Input");
celsius = (fahrenheit - 32) * (5.0 / 9.0);
// ^ see if it was a constant???
printf("That is %.2f Celsius \n", celsius);
return 0;
}
此外,您的代数似乎出错了。您应该明确检查celsius
表达式。