c - 在 switch 语句中使用输入默认值时,程序陷入无限循环

标签 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 时,它似乎适用于所有情况,因为这是问题的要求。但是,当我输入一个字母(例如“t”)时,它会按预期转到默认部分。当它进入这个状态时,它就会进入无限循环。

任何人都可以帮助我并向我展示如何不使其成为无限循环,而只是让它返回到开头,因为不允许用户输入?

最佳答案

发布的代码存在几个问题:

  1. 不处理用户未输入字符串的情况
  2. 多次显示欢迎消息
  3. 无法正确处理菜单选择 5
  4. 不会“同类比较”
  5. 在尝试输入字符串之前不清空标准输入
  6. 输入字符串中不允许有任何空格
  7. ...

以下建议代码:

  1. 更正了上面列出的所有问题
  2. 干净地编译
  3. 正确检查并报告错误
  4. 包含默认字符串,因此程序不会有未定义的行为
  5. 包含所需的头文件以及包含每个头文件的原因的文档

现在建议的代码:

#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

关于c - 在 switch 语句中使用输入默认值时,程序陷入无限循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47340048/

相关文章:

javascript - 似乎无法将每个单词的第一个字母大写

Python替换二维数组中的元素=键

loops - 解释如何矢量化循环

c - 字符串反转 C 中的逻辑错误

C: 在 linux 中发送 http get 请求

c - 哪种查找方法对于简单的整数到字符串查找最有效

java - 在 Android 应用程序中切换基于关闭对话框按钮选择的开关

Java Switch 语句直接使用数组默认

java - 如果输入了选项之外的内容,如何重复 if 或 switch 语句?

c - 如何判断数组的某个位置是否为空?我遇到错误 :invalid operands to binary