【发布时间】:2020-11-13 19:09:16
【问题描述】:
我有一个问题,我想让用户输入一个从 1.00 到 10.00 的数字,如果他输入的数字超出该范围,我会显示他错了,然后重试。我认为我做得很好,但问题是如果用户输入一个字母循环无限重复,我会很感激你的帮助。谢谢你。 :)
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
float a;
do
{
printf("Insert number between 1.00 and 10.00:\n");
scanf("%f", &a);
if (a < 1.00 || a > 10.0)
{
printf("Insert a correct number.\n");
}
} while (a < 1.00 || a > 10.00);
system("pause");
return 0;
}
【问题讨论】:
-
scanf返回一个值。您需要检查它是否能够读取任何内容。
标签: c loops if-statement scanf do-while