好吧,所以我必须创建一个天气程序,当我尝试运行代码时,我没有任何错误,但它只会打印“输入起始温度”和“输入结束温度”。但它不会让我输入数据。有什么我需要改变?我知道我还没有完成代码,但我只想在继续执行其他代码之前测试输入。谢谢您的帮助!为什么printf工作,但scanf不是?
#include <stdio.h>
int main(int argc, char **argv)
{
float celcius, fahrenheit, kelvin, ending, interval;
int c, f, k, temp;
printf("which temperature is being input? (C,F,K) ");
scanf("%d", &temp);
if (temp == c)
{
printf("enter a starting temperature");
scanf("%f", &celcius);
printf("enter an ending temperature");
scanf("%f", &ending);
fahrenheit = celcius * 9/5 + 32;
kelvin = celcius + 273.15;
}
if (temp == f)
{
printf("enter a starting temperature");
scanf("%f", &fahrenheit);
celcius = fahrenheit - 32 * 5/9;
kelvin = fahrenheit - 32 * 5/9 + 273.15;
printf("enter an ending temperature");
scanf("%f", &ending);
if (temp == k)
{
}
printf("enter a starting temperature");
scanf("%f", &kelvin);
fahrenheit = kelvin - 273 * 1.8 + 32;
celcius = kelvin - 273.15;
printf("enter an ending temperature");
scanf("%f", &ending);
}
}
由于用户输入数据后的'\ n',我通常在* scanf()格式字符串中至少在第一个IIRC之后有一个空格。 – technosaurus
查看'fgets'函数,你可以用它代替'scanf' – jev