当用户输入字符而不是数字时 C 中的无限循环
infinite loop in C when the user enters a character instead of a number
我有一个问题,我想让用户输入 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()
无效输入时,它不会更新 a
因为这是不可能的,所以您的循环将永远继续,因为 a
永远不会已更新并且 scanf
函数将永远失败,scanf
实际上没有任何内容“读取”,并且永远不会通过无效输入。
确保在重复循环之前通过“消耗”它们来处理无效输入。
scanf 将 '\n' 字符留在输入流中,因此您需要跳过它:
#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);
printf("\nyou have entered: %f\n", a);
return 0;
}
https://godbolt.org/z/baPxdn。但是没有解决输入错误的问题。
我个人更喜欢阅读该行然后扫描它(它确实解决了所有问题):
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
char str[100];
float a;
do
{
printf("Insert number between 1.00 and 10.00:\n");
fgets(str, 100, stdin);
if(sscanf(str, " %f", &a) != 1) continue;
if (a < 1.00 || a > 10.0)
{
printf("Insert a correct number.\n");
}
} while (a < 1.00 || a > 10.00);
printf("\nyou have entered: %f\n", a);
return 0;
}
您必须检查 'scanf' 的结果,并在发生错误时“清理”IO 流缓冲区。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define CLEAN_STD_STREAM() \
do{\
int ch; \
while ((ch = getchar()) != '\n' && ch != EOF); \
}while(0)
int main()
{
float a;
do
{
printf("Insert number between 1.00 and 10.00:\n");
if(scanf("%f", &a) != 1)
{
CLEAN_STD_STREAM();//fflush() dosen't work on the Linux
continue;
}
if (a < 1.00 || a > 10.0)
{
printf("Insert a correct number.\n");
}
} while (a < 1.00 || a > 10.00);
system("pause");
return 0;
}
我有一个问题,我想让用户输入 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()
无效输入时,它不会更新 a
因为这是不可能的,所以您的循环将永远继续,因为 a
永远不会已更新并且 scanf
函数将永远失败,scanf
实际上没有任何内容“读取”,并且永远不会通过无效输入。
确保在重复循环之前通过“消耗”它们来处理无效输入。
scanf 将 '\n' 字符留在输入流中,因此您需要跳过它:
#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);
printf("\nyou have entered: %f\n", a);
return 0;
}
https://godbolt.org/z/baPxdn。但是没有解决输入错误的问题。
我个人更喜欢阅读该行然后扫描它(它确实解决了所有问题):
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
char str[100];
float a;
do
{
printf("Insert number between 1.00 and 10.00:\n");
fgets(str, 100, stdin);
if(sscanf(str, " %f", &a) != 1) continue;
if (a < 1.00 || a > 10.0)
{
printf("Insert a correct number.\n");
}
} while (a < 1.00 || a > 10.00);
printf("\nyou have entered: %f\n", a);
return 0;
}
您必须检查 'scanf' 的结果,并在发生错误时“清理”IO 流缓冲区。
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define CLEAN_STD_STREAM() \
do{\
int ch; \
while ((ch = getchar()) != '\n' && ch != EOF); \
}while(0)
int main()
{
float a;
do
{
printf("Insert number between 1.00 and 10.00:\n");
if(scanf("%f", &a) != 1)
{
CLEAN_STD_STREAM();//fflush() dosen't work on the Linux
continue;
}
if (a < 1.00 || a > 10.0)
{
printf("Insert a correct number.\n");
}
} while (a < 1.00 || a > 10.00);
system("pause");
return 0;
}