8 Replies - 759 Views - Last Post: 30 October 2016 - 11:23 AM Rate Topic: -----

#1 NemoNobody  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 29-April 16

What’s In the Box Game -- how to use multiple functions

Posted 29 October 2016 - 02:49 PM

Hi, I'm having trouble with how to use multiple functions, especially understanding the difference between Pass by value and pass by reference. Here is the assignment:

For this assignment write a C++ program where the computer is going to play a game with the user. In this game there are 3 boxes. One box has one millions dollars in it (or some other prize you want to give away) and the other 2 boxes have a red snapper fish (or some other losing prize that you want to give away) in it. The rules of the game are as follows:

1. The player will select a box (1, 2 or 3).
2. The computer selects a different box (not the one the player selected and not a winning box) and reveals it to the player.
3. The computer gives the player the option of switching from their original choice to the last box that has not been revealed.
4. The box that was finally picked by the player is what determines a win or loss.

To get full credit on this assignment you must have at least 2 functions. One function must use pass by value and one function must use pass by reference and you must use these properly. (Note: You can mix pass by value and pass by reference in one function and get full credit on this assignment.) You must validate the users input to make sure that they pick a valid box.


I am having trouble with steps 2 and 4. I don't know how to select the non-prize box (the one not chosen by the user and that is not the winning box) and display it for the user to see. In my pass by value function, it says the variables were not declared because I am using variables from the main() function. What should I do about this? In my pass by reference function, I create a random "winning" box, but it says that the variable is unused. The assignment says I can combine the two functions into one, should I do that instead?

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;
void WinNum(); //voiding both functions
void Messages();

int main()
{

int UserPick; //first user pick
int UserPick2; //second and final user pick

cout << "Welcome to the 'Choose A Prize In A Box' game! Here we have 3 boxes, one box has" << endl;
cout << "ONE MILLION DOLLARS in it! The other 2 boxes have a Red Snapper in them!" << endl << endl;
cout << "In which box do you think is the ONE MILLION DOLLARS?" << endl;
cout << "Box Number 1?" << endl;
cout << "Box Number 2?" << endl;
cout << "or Box Number 3?" << endl;

cout << "You will pick box number: ";
cin >> UserPick;

WinNum(); //call function for the winning number box

//FIGURE OUT THE LOSING BOX


cout << "You selected box number " << UserPick << " but let me show you what is in box number..." << endl; //SHOW USER A DIFFERENT BOX THAN THEY CHOSE
cout << "A Red Snapper!" << endl;

cout << "Now would you still like to take the prize in box number " << UserPick << " or would you like to" << endl;
cout << "change your mind and take the prize in box number ?" << endl; //AGAIN, SHOW THE OTHER BOX AS A OPTION

cout << "What box would you like to pick?/n";
cout << "Box Number " << UserPick << endl;
cout << "or Box Number " << endl; //OTHER OPTION BOX
cin >> UserPick2;

if (UserPick2< 1 || UserPick2 > 3) {
    cout << "/nInvalid box number. Please try again:\n";
    cout << "What box would you like to pick?/n";
    cout << "Box Number " << UserPick << endl;
    cout << "or Box Number " << endl;
    cin >> UserPick2;
}

Messages();

    system("pause");
    return 0;
}

//For pass by reference, you can create a function and in this function you will use 
//the random number generator to get the box number (1, 2 or 3) that the winning prize is in.
void WinNum()
	{
srand(time(NULL));

int WinNum = rand() % 3 + 1; //says variable is unused
}

//For the pass by value function you can create a function that figures out if the user has 
//won or lost, or just create a simple function that will print out the messages to the user.

void Messages()
	{
WinNum(); //to use the random winning number variable?
//UserPick and UserPick2 are not declared in this variable, what do I do for this?
if (UserPick2 == UserPick) {
	cout << "You decided not to change your mind!/n";
	}
else if (UserPick2 != UserPick) {
	cout << "You decided to change your mind!/n";
	}
	
if (UserPick2 != WinNum) {
	cout << "You have won a RED SNAPPER!!!/n/n";
	}
else if (UserPick2 == WinNum) {
    cout << "You have won ONE MILLION DOLLARS!!!/n/n";
	}
}


Is This A Good Question/Topic? 0
  • +

Replies To: What’s In the Box Game -- how to use multiple functions

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 6164
  • View blog
  • Posts: 21,248
  • Joined: 05-May 12

Re: What’s In the Box Game -- how to use multiple functions

Posted 29 October 2016 - 07:10 PM

Considering that your functions don't even take parameters, it's a bit hard for you to do functions that pass parameters by value and by reference. Did you even cover these types of parameters in your class lectures? Did you take notes? If not, what does your textbook have to say about functions and their parameters?
Was This Post Helpful? 0
  • +
  • -

#3 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7161
  • View blog
  • Posts: 14,927
  • Joined: 16-October 07

Re: What’s In the Box Game -- how to use multiple functions

Posted 30 October 2016 - 02:54 AM

I'd lay this out like so:
int getUserPick();
int getRevealPick(int);
bool getUserSwitch(int, int);
void showResults(int, int, bool);

int main() {
    // define here only
    srand(time(NULL));

    // 1. The player will select a box (1, 2 or 3).
    int userPick = getUserPick();

    // 2. The computer selects a different box (not the one the player selected and not a winning box) and reveals it to the player.
    int computerPick = getRevealPick(userPick);

    // 3. The computer gives the player the option of switching from their original choice to the last box that has not been revealed.
    bool userSwitch = getUserSwitch(userPick, computerPick);
    // 4. The box that was finally picked by the player is what determines a win or loss.
    showResults(userPick, computerPick, userSwitch);



For the curious requirement of pass by reference...
// load into passed var and return the value in var
int loadUserPick(int &);
void loadRevealPick(int &, int);
bool getUserSwitch(int, int);
void showResults(int, int, bool);

int main() {
    // define here only
    srand(time(NULL));

    // 1. The player will select a box (1, 2 or 3).
    // 2. The computer selects a different box (not the one the player selected and not a winning box) and reveals it to the player.
    int userPick, computerPick;
    getRevealPick(computerPick, loadUserPick(userPick));



Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#4 NemoNobody  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 29-April 16

Re: What’s In the Box Game -- how to use multiple functions

Posted 30 October 2016 - 09:45 AM

Are those global variables? I'm not allowed to have those. And I need to have multiple functions, I only see you using the main() function
Was This Post Helpful? 0
  • +
  • -

#5 Xupicor  Icon User is offline

  • Nasal Demon
  • member icon

Reputation: 457
  • View blog
  • Posts: 1,179
  • Joined: 31-May 11

Re: What’s In the Box Game -- how to use multiple functions

Posted 30 October 2016 - 09:57 AM

What global variables? There are none in baavgai's post. These are function declarations - you are to fill in the definitions.
Was This Post Helpful? 0
  • +
  • -

#6 NemoNobody  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 29-April 16

Re: What’s In the Box Game -- how to use multiple functions

Posted 30 October 2016 - 10:40 AM

So each of those are functions?
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon

Reputation: 5466
  • View blog
  • Posts: 17,018
  • Joined: 25-December 09

Re: What’s In the Box Game -- how to use multiple functions

Posted 30 October 2016 - 10:42 AM

You may want to study the function tutorial contained in my signature below.


Jim
Was This Post Helpful? 1
  • +
  • -

#8 baavgai  Icon User is offline

  • Dreaming Coder
  • member icon


Reputation: 7161
  • View blog
  • Posts: 14,927
  • Joined: 16-October 07

Re: What’s In the Box Game -- how to use multiple functions

Posted 30 October 2016 - 11:05 AM

So, a quick example.
#include <iostream>

// this is a function declaration
// you've already used this with WinNum and Messages in the first post
int getUserPick(); // <-- the ); is a strong indication this is function declaration

int main() {
    int userPick = getUserPick();

    std::cout << "userPick = " << userPick << std::endl;

    return 0;
}

using namespace std;

int getUserPick() {
    cout << "Welcome to the 'Choose A Prize In A Box' game! Here we have 3 boxes, one box has" << endl;
    cout << "ONE MILLION DOLLARS in it! The other 2 boxes have a Red Snapper in them!" << endl << endl;
    cout << "In which box do you think is the ONE MILLION DOLLARS?" << endl;
    cout << "Box Number 1?" << endl;
    cout << "Box Number 2?" << endl;
    cout << "or Box Number 3?" << endl;

    cout << "You will pick box number: ";
    int n;
    cin >> n;
    return n;
}



Alternately,
[code]
// the reference thingy
void loadUserPick(int &);

int main() {
    int userPick;
    loadUserPick(userPick);

    std::cout << "userPick = " << userPick << std::endl;

    return 0;
}

void loadUserPick(int &n) {
// ...
    std::cout << "You will pick box number: ";
    // int n; note, I've passed this value
    std::cin >> n; // this value will be sent back
    // return n;
}



Note, we have no bounds checking in any code. I'd want to seem something that validated if my user entered 1 2 or 3 and asked them to try again if they didn't. You sort of do this for UserPick2 in your original.

Hope this helps.
Was This Post Helpful? 0
  • +
  • -

#9 NemoNobody  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 29-April 16

Re: What’s In the Box Game -- how to use multiple functions

Posted 30 October 2016 - 11:23 AM

Thanks to baavgai, I finally was able to figure out most of it. Now, it all works except for displaying the 3rd, losing box.

#include <iostream>
#include <ctime>
#include <cstdlib>

using namespace std;
int WinNum(); //voiding both functions
int getUserPick();


//For pass by reference, you can create a function and in this function you will use 
//the random number generator to get the box number (1, 2 or 3) that the winning prize is in.
int WinNum()
	{
return rand() % 3 + 1;
}
int getUserPick() {
    cout << "Welcome to the 'Choose A Prize In A Box' game! Here we have 3 boxes, one box has" << endl;
    cout << "ONE MILLION DOLLARS in it! The other 2 boxes have a Red Snapper in them!" << endl << endl;
    cout << "In which box do you think is the ONE MILLION DOLLARS?" << endl;
    cout << "Box Number 1?" << endl;
    cout << "Box Number 2?" << endl;
    cout << "or Box Number 3?" << endl;
    cout << "You will pick box number: ";
    int n;
    cin >> n;
    return n;
}


int main()
{
    
srand(time(NULL));

int UserPick2; //second and final user pick
int userPick = getUserPick();
//FIGURE OUT THE LOSING BOX
int WinningNum = WinNum(); //call function for the winning number box


cout << "You selected box number " << userPick << " but let me show you what is in box number..." << endl; //SHOW USER A DIFFERENT BOX THAN THEY CHOSE
cout << "A Red Snapper!" << endl << endl;

cout << "Now would you still like to take the prize in box number " << userPick << " or would you like to" << endl;
cout << "change your mind and take the prize in box number ?" << endl; //AGAIN, SHOW THE OTHER BOX AS A OPTION

cout << "What box would you like to pick?\n";
cout << "Box Number " << userPick << " or Box Number " << endl; //OTHER OPTION BOX
cin >> UserPick2;

if (UserPick2 < 1 || UserPick2 > 3) {
    cout << "/nInvalid box number. Please try again:\n";
    cout << "What box would you like to pick?\n";
    cout << "Box Number " << userPick << "or Box Number " << endl;
    cin >> UserPick2;
}

if (UserPick2 == userPick) {
	cout << "You decided not to change your mind!\n";
	}
else if (UserPick2 != userPick) {
	cout << "You decided to change your mind!\n";
	}
	
if (UserPick2 != WinningNum) {
	cout << "You have won a RED SNAPPER!!!\n\n";
	}
else if (UserPick2 == WinningNum) {
  cout << "You have won ONE MILLION DOLLARS!!!\n\n";
}

    system("pause");
    return 0;
}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1