stuck with the functions

Help! i can't seem to figure out how to call the function results(); am doing something wrong here?


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


int user_Choice;
int pc_Choice;

using namespace std;
//Function prototype

void userWin()
{
cout << "\n\t\t*****Congratulation! You Win******\n";
}
//Function prototype
void userLose()
{
cout << "\n\t\t*****Ooops! You Lose******\n";
}
//Function prototype
void userTie()
{
cout << "\n\t\t****The game is tie!*****\n";
}

void displayMenu();
void userChoice();
void computerChoice();
void results();




int main()
{
displayMenu();
cin >> user_Choice;
userChoice();
computerChoice();
results();
}

void displayMenu()
{
cout << "\t\t\t*****WELCOME TO YOUR FAVORITE GAME EVER!*****\n\n"
<< "\t\tPress [1] for Rock\n"
<< "\t\tPress [2] for Paper\n"
<< "\t\tPress [3] for Scissors\n"
<< "\t\tPress [4] to Quit The Program\n\n"
<< "\t\tEnter your choice: ";
}
void computerChoice()
{
srand((unsigned)time(0)); // random number range 1 through 3
int pc_Choice = (rand()%3) + 1;
if (pc_Choice == 1)
{
cout << "\t\tThe Computer picked " << pc_Choice << " for a Rock!\n";
}
else if (pc_Choice == 2)
{
cout << "\t\tThe Computer picked " << pc_Choice << " for a Paper!\n";
}
else if (pc_Choice == 3)
{
cout << "\t\tThe Computer picked " << pc_Choice << " for Scissors!\n";
}
}
void userChoice()
{
{
if (user_Choice == 1)
{
cout << "\t\tYou've selected " << user_Choice << " for a Rock!\n";
}
else if (user_Choice == 2)
{
cout << "\t\tYou've selected "<< user_Choice << " for a Paper!\n";
}
else if (user_Choice == 3)
{
cout << "\t\tYou've selected " << user_Choice << " for Scissors!\n";
}
else
{
cout << "\t\tInvalid Choice!\n";
}
}
}
void results()
{
if (user_Choice == pc_Choice)
userTie();
if (user_Choice == 1)
{
if (pc_Choice == 2)
{
userWin();
}

else if (user_Choice == 3)
{
userWin();
}
}
else if (user_Choice == 2)
{
if (pc_Choice == 1)
{
userWin();
}
else if (pc_Choice == 3)
{
userLose();
}
}
else if (user_Choice == 3)
{
if (pc_Choice == 1)
{
userLose();
}
else if (pc_Choice == 2)
{
userLose();
}
}

}
Hello hsv8962,

PLEASE ALWAYS USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/
Hint: You can edit your post, highlight your code and press the <> formatting button.
You can use the preview button at the bottom to see how it looks.

After using namespace std; your code is:

1
2
3
4
5
6
//Function prototype

void userWin()
{
cout << "\n\t\t*****Congratulation! You Win******\n";
}


This is a function definition not a function prototype as you state.

These are function prototypes:

1
2
3
4
void displayMenu();
void userChoice();
void computerChoice();
void results();


I do not see anything wrong with "main" for now other than the return 0;at the end is missing. Can do without, but good coding to have it there.

Sorry it will not be a quick answer as I will need some time to check out the whole program.

Hope that helps,

Andy
Hello hsv8962,

Working with the program I found that the "if" statements in the "results" function did not work correctly.

1
2
3
4
if (user_Choice == pc_Choice)
	userTie();
if (user_Choice == 1)
{


This should be an "else if" not two separate "if" statements. If it is a tie all other "if else if" statements would be checked and a wrong answer could be output. Then the nested "if else if" statements can produce the wrong answer.

I came up with this to correct the problem. See what you think:

1
2
3
4
5
6
7
8
9
10
11
void results()
{
	if (user_Choice == pc_Choice)
		userTie();
	else if (user_Choice == 1)
		pc_Choice == 2 ? userLose() : userWin();
	else if (user_Choice == 2)
		pc_Choice == 1 ? userWin() : userLose();
	else if (user_Choice == 3)
		pc_Choice == 1 ? userLose() : userWin();
}  //  End function Results 


This worked for all the testing that I did.

Other than a "do while" loop in main and adjusting the output display, putting some blank lines between parts, I did not find any problems with the way the program works.

Hope that helps,

Andy

P.S. You need a choice for four in the function "userChoice()".
Last edited on
Help! on this matter. i tried the option for when the two players are tie and then they need to play the game again, but couldn't figure out how to get it done.


//This program lets the user play the game Rock, Paper, Scissors against the computer.
//The user picks a corresponding number from the main menu.
//A winner is selected using the following rules:
//(1) If one player chooses rock and the other player chooses scissors ----> ROCK WINS!
//(2)- If one player chooses scissors and the other player chooses paper -----> SCISSORS WINS!
//(3)- If one player chooses paper and the other player chooses rock -----> PAPER WINS!
//(4)- If both players make the same choice, the game must be played again to determine the winner.
#include <iostream>
#include <cstdlib>
#include <ctime>

using namespace std;


//Global Variable
int userChoice;

//Function Prototype
void displayMenu();
void user_Choose();
void userWin1();
void userWin1();
void userWin1();
void userTie();
void game_Rules();

int main()
{
do
{
displayMenu();
user_Choose();
game_Rules();
}while (userChoice != 4);

return 0;
}

//Function Definitions

//Main Menu
void displayMenu()
{
cout<< "\n\n\t\t *****WELCOME TO YOUR FAVORITE GAME!*****"
<< "\n\t\t\t*****ROCK, PAPER & SCISSOR*****\n\n"
<< "\n\t\t---MAIN MENU---\n"
<< "\t\tPress [1] for Rock\n"
<< "\t\tPress [2] for Paper\n"
<< "\t\tPress [3] for Scissors\n"
<< "\t\tPress [4] to Quit The Program\n\n"
<< "\t\tEnter your choice: ";
cin >> userChoice;
}

//User's choice
void user_Choose()
{
if (userChoice == 1)
{
cout << "\n\t\t*****You picked rock*****";
}
else if (userChoice== 2)
{
cout << "\n\t\t*****You picked paper*****";
}
else if (userChoice== 3)
{
cout << "\n\t\t*****You picked scissor*****";
}
}

//Print result messages
void userWin1()
{
cout << "\n\t\t*****The rock smashes the scissors!******";
}

//Print result messages
void userWin2()
{
cout << "\n\t\t*****Scissors cuts the paper!******";
}

//Print result messages
void userWin3()
{
cout << "\n\t\t*****Paper wraps rock!******";
}

//Print result messages
void userTie()
{
cout << "\n\t\t****The game is tie!*****";
}

//Game rules
void game_Rules()
{
srand((unsigned)time(0));
int compchoice = (rand()%3)+1;

//Rule 1: if the user pick option 1 (ROCK)
if (userChoice==1)
{
if (compchoice==1)
{
cout<<"\n\t\t*****Computer chose rock*****";
userTie();
}
else if (compchoice == 2)
{ cout<<"\n\t\t*****Computer chose paper*****";
userWin3();
}
else if (compchoice == 3)
{
cout<<"\n\t\t*****Computer chose scissors*****";
userWin1();
}
}

//Rule 2: if the user pick option 2 (PAPER)
if (userChoice == 2)
{
if (compchoice==1)
{
cout<<"\n\t\t*****Computer chose paper*****";
userWin3();
}
else if (compchoice == 2)
{
cout<<endl<<"\n\t\t*****Computer chose paper*****";
userTie();
}
else if (compchoice == 3)
{
cout<<"\n\t\t*****Computer chose scissors*****";
userWin3();
}
}

//Rule 3: if the user pick option 3 (SCISSOR)
if (userChoice == 3)
{
if (compchoice==1)
{
cout<<"\n\t\t*****Computer chose rock*****";
userWin1();
}
else if (compchoice == 2)
{
cout<<"\n\t\t*****Computer chose scissors*****";
userTie();
}
else if (compchoice == 3)
{
cout<<"\n\t\t*****Computer chose paper*****";
userWin2();
}
}
if (userChoice == 4)
cout << "\n\t\t*****GoodBye!*****";
}
i tried the option for when the two players are tie and then they need to play the game again, but couldn't figure out how to get it done.

I would suggest making game_Rules() a bool function. Return true if the game should be played again. False, if not. Where you call userTie(), return true.

You can enclose your main logic in another do/while loop.
1
2
3
4
  do
  { displayMenu();
    user_Choose();
  } while (game_Rules());  // Keep looping while true 


PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Last edited on
Topic archived. No new replies allowed.