Question
C++ rock, paper, scissors game there is an error in my code but I'm not sure where. when the player exits the game to start
C++ rock, paper, scissors game there is an error in my code but I'm not sure where. when the player exits the game to start a new game the loop just keeps running and wont stop for user input what is my error?
//Course: CSE1311/Section 2 //Student Name: //Project: 2 //Due Date: 2/7/2019
//Purpose /*The purpose of this project is to incorporate a do-while loop into the Rock, Paper, Scissors game. Also to give the user an additional option to exit the game to start a new match or to exit the program whenever the user is finished The computer and user will input R, P, S, or X for Rock, Paper, or Scissors or Exit Game. The CPU will generate a random number between 0-2. The CPU choice will go through the switch statements and have number switch over to a letter of R, P, or S. after the stwitch statement the CPU and the user input will go through a series of if and else if statments to output who wom the game.*/
#include
using namespace std;
int main() { //char ans = 'Y', // int ans; char X, P, R, S, userChoice, computerChoice; bool N = false; bool Y = true; int runCount = 0, winCount = 0, loseCount = 0, exitCount = 0, tieCount = 0, ans; //start of do while loop do { char X, P, R, S, userChoice, computerChoice; bool N = false; bool Y = true; //computerchoice random value (0 - 2) srand(time(0)); int computerGuess = rand() % (3);
//output statements cout << "Lets play Rock, Paper, Scisssors! " << endl; cout << "R = Rock" << endl; cout << "P = Paper" << endl; cout << "S = Scissors" << endl; cout << "X = Exit Game" << endl << endl; cout << "enter your move: "; cin >> userChoice; cout << endl; //'X' if statement and bad code message { if(userChoice != 'R' && userChoice != 'P' && userChoice != 'S' && userChoice != 'X' && userChoice != 'Y' && userChoice != 'N') { cout << "Bad code try again!" << endl; } if (userChoice == 'X') { ++exitCount; cout << "Ties: " << tieCount << endl; cout << "Exited game: " << exitCount << endl; cout << "Total Games Played: " << runCount << endl; cout << "Wins: " << winCount << endl; cout << "Loses: " << loseCount << endl << endl; runCount = 0, winCount = 0, loseCount = 0, tieCount = 0; cout << "Want to Exit Program?" << endl; cin >> ans; } } //switch statements switch (computerGuess) { case 0: computerChoice = 'R'; break; case 1: computerChoice = 'P'; break; case 2: computerChoice = 'S'; break; }
//if else statements { if (computerChoice == userChoice) cout << "It's a Tie!" << endl; if (computerChoice == userChoice) { ++tieCount; ++runCount; } else if (computerChoice == 'R' && userChoice == 'P') cout << "user wins - paper covers rock!" << endl; if(computerChoice == 'R' && userChoice == 'P') { ++winCount; ++runCount; } else if (computerChoice == 'S' && userChoice == 'P') cout << "Computer wins - scissors cuts paper!" << endl; if(computerChoice == 'S' && userChoice == 'P') { ++loseCount; ++runCount; } else if (computerChoice == 'P' && userChoice == 'R') cout << "computer wins - paper covers rock!" << endl; if (computerChoice == 'P' && userChoice == 'R') { ++loseCount; ++runCount; } else if (computerChoice == 'S' && userChoice == 'R') cout << "user wins - rock smashes scissors!" << endl; if(computerChoice == 'S' && userChoice == 'R') { ++winCount; ++runCount; } else if (computerChoice == 'R' && userChoice == 'S') cout << "computer wins - rock smashes scissors!" << endl; if (computerChoice == 'R' && userChoice == 'S') { ++winCount; ++runCount; } else if (computerChoice == 'P' && userChoice == 'S') cout << "user wins - scissors cuts paper!" << endl; if(computerChoice == 'P' && userChoice == 'S') { ++winCount; ++runCount; } } // end of R, P, S if statement cout << endl;
} while (ans = 1); // end of do while loop
cout << "Thanks for playing!" << endl; cout << "Coded by __________" << endl;
return 0;
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started