【发布时间】:2017-07-10 01:46:18
【问题描述】:
所以我正在尝试制作一个简单的温度转换器。对我来说一切都很好,但我不知道为什么扫描输入没有被识别。 感谢您的宝贵时间。
#include <stdio.h>
exercise1(){
float a;
char tem;
printf("--- Temperature Converter ---\n");
printf("Please enter a temperature: ");
scanf("%f", &a);
printf("\nWould you like to convert to Celsius or Fahrenheit? (c or f)");
scanf("%c", &tem); getchar();
if (tem == 'c'){
a = ((float)a-32) * 5.0f/9.0f;
printf("\nYour temperature is %g\n", &a);
}
else if (tem == 'f'){
a = (float)a * 9.0f/5.0f + 32;
printf("\nYour temperature is %g\n", &a);
}
else
printf("\nPlease enter a valid conversion type!\n");
}
}
【问题讨论】:
-
printf("\nYour temperature is %g\n", &a);remove&-->printf("\nYour temperature is %g\n", a); -
可能是因为您在它之后调用了
getchar。顺便说一句,有了float a,后面的代码就不需要(float)a(但这纯粹是语义上的)。 -
谢谢,我把所有的答案都结合起来了。
标签: c if-statement scanf equals-operator