【问题标题】:Need to loop switch statement until 1 or 2 is entered需要循环 switch 语句直到输入 1 或 2
【发布时间】:2020-09-26 18:52:42
【问题描述】:

我需要循环 switch 语句直到输入 1 或 2。任何帮助都会很好或示例代码。我尝试使用 while 循环,但它一直在崩溃。我也尝试了一个do while,它变成了一个无限循环,所以我一定是做错了什么。

int main (void)
{
    int choice; // choice to run program from start menu or quit
    
    while (1)
    {
        // Menu for entering resistor and capacitor values
        printf("Welcome to the 555 Timer Frequency and Duty Cycle Calculator\n");
        printf("Please enter two resistor values in between ");
        printf("1 kOhms and 100 kOhms and a capacitor value\n\n");
 
        //Start of menu 
        printf("Menu\n\n");
        printf("1. Continue\n");
        printf("2. Exit\n");

        scanf("%d", &choice); // User inputs value


        switch (choice)
        {
        case 1:
            resistor(); // Program resistor is run if 1 is input
            break;
        case 2:
            printf("Goodbye."); // Program ends if 2 is input
            break; // break is used to exit while loop
        default:
            printf("Sorry I do not understand\n\n"); 
            fflush(stdin);  /* clears input buffer to allow user to input a new value if anything other
                               than 1 or 2 is input into scanf  */
        }

        break;
    }

    return 0;
}

【问题讨论】:

  • fflush(stdin); 为什么?您只需要刷新输出缓冲区
  • 我该怎么做?
  • 通过 stdout 而不是标准输入。但我没有看到崩溃godbolt.org/z/cnfv5W
  • 我需要刷新我认为是标准输入的用户输入的选择?此外,每当输入除 1 或 2 以外的整数或字符时,它都会变成无限循环,我需要它来打印语句“对不起,我不明白”,然后重新运行菜单
  • fflush(stdin) 是未定义的行为,应该避免。

标签: c


【解决方案1】:

问题是你的“while”末尾有一个“break”,所以它只执行一次。

自从我用 C 编写程序以来已经有很长时间了,但如果我没记错的话,你应该避免使用“fflush()”,因为它具有未定义的行为。相反,您可以将“scanf()”更改为:

scanf(" %d", &choice); // User inputs value

这样,在拥有更简洁的代码的同时,您可以获得与“fflush()”相同的假定效果。

【讨论】:

    【解决方案2】:

    您可以使用此代码

    for(;;) {
        // Menu for entering resistor and capacitor values
    
        // Start of menu
    
        // User inputs value
    
        if(choice == 1) { ... break; }
        else if(choice == 2) { ... return 0; } // If you use the break,the for loop will be stopped or You can use return 0; to return the whole function
        else { ... break; }
    }
    

    或者,您也可以使用此代码

    for(;;) {
        switch(choice ) {
            case 1: ... break;
            case 2: ... return 0; // If you use the break, only switch statement is stopped. You can use return 0; to return the whole function
     
            default: ... break;
        }
    }
    

    编辑:

    #include <stdio.h>
    
    int main (void)
     {
    int choice; // choice to run program from start menu or quit
    
    // Menu for entering resistor and capacitor values
        printf("Welcome to the 555 Timer Frequency and Duty Cycle Calculator\n");
        printf("Please enter two resistor values in between ");
        printf("1 kOhms and 100 kOhms and a capacitor value\n\n");
    
    while (1)
    {
        
        //Start of menu 
        printf("Menu\n");
        printf("1. Continue\n");
        printf("2. Exit\n");
        
        scanf("%d", &choice); // User inputs value
        
        
        switch (choice)
            {
                case 1: // Program resistor is run if 1 is input
                    break;
                case 2: printf("Goodbye."); // Program ends if 2 is input
                     return 0; // return 0; is used to exit while loop
                    
                default: printf("Sorry I do not understand\nStart Again !\n"); 
                    // fflush(stdin);  /* clears input buffer to allow user to input a new value if anything other than 1 or 2 is input into scanf     */
                    
                    
                    break;
                    
            }
            
            printf("\n");
    
        
    }
    
    return 0;
    }
    

    【讨论】:

    • @ElliottYeah 我同意你的看法。但是我已经习惯了像上面那样使用它:)
    • for (;;) 是指定无限循环的传统方式,所以我也使用它。但这只是个人喜好问题。
    • 哎呀!!!!我的错。非常感谢@AndreasWenzel 让我知道。我忘记了,我会改代码
    猜你喜欢
    • 2019-11-07
    • 1970-01-01
    • 2018-08-23
    • 2018-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-14
    相关资源
    最近更新 更多