I'm starting to learn programming and am working with c and conditional loops right now. And I wanted to make an interactive simulation of the Monty hall car behind doors problem(One of the 3 doors has a car behind it, the other 2 have goats. After the used picks one, Monty reveals one of the others with the goat behind it and allows the user to switch to the other door). According to what I've read from C Programming a modern approach by K.N. King, this should be possible with a few functions, conditionals and loops. But The code I've written loops infinitely in one of my functions and I'm not sure why, here's my code:
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
// A function that gets a valid door input from the user
int chooseDoor(){
int choice = 0;
while(choice < 1 || choice > 3){
printf("Choose a door from 1 to 3: ");
scanf("%d",&choice);
printf("%d\n", choice);
}
return choice;
}
// A function to get a valid yes or no response from the user
int validateYn(){
char yn[1];
fgets(yn,1,stdin);
while(yn[0] != "Y" && yn[0] != "y" && yn[0] != "N" && yn[0] != "n"){
printf("Error: Please enter Y or N: ");
fgets(yn,1,stdin);}
if(yn[0] == "Y" || yn[0] == "y"){
return 1;}
else{
return 0;}}
int main(){
bool play = true; //conditional boolean to repeat the game
while(play){
int car, shown, other, choice; //initialisers for the door with the car, door that monty will reveal, the remaining door and door chosen by player
bool swap;
car = rand()%3; //select a random door between 1 and 3
choice = chooseDoor(); //call function to get user input of chosen door
printf("You picked door #%d", choice);
// A loop to find the lowest door that is not picked by the user and has a goat behind it
for(int i = 0; i<3; ++i){
if( i == car || i == choice){
continue;}
shown = i;
break;}
// A loop to find the remaining door besides the one revealed and the one user picks
for(int i = 0; i<3; ++i){
if( i == car || i == choice || i == shown){
continue;}
other = i;
break;}
printf("Monty opens door #%d, there's a goat!\nWould you like to switch to door #%d?", shown, other);
swap = validateYn();
// Change user choice if user says yes to swap
if(swap){
choice = other;}
// win if user choice had car, lose otherwise
if(choice == car){
printf("Monty opens door #%d, it's the car! You win!", car);}
else{
printf("Monty opens door #%d, there's a goat. You lose.", choice);}
}
printf("Would you like to play again?");
play = validateYn();
return 0;}
My friend also told me that a switch statement should make things easier, but I have no idea how to use them right, thanks