C++ PROGRAM // Function Prototypes // ******************************************
ID: 3763090 • Letter: C
Question
C++ PROGRAM
// Function Prototypes
// ********************************************************
// The getUserChoice function displays a menu allowing *
// the user to select rock, paper, or scissors. The *
// function then returns 1 for rock (via the ROCK *
// constant), or 2 for paper (via the PAPER constant), *
// or 3 for scissors (via the SCISSORS constant). *
// ********************************************************
int getUserChoice();
// ********************************************************
// The getComputerChoice function returns the computer's *
// game choice. It returns 1 for rock (via the ROCK *
// constant), or 2 for paper (via the PAPER constant), *
// or 3 for scissors (via the SCISSORS constant). *
// YOU NEED to use rand()
// ********************************************************
int getComputerChoice();
// ********************************************************
// The determineWinner function accepts the user's game *
// choice and the computer's game choice as arguments and *
// displays a message indicating the winner. *
// For example, If the user chooses rock and the computer chooses
// scissors, you should display “ rock smashes scissors”
// ********************************************************
void determineWinner(int, int);
// ********************************************************
// The displayChoice function accepts an integer argument *
// and displays rock, paper, or scissors. *
// ********************************************************
void displayChoice(int);
//The displayChoice function is written for you.
void displayChoice(int choice)
{
int ROCK = 1;
int PAPER = 2;
int SCISSORS = 3;
if (choice == ROCK)
cout << "Rock ";
else if (choice == PAPER)
cout << "Paper ";
else if (choice == SCISSORS)
cout << "Scissors ";
else
cout << "Error condition detected! ";
}
Your main function will look like this. You need to call your functions at the appropriate locations.
int main()
{
int userChoice; // To hold the user's choice
int computerChoice; // To hold the computer's choice
// Get the computer's choice.
// Get the user's choice.
while (userChoice != 4)
{
// Determine the winner.
// Get the computer's choice.
// Get the user's choice.
}
return 0;
}
Explanation / Answer
#include <vector>
#include <iostream>
using namespace std;
// Function Prototypes
// ********************************************************
// The getUserChoice function displays a menu allowing *
// the user to select rock, paper, or scissors. The *
// function then returns 1 for rock (via the ROCK *
// constant), or 2 for paper (via the PAPER constant), *
// or 3 for scissors (via the SCISSORS constant). *
// ********************************************************
int getUserChoice();
// ********************************************************
// The getComputerChoice function returns the computer's *
// game choice. It returns 1 for rock (via the ROCK *
// constant), or 2 for paper (via the PAPER constant), *
// or 3 for scissors (via the SCISSORS constant). *
// YOU NEED to use rand()
// ********************************************************
int getComputerChoice();
// ********************************************************
// The determineWinner function accepts the user's game *
// choice and the computer's game choice as arguments and *
// displays a message indicating the winner. *
// For example, If the user chooses rock and the computer chooses
// scissors, you should display “ rock smashes scissors”
// ********************************************************
void determineWinner(int, int);
// ********************************************************
// The displayChoice function accepts an integer argument *
// and displays rock, paper, or scissors. *
// ********************************************************
void displayChoice(int);
//The displayChoice function is written for you.
void displayChoice(int choice)
{
int ROCK = 1;
int PAPER = 2;
int SCISSORS = 3;
if (choice == ROCK)
cout << "Rock ";
else if (choice == PAPER)
cout << "Paper ";
else if (choice == SCISSORS)
cout << "Scissors ";
else
cout << "Error condition detected! ";
}
int main()
{
int userChoice; // To hold the user's choice
int computerChoice; // To hold the computer's choice
// Get the computer's choice.
// Get the user's choice.
userChoice = getUserChoice();
computerChoice = getComputerChoice();
while (userChoice != 4)
{
cout<<"User selected: ";
displayChoice(userChoice);
cout<<"Computer selected: ";
displayChoice(computerChoice);
cout<<" ";
determineWinner(userChoice, computerChoice);
cout<<" ";
userChoice = getUserChoice();
computerChoice = getComputerChoice();
}
return 0;
}
int getUserChoice() {
cout<<"Enter 1 for rock 2 for paper 3 for scissors Enter: ";
int in;
cin>>in;
return in;
}
int getComputerChoice() {
int c = rand()%3+1;
return c;
}
void determineWinner(int a, int b) {
if (a == b) {
cout<<"Draw!!";
} else if ((a==1 && b==2)||(a==2 && b==1)) {
cout<<"Paper beats Rock !!";
} else if ((a==2 && b==3)||(a==3 && b==2)) {
cout<<"Scissor beats Paper !!";
} else {
cout<<"Rock beats Scissors !!";
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.