简体   繁体   中英

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:

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. 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.

You're shadowing valid_input from within the while loop scope:

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. So, the valid_input your loop checks for actually never changes.

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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