Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8219145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T13:03:33+00:00 2026-06-07T13:03:33+00:00

I am writing a command line C program that simulates the Monty Hall Problem

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T13:03:34+00:00Added an answer on June 7, 2026 at 1:03 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a program in Python that accepts command line arguments. I am parsing
I'm writing a ruby program that executes some external command-line utilities. How could I
I'm writing a program that takes a single command line argument. This argument needs
I'm writing a Perl program that will take a few command-line arguments (they'll actually
I'm currently writing a lightweight program that consolidates many command line and other external
If you are writing a program that is executable from the command line, you
I've written a simple program that captures and executes command line Python scripts, but
I'm writing a small command-line program that reads two floats, an int, and a
I am writing a program that takes one action if the command line contains
I'm writing a small command-line program that asks the user for polynomials in the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.