【发布时间】:2015-12-07 19:01:25
【问题描述】:
我使用 cs50.io (c) 编写了一个代码。唯一的问题是,每当我在终端中检查 50 时,我都会收到一条错误消息,指出拒绝非数字输入 + 预期输入...这是我的代码,下面是终端中的错误。任何帮助将不胜感激。
#include <stdio.h>
int main() {
float celsius, fahrenheit;
printf("\nEnter temp in Celsius : ");
scanf("%f", &celsius);
fahrenheit = (1.8 * celsius) + 32;
printf("F: %.1f\n", fahrenheit);
return (0);
}
https://sandbox.cs50.net/checks/ef786d6473b2409381a7161c725f8776
【问题讨论】:
-
类似
while (scanf("%f", &celsius) != 1) printf("\nEnter temp in Celsius : ");?? -
a) 检查来自
scanf的返回值和b) 多读一个字符并检查它是newline。 -
请注意,CS50 课程附带的库包含在源文件
cs50.c和cs50.h中,其中定义的两个函数是GetFloat()和GetDouble(),它们处理这些问题。