Rock Paper Scissors code using if-then statements

How do I make this code correct using "switch" and "if-then" statements?

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

/*
	Program designed to play rock, paper, scissors with a user.
*/

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
	cout << "Rock, Paper, Scissors GO!" << endl; // Little introduction here

	// Input section
	char choice;
	int seed;
	
	cout << "To play, please enter R, for rock, P, for paper, or S, for scissors: " << endl; // Defines the choices for the user.
	cin >> choice; // Keeps the choices for the player.
	cout << "Please enter the seed for the random number for the computer: " << endl; // Need the seed for the computer.
	cin >> seed; // Keeps the seed for the computer.
	cout << "You have entered: " << seed << endl; //Shows the user their seed choice

	// Process Section
	char computerCh; //computerCh is the choice of the computer
	int R, S, P; //User input
	srand(seed);
	computerCh = rand() % 3 + 1;

	//Computer's choices and outcomes
	if (computerCh <= 0)
	{
		cout << "Computer chose Rock." << endl;

	}

	else if (computerCh <= 1)
	{
		cout << "Computer chose Paper." << endl; 

	}

	else if (computerCh <= 2)
	{
		cout << "Computer chose Scissors." << endl;

	}
	
	// User's choices
	// This section is for rock.
	if (choice == R && computerCh <= 0)
	{
		cout << "You tied with the computer!" << endl;

	}
	else if (choice == P && computerCh <= 1)
		cout << "Paper beats rock! You lost." << endl;
	
	else if (choice == S && computerCh <= 2)
	{
		cout << "Rock beats scissors! You won!" << endl;

	}

	// This section is for paper.
	if (choice == P && computerCh <= 1)
	{
		cout << "You tied with the computer!" << endl;

	}
	else if (choice == R && computerCh <= 0)
	{
		cout << "Paper beats rock! You won!" << endl;

	}
	else if (choice == S && computerCh <= 2 )
	{
		cout << "Scissors beat papers. You lost." << endl;

	}

	// This section is for scissors.
	if (choice == S && computerCh <= 2)
	{
		cout << "You tied with the computer!" << endl;

	}
	else if (choice == R && computerCh <= 0)
	{
		cout << "Rock beats scissors. You lost." << endl;

	}
	else if (choice == P && computerCh <= 1)
	{
		cout << "Scissors beats paper! You won!" << endl;

	}


	// This is for switching the cases.
	switch (computerCh)
	{
	case 0:
		computerCh = R;
	case 1:
		computerCh = P;
	case 2:
		computerCh = S;
	}

	return EXIT_SUCCESS;
}
closed account (iGLbpfjN)
Line 29. The correct syntax is computerCh = rand() % 3;, because you want to generate a value between 0 and 2.

Line 20. Remember to restrict the choices of the user. If I enter letter F, the program won't work. Try doing an if statement to restrict it.

Line 102 to 110 and 32 to 48 can be merged in one switch() statement:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
switch(computerCh)  // Remember that in each case you have to put a break!!!
	{
		case 0:
			computerCh = 'R';
			cout << "Computer chose Rock." << endl;
			break; 
			
		case 1:
			computerCh = 'P';
			cout << "Computer chose Paper." << endl; 
			break;
			
		case 2:
			computerCh = 'S';
			cout << "Computer chose Scissors." << endl;
			break;
	}


Then, in the sections you can nest if statements like this:
1
2
3
4
5
6
7
	
if(choice == 'R')
{
		if(computerCh == 'R') cout << "You tied with the computer!" << endl;
		else if(computerCh == 'S') cout << "Rock beats scissors! You won!" << endl;
		else if(computerCh == 'P') cout << "Paper beats rock! You lost." << endl;
}
Last edited on
Topic archived. No new replies allowed.