SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Walter Savitch ,julia Lobur
Chapter:
More Flow Of Control
Exercise:
Programming Projects
Question:1 | ISBN:9780321531346 | Edition: 7

Question

Write a program to score the paper-rock-scissor game. Each of two users types in either P, R, or S. The program then announces the winner as well as the basis for determining the winner: Paper covers rock, Rock breaks scissors, Scissors cut paper, or Nobody wins. Be sure to allow the users to use lowercase as well as uppercase letters. Your program should include a loop that lets the user play again until the user says she or he is done.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

 Program:

#include<iostream>
Using namespace STD;
void main()
{
 char choice1, choice2,choice,userChoice;
 do	
 {  for(int i = 0; i<2; i++)
  {
   do
   { 
	cout<<" Enter your choice P, R or S:";
	cin>>choice;
	}while(choice != 'p' && choice !='P' && choice != 'r' 
           && choice !='R' && choice !='S' && choice!='s');
	if(i == 0)
    {
	 choice1= choice;
	}
	else if(i ==1)
	{
	 choice2 = choice;
      }
	}
	if(choice1 == choice2)
     {
	cout<<" Game has ended in a draw\n";
	}
	else
	{
	if(choice1 == 'p' || choice1 =='P')
	{
	 if (choice2 =='s' || choice2 =='S')
	 {
	  cout<<" As scissor can cut paper player 2 win \n";
	 }
	 else
	 {
	  cout<<" As paper cover rock, player 1 wins \n";
	 }
	}
	if(choice1 == 's' || choice1 =='s')
	{
	 if (choice2 =='p' || choice2 =='p')
	 {
		cout<<" As scissor can cut paper player 1 win\n";
	 }
      else
	 {
	   cout<<"As  rock can break scissor ,
 player 2 wins\n";
	 }
	}
	if(choice == 'r' || choice1 =='r')
	{
	  if (choice2 =='s' || choice2 =='S')
	  {
	   cout<<"As rock can break scissor player 1 wins";
	  }
	 else
	  {
 		cout<<"As paper cover rock, player 2 wins";
       }
	 }
	}

	cout<<"\n To repeat calculations for 
different variables then press 'y' or 'Y'\n ";		
	cin>>userChoice;
	}while(userChoice =='y' || userChoice =='Y');	
    
}

Output:

 

Enter your choice P, R or S:r
Enter your choice P, R or S:p
As paper cover rock, player 2 wins
 To repeat calculations for different variables then press 'y' or 'Y':  n
0 0

Discussions

Post the discussion to improve the above solution.