程序没有正确读取 "double" 输入
Program not reading the "double" input correctly
在这个 C 程序中,如果我输入 b 作为输入,程序会打印 Enter correct input。如果我输入 b45 作为输入,程序再次打印 Enter correct input。但是,如果我输入 45b,程序会将 45 作为输入并正常进行。
如果我输入 45b,程序应该打印 Enter correct input,但它没有发生。
#include <stdio.h>
int main()
{
double i;
printf ("Enter a number. \n");
while (scanf("%lf", &i) == 0)
{
printf("Enter correct input. \n");
fflush (stdin);
}
printf ("%lf\n", i);
return 0;
}
不是通过 scanf()
阅读,而是使用 fgets(buf, ...)
阅读 行 的用户输入,然后使用 strtof(buf, &endptr))
来评估输入的是数字,endptr
评估数字文本后的内容。
在这个 C 程序中,如果我输入 b 作为输入,程序会打印 Enter correct input。如果我输入 b45 作为输入,程序再次打印 Enter correct input。但是,如果我输入 45b,程序会将 45 作为输入并正常进行。
如果我输入 45b,程序应该打印 Enter correct input,但它没有发生。
#include <stdio.h>
int main()
{
double i;
printf ("Enter a number. \n");
while (scanf("%lf", &i) == 0)
{
printf("Enter correct input. \n");
fflush (stdin);
}
printf ("%lf\n", i);
return 0;
}
不是通过 scanf()
阅读,而是使用 fgets(buf, ...)
阅读 行 的用户输入,然后使用 strtof(buf, &endptr))
来评估输入的是数字,endptr
评估数字文本后的内容。