在第5行中,我读取整数并且isint
在读取整数时获取1,如果不是整数则为0。如果isint
是0我有一个循环要求用户给一个整数,我读直到用户给出一个整数。我尝试给出一个字符而不是一个整数的代码,但我有一个无限循环。该程序只是不等待给一个新的输入。我的代码有什么问题?为什么scanf在无效输入中陷入无限循环?
#include <stdio.h>
int main(void) {
int arg1;
//int arg2;
int attacknum = 1;
int isint = 1;
//printf("Insert argument attacks and press 0 when you have done this.\n");
printf("Attack %d\n", attacknum);
attacknum++;
printf("Give attacking argument:");
isint = scanf("%d", &arg1); //line 5
while(isint == 0){
printf("You did not enter a number. Please enter an argument's number\n");
isint = scanf("%d", &arg1);
printf("is int is %d\n", isint);
}
return 0;
}
printf(“is int is%d \ n”,isint)的输出是什么? – Xonar
请提供您场景的控制台输出。 – zsawyer