打破案例与某些回应

Breaking Case with certain responses

我正在创建一个小评分程序,但遇到了这个问题。当我输入 'e' 时,它会按照默认建议正确中断,但随后会运行第一个 switch 语句,然后退出程序。

只有当我输入字母 'e' 时才会出现这种情况。如果我输入 'q' 或其他任何内容,它不会退出 ?

case 'd': // Field Competition Logs
{
  int fieldRound;

  int ifaaField[2], ifaaFieldTotal;
  int ifaaHunter[2], ifaaHunterTotal;
  int fitaField[1];
  int field3D[1];

  printf ("Please select the type of round you shot\n\n");
  printf ("\t(a) IFAA Field\n\t(b) IFAA Hunter\n\t(c) Fita Field\n\t(d) 3D Field\n> ");
  scanf (" %d", &fieldRound);

  switch (fieldRound)
  {
    case 'a': // Ifaa Field Round
    {
      printf ("Please enter the score for your first round > ");
      scanf (" %d", &ifaaField[0]);

      printf ("Please enter the score for your second round > ");
      scanf (" %d", &ifaaField[1]);

      ifaaFieldTotal = ifaaField[0] + ifaaField[1];

      printf ("Total of your first half (%d) and your second half (%d) is %d", ifaaField[0], ifaaField[1], ifaaFieldTotal);

      break;
    }

    case 'b': // Ifaa Hunter Round
    {
      printf ("Please enter the score for your first round > ");
      scanf (" %d", &ifaaHunter[0]);

      printf ("Please enter the score for your second round > ");
      scanf (" %d", &ifaaHunter[1]);

      ifaaHunterTotal = ifaaHunter[0] + ifaaHunter[1];

      printf ("Total of your first half (%d) and your second half (%d) is %d", ifaaHunter[0], ifaaHunter[1], ifaaHunterTotal);

      break;
    }

    case 'c': // Fita Field Round
    {
      printf ("Please enter your Fita Field score > ");
      scanf (" %d", &fitaField[0]);

      printf ("Total of your Fita Field round is %d", fitaField[0]);

      break;
    }

    case 'd': // Field 3D Round
    {
      printf ("Please enter your 3D Field score > ");
      scanf (" %d", &field3D[0]);

      printf ("Total of your 3D Field round is %d", field3D[0]);

      break;
    }

    default:
    printf ("Please enter a valid response");
    break;
  }

  break; // Breaks out of Case D

}

case 'e': // Exits the program
{
  printf ("Thank you, Good bye!\n");
  return 0;
}

输出

Please select the type of round you shot

    (a) IFAA Field
    (b) IFAA Hunter
    (c) Fita Field
    (d) 3D Field
> e
Please enter a valid response
Hi s. c, Please choose from the following options by typing the letter and pressing the 'return' key

    (a) Enter Scored Practice logs
    (b) Enter Practice Arrow count
    (c) Enter Competition logs
    (d) Enter Field Competition Logs
    (e) Exit Program
> Thank you, Good bye!
        scanf (" %d", &fieldRound);

这个scanf失败了!它需要数字输入,而不是字母字符!

改变

        scanf (" %d", &fieldRound);

        scanf (" %c", &fieldRound);

在您当前的配置中,您的 scanf 需要一个十进制数,而不是一个字符。因此,如果您尝试输入一个字符,它就会失败。

此外,您需要将 int fieldRound; 更改为 char fieldRound;