I'm trying to write a code for Rock Paper Scissors using a random generator.
However I need to have the choices for the user be the letters p, r, or s and they need to be able to be entered as capital (P) or lowercase (p) and mean the same thing (paper). I don't know how to assign a general name (in this case pChoice) to the options given to the user. Is that even possible?
Here is my code:
1 #include <iostream>
2 #include <cstdlib> // for the random number generation
3 #include <ctime> // for the time function
4 using namespace std;
5
6 int main()
7 {
8 int cChoice;
9 int pChoice (P = 1, R = 2, S = 3); //error not declared
10
11 cout << "Please enter your move: 'P' for Paper, 'R' for Rock, 'S' for 12 Scissors";
13 cin >> pChoice ;
14
15 srand(time(0));
16
17 cChoice = rand() % 3 + 1;
18 cout << "Computer's move is" << cChoice;
19
20 if (cChoice == 1 && pChoice == 1)
21 cout << "Tie!!";
22
23 else if (cChoice == 2 && pChoice == 2)
24 cout << "Tie!!";
25
26 else if (cChoice == 3 && pChoice == 3)
27 cout << "Tie!!";
28
29 else if (cChoice == 1 && pChoice == 3)
30 cout << "You Win! Scissors Cut Paper!!";
31
32 else if (cChoice == 2 && pChoice == 1)
33 cout << "You Win! Paper Covers Rock!!";
34
35 else if (cChoice == 3 && pChoice == 2)
36 cout << "You Win! Rock Breaks Scissors!!";
37
38 else if (cChoice == 1 && pChoice == 2)
39 cout << "Computer Wins! Paper Covers Rock!!";
40
41 else if (cChoice == 2 && pChoice == 3)
42 cout << "Computer Wins! Rock Breaks Scissors!!";
43
44 else if (cChoice == 3 && pChoice == 1)
45 cout << "Computer Wins! Scissors Cut Paper!!";
46
47 else (pChoice > 3)
48 cout << "Invalid move!!"; // 47:2: error: expected ';' before 'cout'
49
50
51 return 0;
51 }
You should make char variable to accept P, R or S and check with if-else or switch statements, then make integer variable which will be 1,2 or 3 depending on P, R and S, so it should be something like this:
1 2 3 4 5 6 7 8 9 10
char char_pChoice;
int pChoice;
std::cout<<"Please enter your move: 'P' for Paper, 'R' for Rock, 'S' for Scissors"<<std::endl;
std::cin>>char_pChoice;
if(char_pChoice=='P')
pChoice=1;
elseif(char_pChoice=='R')
pChoice=2;
elseif(char_pChoice=='S')
pChoice=3;
Thank you for your help! Your examples really helped me solve that issue I was stuck on :)
How could I make it so the program ends after an invalid answer is inputed. For example:
program: "Please enter your move: 'P' for Paper, 'R' for Rock, 'S' for 12 Scissors"
user: L
program: "Invalid move!!"
*Program ends*
Right now I am getting the following:
program: "Please enter your move: 'P' for Paper, 'R' for Rock, 'S' for 12 Scissors"
user: L
program: "Invalid move!!"
program: "Computer's move is ___"