簡體   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