Hello All,

I am currently a beginner C++ programmer.

Here I have a program that simulates the "Monty Hall" problem. At first, the program prompted the user to choose what door they wanted to pick, and then went from there.

I restructured it so that the computer simulates everything by itself and runs 10,000 times. However, as you will see in the code, the last part of my main function has variables that are not defined outside of the for loop.

So, my question is, how do I forward those 4 variables out of the for loop so that I can use them to output the final results? I feel like this is a simple problem, but for whatever reason I cannot wrap my head around the answer.

Ideally, the final result will look something like:
"After running the program 10,000 times, the final results were:

x wins out of x stays.

x wins out of x switches."

Thank you for any help provided. I will answer any necessary questions as best as I can. I apologize in advance for any code that is messy, inefficient, or otherwise unsatisfactory. As I stated I am a beginning programmer who is still trying to wrap his head around functions.



Code:
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;

int getUserChoice ();

int determinePrizeLocation ();

int getFinalChoice (int firstChoice, int grandPrize);

bool getSwitchOrStay (int firstChoice, int finalChoice);

void determineOutcomeStay (int finalChoice, int grandPrize, int winStay, int numOfStay);

void determineOutcomeSwitch (int finalChoice, int grandPrize, int winStay, int numOfSwitch);

void outputFinalResult (int winStay, int winSwitch, int numOfStay, int numOfSwitch);

int main ()
{
	for (int i = 0; i > 10000; i++) 
	{
		srand (time(0));

		int firstChoice;

		int grandPrize;

		int finalChoice;

		int winStay = 0;

		int winSwitch = 0;

		int numOfStay = 0;

		int numOfSwitch = 0;

		firstChoice = getUserChoice ();

		grandPrize = determinePrizeLocation ();

		finalChoice = getFinalChoice (firstChoice, grandPrize);

		bool switchOrStay = getSwitchOrStay (firstChoice, finalChoice);

		if (switchOrStay == true)
			determineOutcomeStay (finalChoice, grandPrize, winStay, numOfStay);

		else
			determineOutcomeSwitch (finalChoice, grandPrize, winSwitch, numOfSwitch);
	}

	outputFinalResult (winStay, winSwitch, numOfStay, numOfSwitch);

	return 0;
}

int getUserChoice ()
{
	return rand () % 3 + 1; // Random first choice
}

int determinePrizeLocation ()
{
	int grandPrize = rand () % 3 + 1; // Grand prize is placed randomly

	return grandPrize;
}

int getFinalChoice (int firstChoice, int grandPrize)
{
	int revealedDoor = rand () % 3 + 1; // Random number btw 1-3

	int finalChoice;

	while (revealedDoor == grandPrize || revealedDoor == firstChoice) // Reveal whichever door isn't the grand prize or the door user selected
	{
		revealedDoor = rand () % 3 + 1;
	}
	
	int switchProbability = rand () % 2 + 1; // 50% chance of keeping or switching choice

	if (switchProbability == 1) // Represents user keeping their choice
	{
		return firstChoice;
	}

	else if (switchProbability == 2) // Represents user switching their choice
	{
		finalChoice = rand () % 3 + 1; // Randomizes finalChoice btw 1-3

		while (finalChoice == revealedDoor || finalChoice == firstChoice) // Ensures that finalChoice isn't the door that was eliminated or
		{                                                                 // the door that was initially selected
			finalChoice = rand () % 3 + 1;
		}
	
		return finalChoice;
	}
}

bool getSwitchOrStay (int firstChoice, int finalChoice)
{
	if (finalChoice == firstChoice)
		return true;

	else
		return false;
}

void determineOutcomeStay (int choice, int grandPrize, int winStay, int numOfStay)
{
	if (choice == grandPrize)
	{
		winStay++; // Increments winStay by 1 if user won by staying with their first choice	
	}

	numOfStay++;
}

void determineOutcomeSwitch (int choice, int grandPrize, int winSwitch, int numOfSwitch)
{
	if (choice == grandPrize)
	{
		winSwitch++; // Increments winSwitch by 1 if user won by switching their first choice
	}

	numOfSwitch++;
}

void outputFinalResult (int winStay, int winSwitch, int numOfStay, int numOfSwitch)
{
	cout << "\nAfter running the program 10,000 times, the final results were:" << endl;
	cout << "\n" << winStay << " wins out of " << numOfStay << " stays." << endl;
	cout << "\n" << winSwitch << " wins out of " << numOfSwitch << " switches." << endl;
}