1. Engineering
  2. Computer Science
  3. can someone help me fix up my code i am...

Question: can someone help me fix up my code i am...

Question details

Can someone help me fix up my code? I am designing a rock paper scissors program. Some of the outputs for the second option are of the program are coming out wrong. For example, it showed that scissors beat scissors which is of course false.

Code:

#include <iostream> // For cin and cout
#include <cstdlib> // For rand()
#include <iomanip>
using namespace std;


//-----------------------------------------------------------------------------------------
int main()
{
// Display Instructions
cout << "CS 141 Program #1: Rock/Paper/Scissors \n"
<< endl
<< "Welcome to the game of Rock/Paper/Scissors where you play against \n"
<< "the computer. On each move the computer will choose R, P, or S, then \n"
<< "the user will be prompted for their choice, and then the score will \n"
<< "be updated. P beats R, R beats S, and S beats P. The score starts \n"
<< "at 0. Add one if the person wins, subtract one if the computer wins. \n"
<< "The game ends if the score reaches -5 or + 5. \n"
<< "User input of 'x' or 'X' causes the game to exit. \n"
<< endl
<< "Here we go! \n"
<< endl;
  
int computerChoiceVersion;
cout << "Select how the computer chooses its move: \n"
<< "1. Always choose Rock \n"
<< "2. Random guess \n"
<< "3. Random guess with graphical score \n"
<< "Enter your choice: ";

cin >> computerChoiceVersion;
cout << endl;
char computerChoice;
char userChoice;
int userScore = 0;
int computerScore = 0;

int moveNumber = 1;
while( moveNumber <= 100) {
cout << setw(1) << moveNumber << ". ";
  
// Get the computer choice
               if (computerChoiceVersion == 1) {
computerChoice = 'R';
// Prompt for and get the user's choice
cout << "Your move: ";
cin >> userChoice;
//cout << userChoice;
if(userChoice == 'x' || userChoice == 'X'){
cout << endl << "Exiting program..." << endl << "Tie game!" << endl;
cout << "Ending program...";
exit(0);
}
cout << endl;
  
// Display computer choice
cout << " Computer chose " << computerChoice << endl;
  
// Update score, and then display it
               if(computerChoice == 'R' && userChoice == 'R' || userChoice == 'r') {
cout << " Tie. Score: " << userScore << endl; }
else if (computerChoice == 'R' && userChoice == 'P' || userChoice == 'p'){
userScore++;
cout << " User's point. Score: " << userScore << endl;
}
else if (computerChoice == 'R' && userChoice == 'S' || userChoice == 's'){
userScore--;
cout << " Computer's point. Score: " << userScore << endl;
}
  
  
}
int computerNumber = rand() % 3 + 1;
if (computerChoiceVersion == 2) {
if (computerNumber == 1) {
computerChoice = 'R';
}
else if (computerNumber == 2) {
computerChoice = 'P'; }
else {
computerChoice = 'S';
}
cout << "Your move: ";
cin >> userChoice;
cout << userChoice;
//cout << userChoice;
if(userChoice == 'x' || userChoice == 'X'){
cout << endl << "Exiting program..." << endl << "Tie game!" << endl;
cout << "Ending program...";
exit(0);
}
cout << endl;
  
// Display computer choice
cout << " Computer chose " << computerChoice << endl;
if(computerChoice == 'R' && userChoice == 'R' || userChoice == 'r') {
cout << " Tie. Score: " << userScore << endl;
}
else if (computerChoice == 'R' && userChoice == 'P' || userChoice == 'p'){
userScore++;
cout << " User's point. Score: " << userScore << endl;
}
else if (computerChoice == 'R' && userChoice == 'S' || userChoice == 's'){
userScore--;
cout << " Computer's point. Score: " << userScore << endl;
}
else if (computerChoice == 'P' && (userChoice == 'S' || userChoice == 's')){
userScore++;
cout << " User's point. Score: " << userScore << endl;
}
else if (computerChoice == 'P' && (userChoice == 'R' || userChoice == 'r')){
userScore--;
cout << " Computer's point. Score: " << userScore << endl;
}
else if (computerChoice == 'P' && (userChoice == 'P' || userChoice == 'p')){
cout << " Tie. Score: " << userScore << endl;
}
else if (computerChoice == 'S' && userChoice == 'R' || userChoice == 'r'){
userScore++;
cout << " User's point. Score: " << userScore << endl;
}
else if (computerChoice == 'S' && userChoice == 'P' || userChoice == 'p'){
userScore--;
cout << " User's point. Score: " << userScore << endl;
}
else if (computerChoice == 'S' && userChoice == 'S' || userChoice == 's'){
cout << " Tie. Score: " << userScore << endl;
}
// Display final message, depending on the score
if (userScore == 5){
cout << "User wins!" << endl;
exit(0);
}
else if (userScore == -5){
cout << "Computer wins!" << endl;
cout << "Ending program...";
exit(0);
}
  
}
moveNumber++;
}
cout << "Ending program..." << endl;
return 0;
}

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution