hi, I am trying to write a program and the first step is to set a variable to a random no btwn 1 and 3. I seeded the random function with the system time and used this code
srand(time(0));
for (int i = 0; i < 1; i++)
{
int my_car = (rand() % 3 ) + 1;
}
it does not work and sets the variable to another random no 4177945, which I believe may have been put into that memory bit by another program I ran or is just another random number. Anybody has help for me on this?
Thanks.
Well, I don't see any problems....except your strange for loop that only runs once...it SHOULD work. If you are using VC++ or something that has an intergrated debugger, try stepping through the code and seeing what happens.
I took out the for loop. I am also using Dev C++ from bloodshed.
The whole problem I am trying to solve is below, followed by the code I have written so far. Can you take a look at the code and tell me what is wrong with it. Any help will be really appreciated.
problem:
/*Introduction (from Wikipedia)
The Monty Hall Problem is a probability puzzle loosely based on the American television game show Let's Make a Deal
(the name comes from the show's host, Monty Hall).
A well-known statement of the problem was published in Parade magazine in 1990:
Suppose you're on a game show, and you're given the choice of three doors: Behind one door is a car; behind the others, goats.
You pick a door, say #1, and the host, who knows what's behind the doors, opens another door, say #3, which has a goat.
He then says to you, "Do you want to pick door #2?" Is it to your advantage to switch your choice of doors?
What are the odds of winning the car if you switch doors vs. the odds if you stick with your original choice?
Write a program to experiment with the Monty Hall problem Your program should allow the user to play the whole game.
Playing the game
If the user chooses to play the game him/herself, your program should randomly determine which door the car is behind,
and then assume the role of the game show host Monty while the user assumes the role of the contestant.
The user picks a door (1, 2, or 3), and then Monty opens a different door to reveal a goat.
At this point the user may choose either to stick with his/her original door, or to choose the other remaining closed door.
Your program should then indicate whether the user won or lost the game: i.e.
whether the user's final door selection was the door concealing the car or the door concealing the other goat.
Finally, your program should allow the user to play again if he/she wants to.
Remember that Monty knows exactly where the car is, and will NEVER open that door!
However, if the contestant happens to pick that door first,
then Monty has to make a decision about which of the remaining two doors to open.
His best strategy in this case is to choose randomly in order to avoid giving out any extra information to the contestant.
void get_input(int& choice);
//Requires a nonnegetive integer between 1 and 3 from the user as his/her choice.
//Stores the input value for the choice of user.
int play_game(int& choice, int& goat_door1, int& goat_door2);
//Requires a choice from the user.
//Allows the user to play the game, and gives user the option to switch doors.
int random(int& car_door, int& goat_door1, int& goat_door2);
//Sets a variable, car_door to any random no between 1 and 3, and then two variables,
//goat_door1, goat_door2 to the two other numbers left between 1 and 3 after the
//random number is set
void show_results(char& stay);
//Requires user to play the game and make a decision to switch or stay.
//Shows the results of the game, ie whether user wins or loses in either case of switching or staying.
int main()
{
int choice, car_door, goat_door1, goat_door2, no_of_simulations;
int switch_wins, stay_wins;
char play_again, stay;
// initialize the pseudorandom number generator by seeding it with the current time
srand(time(0));
cout << "Welcome to the LET'S MAKE A DEAL Game Show." << endl <<
"The game plan is to try to win a car hidden behind one of three doors,1, 2 and 3." <<
"Behind the other two doors are goats. You must pick a door you believe the car is hidden behind." << endl;
do {
//Computer sets pseudorandom number to any integer between 1 and 3:\n";
cout << "Do you want to play another game?.\n" <<
"Please enter y or Y for yes and n or N for no.\n";
cin >> play_again;
} while (!(play_again == 'n' || play_again == 'N'));
system ("Pause");
return 0;
}
void get_input(int& choice)
{
cout << "Please enter a positive integer for your choice of door.\n" <<
"Enter 1 for door #1, 2 for door #2 or 3 for door #3.\n";
cin >> choice;
cout << "You chose door " << choice << endl;
while (choice <= 0 || choice > 3)
{
cout << "This is not a valid input.\n" <<
"Please enter number between 1 and 3 as your choice.\n";
cin >> choice;
}
}
int play_game(int& choice, int& goat_door1, int& goat_door2)
{
int car_door, open;
char trade, stay;
cout << "You have chosen door number " << choice << endl;
//Computer sets pseudorandom number to any integer between 1 and 3:\n";
if (choice == car_door)
{
{
open = (rand() % 2 ) + 1;
}
if (open == 1)
{
cout << "Door number " << goat_door1 << " has a goat behind it.\n" <<
"Do you want to switch doors?\n";
cout << "Enter y or Y for yes and n or N for no.\n";
}
else
{
cout << "Door number " << goat_door2 << " has a goat behind it.\n" <<
"Do you want to switch doors?\n";
cout << "Enter y or Y for yes and n or N for no.\n";
}
cin >> trade;
{
bool go_on;
while (go_on)
switch (trade)
{
case 'N':
case 'n':
stay = 'T';
go_on = false;
break;
case 'Y':
case 'y':
cout << "You decided to switch doors.\n";
if (open == 1)
choice = 3;
else
choice = 2;
stay = 'F';
go_on = false;
break;
default:
cout << "That is not a possible choice. Please enter your choice again.\n";
cin >> choice;
break;
}
}
}
void show_results(char& stay)
{
int choice, car_door;
if (stay == 'T')
{
cout << "Your first choice was door number" << choice << " and you chose not to switch.";
if (choice == car_door)
cout << "You won! The car was behind Door number " << choice << endl;
else
cout << "You lost! The car was behind Door number " << car_door << endl;
}
else
{
cout << "You chose to switch Doors." << endl;
if (choice == car_door)
cout << "You won! The car was behind Door number " << choice << endl;
else
cout << "You lost! The car was behind Door number " << car_door << endl;
}
You can't do that. You can only return one value from a function.
expr {, expr ... } evaluates to the value of the left-most expression, so random() returns car_door. (Which actually doesn't matter because your code ignores the return code anyway).
In your original post, I see that you actually declare the variable inside the for loop. Were you also declaring the variable in the outer scope? This could very well explain the behavior you saw, assuming that you printed the value outside the for loop.
(To explain what I mean, consider this code:)
1 2 3 4 5 6 7 8
int main() {
int x = 0;
for( int y = 1; y < 3; ++y ) {
int x = y * 2;
cout << x << ' ';
}
cout << x << ' ';
}
This code outputs 2 4 0
Note that int x is declared two places -- once at the top of main and once inside the for loop. This is called shadowing a variable. That is, all references to x within the for loop refer to the x declared inside the for loop, and all references to x outside the for loop refer to the x declared at the top of main.
[Note: shadowing variables/parameters is never a good thing.]
Thanks Jsmith. I took out the for loop now and made the change to the return expression. You said the random() function returns car_door, but my code ignores it. I believe so cos when I print out car door in the random function, it works fine, but when I call it in the main funtion, it gives me a different value. I think my get_input function does the same thing cos, it returns an unknown value for choicein the main function even though, it prints the correct value in the input function. Can you tell me why and how to fix it?
Thanks, but I still cant get how to get my function to return the values I need and to be able to call them in other functions like the play_game function.
int random(int& car_door, int& goat_door1, int& goat_door2);
ie, all three parameters are references. This means that if random() modifies the values of one or more of these parameters, the corresponding actual parameter in the caller is also modified. For example
1 2 3 4 5
int x, y, z;
random( x, y, z );
// x is now whatever random set its car_door value to;
// y is now whatever random set its goat_door1 value to;
// z ... ditto.
It looks like random modifies the values of all three in every case, so
in main where you call:
random(car_door, goat_door1, goat_door2);
The values of car_door, goat_door1, and goat_door2 should be equal to the values assigned to the corresponding parameters by random. Is this not what you are seeing?