C++ help with basic function use Play the rock paper scissors game. Two players
ID: 3936028 • Letter: C
Question
C++ help with basic function use
Play the rock paper scissors game. Two players enter either rock, paper, or scissors and the winner is determined as follows:
Ask the user if s/he wants to play again. BE SURE to include a function called play as shown in partial program below. Output should look like the below.
Sample run:
Sample run:
partial program to use:
#include <iostream>
#include <string>
using namespace std;
/* Type your code here. */
int main()
{
string p1, p2;
cout << "Play rock, paper, scissors ";
string goOn = "yes";
while (goOn == "yes") {
cout << "Player 1: ";
cin >> p1;
cout << p1 << endl;
cout << "Player 2: ";
cin >> p2;
cout << p2 << endl;
play(p1, p2);
cout << "Do you want to continue? (yes or no): ";
cin >> goOn;
cout << goOn << endl;
}
}
Explanation / Answer
#include <iostream>
#include <string>
using namespace std;
void play( string p1, string p2 ){
if( p1 == "paper" ){
if( p2 == "scissors" ){
cout << "Player 2 wins -- Scissors cut paper" << endl; }
else if( p2 == "rock" ){
cout << "Player 1 wins -- Paper covers rock" << endl; }
else{
cout << "Draw!" <<endl; }
}
if( p1 == "scissors" ){
if( p2 == "paper" ){
cout << "Player 1 wins -- Scissors cut paper" << endl; }
else if( p2 == "rock" ){
cout << "Player 2 wins -- Rock breaks scissors" << endl; }
else{
cout << "Draw!" <<endl; }
}
if( p1 == "rock" ){
if( p2 == "scissors" ){
cout << "Player 1 wins -- Rock breaks scissors" << endl; }
else if( p2 == "paper" ){
cout << "Player 2 wins -- Paper covers rock" << endl; }
else{
cout << "Draw!" <<endl; }
}
cout << endl;
}
int main()
{
string p1, p2;
cout << "Play rock, paper, scissors ";
string goOn = "yes";
while (goOn == "yes") {
cout << "Player 1: ";
cin >> p1;
cout << "Player 2: ";
cin >> p2;
play(p1, p2);
cout << "Do you want to continue? (yes or no): ";
cin >> goOn;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.