在switch语句中使用输入default时,程序陷入无限循环

时间:2017-11-16 22:17:52

标签: c loops switch-statement infinite-loop

所以我目前在C中编写一个程序,使用switch语句可以允许输入字符串,然后根据您从菜单中选择的值确定如何处理它。

这是我的代码,

#include <stdio.h>


int main()
{
    int menu, end, i;
    char string[100];
    end = 0;

    do
    {
        printf("Welcome to the string operations program.\n\n");

        printf("1 - Enter a string\n");
        printf("2 - Display the message using the string\n");
        printf("3 - Count the number of characters in the string\n");
        printf("4 - Display the string backwards\n");
        printf("5 - Exit\n\n");

        printf("Option: ");
        scanf("%d", &menu);

        switch (menu)
        {
            case 1:
                printf("String: ");
                scanf("%s", string);
                break;

            case 2:
                printf("This is a message: Hello %s\n", string);
                break;

            case 3:
                printf("There are %d characters in %s\n", strlen(string), string);
                break;

            case 4:
                printf("string reversed gives: ");
                for (i = strlen(string) - 1; i >= 0; i--)
                    printf("%c", string[i]);
                printf("\n");
                break;

            case 5:
                printf("Exit");
                return 1;
                break;

            default:
                printf("Invalid input, try again.\n");
                break;


        }
    }while (end != 1);
    return 0;
}

当我通过1,2,3,4,5运行时似乎适用于所有事情,因为这是问题的要求。但是,当我输入一封信,例如&#39; t&#39;它按预期进入默认部分。当它进入它时会进入无限循环。

任何人都可以帮助我,并告诉我如何不让它成为一个无限循环,但只是让它回到开始,因为不允许用户输入?

2 个答案:

答案 0 :(得分:1)

发布的代码有几个问题:

  1. 未处理使用未输入字符串的情况
  2. 多次显示欢迎讯息
  3. 无法正确处理菜单选择5
  4. 不比较苹果与苹果的关系。
  5. 在尝试输入字符串
  6. 之前不会清空标准输入
  7. 不允许输入字符串中的任何空格
  8. ....
  9. 以下提议的代码:

    1. 纠正了上述所有问题
    2. 干净地编译
    3. 正确检查并报告错误
    4. 包含默认字符串,因此程序没有未定义的行为
    5. 包含所需的头文件和文档,以包含每个头文件的原因
    6. 现在建议的代码:

      #include <stdio.h>   // printf(), scanf(), fprintf()
      #include <stdlib.h>  // exit(), EXIT_FAILURE
      #include <string.h>  // strlen()
      
      
      int main()
      {
          int menu;
          int end;
         //int i;
          char string[100] = "no string entered";
          end = 0;
      
          printf("Welcome to the string operations program.\n\n");
      
          do
          {
              printf("1 - Enter a string\n");
              printf("2 - Display the message using the string\n");
              printf("3 - Count the number of characters in the string\n");
              printf("4 - Display the string backwards\n");
              printf("5 - Exit\n\n");
      
              printf("Option: ");
              if( 1 != scanf("%d", &menu) )
              {
                  fprintf( stderr, "scanf for menu selection failed\n" );
                  exit( EXIT_FAILURE );
              }
      
              // implied else, scanf successful
      
              switch (menu)
              {
                  case 1:
                      // note leading space in format string to consume leftover newline in 'stdin'
                      printf("String: ");
                      if( 1 != scanf(" %99[^\n]", string) )
                      {
                          fprintf( stderr, "scanf for input string failed\n" );
                          exit( EXIT_FAILURE );
                      }
      
                      // implied else, scanf successful
                      break;
      
                  case 2:
                      printf("This is the string entered: <%s>\n", string);
                      break;
      
                  case 3:
                      // note appropriate format specifier for `size_t` from `strlen()`
                      printf("There are %lu characters in <%s>\n", strlen(string), string);
                      break;
      
                  case 4:
                      printf("string reversed gives: ");
                      for (size_t i = strlen(string); i; i--)
                          printf("%c", string[i-1]);
      
                      printf("\n");
                      break;
      
                  case 5:
                      printf("Exit");
                      end = 1;
                      break;
      
                  default:
                      printf("Invalid input, try again.\n");
                      break;
              }
          } while (end != 1);
          return 0;
      }
      

      程序运行会产生以下输出:

      Welcome to the string operations program.
      
      1 - Enter a string
      2 - Display the message using the string
      3 - Count the number of characters in the string
      4 - Display the string backwards
      5 - Exit
      
      Option: 2
      This is the string entered: <no string entered>
      1 - Enter a string
      2 - Display the message using the string
      3 - Count the number of characters in the string
      4 - Display the string backwards
      5 - Exit
      
      Option: 1
      String: this is a string with spaces
      1 - Enter a string
      2 - Display the message using the string
      3 - Count the number of characters in the string
      4 - Display the string backwards
      5 - Exit
      
      Option: 2
      This is the string entered: <this is a string with spaces>
      1 - Enter a string
      2 - Display the message using the string
      3 - Count the number of characters in the string
      4 - Display the string backwards
      5 - Exit
      
      Option: 3
      There are 28 characters in <this is a string with spaces>
      1 - Enter a string
      2 - Display the message using the string
      3 - Count the number of characters in the string
      4 - Display the string backwards
      5 - Exit
      
      Option: 4
      string reversed gives: secaps htiw gnirts a si siht
      1 - Enter a string
      2 - Display the message using the string
      3 - Count the number of characters in the string
      4 - Display the string backwards
      5 - Exit
      
      Option: 6
      Invalid input, try again.
      1 - Enter a string
      2 - Display the message using the string
      3 - Count the number of characters in the string
      4 - Display the string backwards
      5 - Exit
      
      Option: 5
      Exit
      

答案 1 :(得分:0)

就像Anton提到的那样,问题是因为s​​canf声明。你可以添加一个getchar();休息之前;默认情况下:解决此问题。还有两个建议: *你可以在这里删除do-while循环,因为它没有特殊用途,最好使用while(1)循环。 *也是休息;不需要案例5中的陈述。