2015-10-18 35 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);