We are supposed to code a game of rock, paper, scissors using functions. I have completed everything besides determining the winner and displaying this. The 'void result' is what I'm having trouble with.
Currently, I am able to choose rock paper scissors and have the computer display the user's choice and the computer's choice. Here is my current code:
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
int user(int);
int pc(int);
int result(int);
string userChoice, pcChoice;
int userNum, pcNum;
void result(int, int);
int main()
{
user(userNum);
pc(pcNum);
cout << "You chose " << userChoice << endl << "They chose " << pcChoice;
return 0;
void result();
}
int user(int userNum)
{
cout << "Choose rock, paper, or scissors: ";
cin >> userChoice;
if(userChoice == "rock")
{
userNum = 1;
}
else if(userChoice == "paper")
{
userNum = 2;
}
else if(userChoice == "scissors")
{
userNum = 3;
}
else
{
cout << "Input invalid. Run again and enter rock, paper, or scissors." << endl;
exit(0);
}
return userNum;
}
int pc(int pcNum)
{
srand(time(0));
pcNum = (rand() % 3 1);
if (pcNum == 1)
{
pcChoice = "rock";
}
else if (pcNum == 2)
{
pcChoice = "paper";
}
else if (pcNum == 3)
{
pcChoice = "scissors";
}
return pcNum;
}
void result(int pcNum, int userNum)
{
if (pcNum == 1)
{
if (userNum == 1)
{
cout << "Tie. Play again.";
}
else if (userNum == 2)
{
cout << "You win. Paper covers rock.";
}
else if (userNum == 3)
{
cout << "You lose. Rock breaks scissors.";
}
}
else if (pcNum == 2)
{
if (userNum == 1)
{
cout << "You lose. Paper covers rock.";
}
else if (userNum == 2)
{
cout << "Tie. Play again.";
}
else if (userNum == 3)
{
cout << "You win. Scissors cut paper.";
}
}
else if (pcNum == 3)
{
if (userNum == 1)
{
cout << "You win. Rock breaks scissors.";
}
else if (userNum == 2)
{
cout << "You lose. Scissors cut paper";
}
else if (userNum == 3)
{
cout << "Tie. Play again.";
}
}
}
CodePudding user response:
void result();
This is a function declartion, not a function call.
You can't declare any function in a function. Also, you have declare that function. So, I think what you want is:
result(userNum, pcNum);
CodePudding user response:
Please read the comments marked as // CHANGE HERE
#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;
// CHANGE HERE:
// 1. Changed function signatures
// 2. Removed unnecessary global variables
// 3. Removed unnecessary functions
int user();
int pc();
void result(int, int);
string userChoice, pcChoice;
int main()
{
// CHANGE HERE: added local variables to store return values
int userNum = user();
int pcNum = pc();
// CHANGE HERE: added endl
cout << "You chose " << userChoice << endl << "They chose " << pcChoice << endl;
// CHANGE HERE: call result function here
result(pcNum, userNum);
return 0;
}
int user()
{
cout << "Choose rock, paper, or scissors: ";
cin >> userChoice;
if(userChoice == "rock")
{
return 1;
}
else if(userChoice == "paper")
{
return 2;
}
else if(userChoice == "scissors")
{
return 3;
}
cout << "Input invalid. Run again and enter rock, paper, or scissors." << endl;
exit(0);
}
int pc()
{
srand(time(0));
// CHANGE HERE: make pcNum local variable
int pcNum = (rand() % 3 1);
if (pcNum == 1)
{
pcChoice = "rock";
}
else if (pcNum == 2)
{
pcChoice = "paper";
}
else if (pcNum == 3)
{
pcChoice = "scissors";
}
return pcNum;
}
void result(int pcNum, int userNum)
{
if (pcNum == 1)
{
if (userNum == 1)
{
cout << "Tie. Play again.";
}
else if (userNum == 2)
{
cout << "You win. Paper covers rock.";
}
else if (userNum == 3)
{
cout << "You lose. Rock breaks scissors.";
}
}
else if (pcNum == 2)
{
if (userNum == 1)
{
cout << "You lose. Paper covers rock.";
}
else if (userNum == 2)
{
cout << "Tie. Play again.";
}
else if (userNum == 3)
{
cout << "You win. Scissors cut paper.";
}
}
else if (pcNum == 3)
{
if (userNum == 1)
{
cout << "You win. Rock breaks scissors.";
}
else if (userNum == 2)
{
cout << "You lose. Scissors cut paper";
}
else if (userNum == 3)
{
cout << "Tie. Play again.";
}
}
}