2015-10-18 34 views
-1

我有一個程序,提示用戶輸入一個不是負數或字符的整數。我想提示用戶輸入一個浮點數。我該怎麼做呢?如何在c中輸入float時進行錯誤檢查?

do { 
    printf("Enter a number.\n"); 
} while(((scanf("%d%c", &x1, &term) != 2 || term != '\n') 
      && reset_stdin()) || x1 < 1); 

int reset_stdin() 
{ 
    while (getchar()!='\n'); 
    return 1; 
} 
+0

何必下投票? – user5410809

回答

1

要與float使用scanf,只需使用"%f"格式說明。

float input; 
scanf("%f", &input); 
0

要提示你用一個浮點數用戶:

scanf("%f", &varname);