我有这个运行一次的代码;然后我输入一些非数字文本,它会继续打印
Enter Seed
不执行scanf
代码。
do {
printf("Enter SEED: ");
scanf("%d", &seed);
}
while (!isdigit(seed));
最佳答案
先前输入的尾随换行符将被视为新输入,您需要将其吃掉
避免使用 scanf
,而是使用 fscanf
:-
int seed, ch;
do{
printf("Enter SEED: ");
if(fscanf(stdin, "%d", &seed) == 1)
break;
else
while( (ch = getchar()) != '\n' && ch != EOF); //Eat the trailing newline
}while(1);
关于c - 在 C 中使用 do while 循环进行数字验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21978250/