首页 > 解决方案 > 当用户输入无效输入时,为什么我的程序会进入无限循环?

问题描述

#include <stdio.h>

int main() {
  int input;
  do {
    printf("Welcome to the Arithmetic Quiz Program!\n\n");
    printf("Please choose one of the following by entering the correspoding "
           "number:\n\n");
    printf("1. Give me an addition problem.\n");
    printf("2. Give me a subtraction problem.\n");
    printf("3. Give me a multiplication problem.\n");
    printf("4. Quit.\n\n");

    scanf(" %i", &input);

  } while (input != 1 && input != 2 && input != 3 && input != 4);
  return 0;
}

当我输入不是整数的东西(例如:'a'、'b'、..)时,结果如下:

标签: c

解决方案


推荐阅读