繁体   English   中英

程序未正确读取“双”输入

[英]Program not reading the "double" input correctly

在这个 C 程序中,如果我输入 b 作为输入,程序会打印 Enter correct input。 如果我输入 b45 作为输入,程序会再次打印 Enter correct input。 但是,如果我输入 45b,程序将 45 作为输入并正常运行。

如果我输入 45b,程序应该打印输入正确的输入,但它没有发生。

#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评估数字之后的内容文本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM