我有只运行一次的代码;然后我输入一些非数字文本,然后继续打印
输入种子
无需执行scanf
代码。
do {
printf("Enter SEED: ");
scanf("%d", &seed);
}
while (!isdigit(seed));
来自先前输入的尾随换行符被当作新输入,您需要将其吃光
避免使用scanf
usefscanf
代替:
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);
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句