出于某种原因,如果用户输入错误的数据类型,如“J”或“%”,则循环将停止请求输入和将继续显示"Enter an integer >"
一遍又一遍。 我怎样才能使程序处理不好的投入? 为什么不进入非数值事业等奇怪的行为?
#define SENTINEL 0;
int main(void) {
int sum = 0; /* The sum of numbers already read */
int current; /* The number just read */
do {
printf("\nEnter an integer > ");
scanf("%d", ¤t);
if (current > SENTINEL)
sum = sum + current;
} while (current > SENTINEL);
printf("\nThe sum is %d\n", sum);
}