简体   繁体   English

为什么在while循环后此C程序不起作用?

[英]Why does this C program not work after the while loop?

I am writing a command line C program that simulates the Monty Hall Problem , but I am having trouble with a particular section of my code, where the program simply prompts the user to input the number door they would like to open, it gets the input and makes sure it is valid: 我正在编写一个模拟Monty Hall问题的命令行C程序,但是我在代码的特定部分遇到了麻烦,该程序只是提示用户输入他们想打开的数字门,它就可以输入并确保它是有效的:

printf("Please enter the door you would like to choose! (Door 1, 2 or 3)\n\nDoor ");

char init_input[255];

int selection;
int valid_input = 0;
while(valid_input == 0)
{   
    gets(init_input);
    int len = strlen(init_input);
    while(len != 1)
    {
        printf("Please choose either door 1, 2 or 3\n\n");
        printf("Door ");
        gets(init_input);
        len = strlen(init_input);
    }
    int valid_input = 0;
    char input = init_input[0];
    switch(input)
    {
        case('1'):
        {
            selection = 1;
            valid_input = 1;
            printf("Door 1\n");
            break;
        }
        case('2'):
        {
            selection = 2;
            valid_input = 1;
            printf("Door 2\n");
            break;

        }
        case('3'):
        {
            selection = 3;
            valid_input = 1;
            printf("Door 3\n");
            break;


        }
        default:
        {
            printf("\nPlease choose either door 1, 2 or 3\n\nDoor ");
            break;
        }
    }
}

printf("\nYou chose Door %d, now I will reveal one of the doors that has a goat behind it:\n\n", selection);

The program works fine until you input any of the valid door numbers: 1, 2 or 3, it doesn't crash but doesn't print the desired output after the while loop and continue with the program. 该程序可以正常工作,直到您输入任何有效的门号:1、2或3,它不会崩溃,但是在while循环后不会打印所需的输出并继续执行该程序。 However, the name of the door chosen is printed when I input a valid number, which suggests that it isn't anything to do with the switch statement. 但是,当我输入有效数字时,会打印所选门的名称,这表明它与switch语句无关。

You're shadowing valid_input from within the while loop scope: 您正在while循环范围内隐藏有效valid_input

int valid_input = 0;
while (valid_input == 0) {
    // ...
    int valid_input = 0;     // remove this
}

When you write to or read from valid_input in the loop, you are reading from the one declared in the loop, not outside of it. 当您在循环中对valid_input进行写入或读取时,您正在读取的是在循环中声明的内容,而不是在其外部。 So, the valid_input your loop checks for actually never changes. 因此,循环检查的valid_input实际上永远不会更改。

If you want to program, you absolutely positively should learn to use a debugger. 如果要编程,绝对应该积极学习使用调试器。 Most compilers have a reasonably nice one these days. 如今,大多数编译器的性能都不错。 Step through your code. 单步执行代码。 You should be able to determine where it goes wrong. 您应该能够确定哪里出了问题。 And you might learn a few other unexpected things about your code as well. 而且您可能还会学到一些其他有关代码的意外情况。

I can't immediately see why the loop wouldn't exit given input of a valid door number. 我无法立即看到为什么在输入有效门号的情况下循环不会退出。 But, then again, I'm not 100% sure exactly where to look. 但是,话又说回来,我不确定百分百知道在哪里。

(BTW, I've created test programs of the "Monty Hall problem". It's true and here's why: By revealing one of the doors, you are provided additional information about the door you did not choose. However, since they will never reveal the door you originally chose, you do not learn anything about that door. By switching, your odds of winning become 1/2 instead of 1/3.) (顺便说一句,我已经创建了“蒙蒂霍尔问题”的测试程序。这是真的,原因如下:通过显示其中一扇门,可以为您提供有关您未选择的门的更多信息。但是,由于它们永远不会显示您最初选择的那扇门,您对那扇门一无所知。通过切换,您获胜的几率变成1/2而不是1/3。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM