Rock, Paper, Scissors Program

My code is posted below. All I'm trying to do is write a rock, paper, scissors program that will loop the menu so you can keep playing the game without closing it. Can someone help me figure out why my code is not working. Are you able to enclose a do-while statement within a switch statement? It seems like that may be the source of my issue. In my 1st case, I want the program to ask the user if they want to play again and start the program over from the 2nd menu. In my 2nd case, I want to have the statistics of how many games were won, lost, and draw. In the 3rd case, I want to quit the program.

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <cmath>
using namespace std;

int main(){

char ch;

int wins, losses, draw, player1, computer, menuChoice;

computer = rand() % 3 + 1;

wins = 0;
losses = 0;
draw = 0;


cout << "Welcome to Rock, Paper, Scissors game!\n";

cout << " " <<endl;

cout << "1 - Play Paper, Rock, Scissors versus the Computer"<<endl;
cout << "2 - Print out the Statistics versus the Computer"<<endl;
cout << "3 - Quit the Program"<<endl;

cout << "Please choose a number from the menu above:\n";
cin >> menuChoice;
if (menuChoice < 1 || menuChoice > 3){

cout << "Error. Please enter in a number from 1 to 3.";

}
do {

switch (menuChoice){

case 1:

cout << "Rock, Paper, Scissors is a simple game. The rules are:\n";

cout << " " <<endl;

cout << "Rock beats Scissors\n";
cout << "Scissors beats Paper\n";
cout << "Paper beats Rock\n";

cout << " " <<endl;

cout << "Press 1 for Rock, 2 for Paper, 3 for Scissors:" << endl;
cin >> player1;

int computer = rand() % 3 + 1;
cout << "The computer chose: " << computer << endl;

if (player1 == 1 && computer == 1){

cout << "Rock meets Rock. Its a draw!" <<endl;
draw++;
}

else if (player1 == 1 && computer == 2){

cout << "Rock covers Paper. You lose!" <<endl;
losses++;
}

else if (player1 == 1 && computer == 3){

cout << "Rock crushes Scissors. You win!" <<endl;
wins++;
}

else if (player1 == 2 && computer == 1){

cout << "Paper covers Rock. You win!" <<endl;
wins++;
}

else if (player1 == 2 && computer == 2){

cout << "Paper meets Paper. Its a draw!" <<endl;
draw++;
}

else if (player1 == 2 && computer == 3){

cout << "Paper is cut by Scissors. You lose!" <<endl;
losses++;
}

else if (player1 == 3 && computer == 1){

cout << "Scissors are crushed by Rock. You lose!" <<endl;
losses++;
}

else if (player1 == 3 && computer == 2){

cout << "Scissors cut Paper. You win!" <<endl;
wins++;
}

else if (player1 = 3 && computer == 3){

cout << "Scissors meets Scissors. Its a draw!" <<endl;
draw++;
}

else {

cout << "You didn't select 1, 2 or 3" <<endl;
}

cout << "Would you like to play again? Y/N" <<endl;
cin >> ch;
system("CLS");
} while (ch == 'Y' || ch == 'y')

break;


return 0;
}

Unless you pasted the code incorrectly then it won't compile. The switch statement within the do-while is not terminated with a closing brace (}). In any case I think you need to understand what you are trying to do. For example, I think you want a loop at the outer level that presents the game/stats/quit options. Within that you need a switch for the choice. If choice 1 is chosen then you need an inner loop which presents the "play again" option. Something like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int main()
{
	int wins = 0, losses = 0, draws = 0;
	for (;;)
	{
		cout << "1 - Play Paper, Rock, Scissors versus the Computer" << endl;
		cout << "2 - Print out the Statistics versus the Computer" << endl;
		cout << "3 - Quit the Program" << endl;
		cout << "Please choose a number from the menu above:\n";

		int menuChoice;
		cin >> menuChoice;

		switch (menuChoice)
		{
		case 1:
			do
			{
				play_game();

				cout << "Would you like to play again? Y/N" << endl;
				cin >> ch;
			} while (isupper(ch) == 'Y');
			break;

		case 2:
			display_stats();
			break;

		case 3:
			exit(0);
		}
	}
}

Topic archived. No new replies allowed.