![](/img/trans.png)
[英]FILE * is reading zeros into a 'double' variable from input text file after reading previous entries correctly
[英]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.