Rock paper scissors

Hello. I'm making a rock paper scissors game and its causing me some problems.
I would appreciate someone explaining to me what went wrong and if I'm over complicating a simple project. Here you go.

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
#include "stdafx.h"
#include <iostream>
#include <cstring>
using namespace std;

int main()
{
	//declarations
	char ans1, ans2, ans3;
	
	//input
	do
	{
		cout << "Welcome to the classic game of rock, paper, and scissors.\n";
		cout << "User 1. Please select the following choices:\n";
		cout << "(P, R, or S)\n";
		cin >> ans1;
		cout << "User 2. Please select the following choices:\n";
		cout << "(P, R, or S)\n";
		cin >> ans2;
		
	//processing
		
		if (ans1 == 'P' || ans1 == 'p' && ans2 == 'R' || ans2 == 'r')
		{
			cout << "Paper covers rock.\n";
			cout << "The winner is User 1.\n";
		}
		
		else if (ans1 == 'R' || ans1 == 'r' && ans2 == 'S' || ans2 == 's')
		{
			cout << "Rock breaks scissors.\n";
			cout << "The winner is User 1.\n";
		}
		
		else if (ans1 == 'S' || ans1 == 's' && ans2 == 'P' || ans2 == 'p')
		{
			cout << "Scissors cuts paper.\n";
			cout << "The winner is User 1.\n";
		}
		
		else if (ans2 == 'P' || ans2 == 'p' && ans1 == 'R' || ans1 == 'r')
		{
			cout << "Paper covers rock.\n";
			cout << "The winner is User 2.\n";
		}
		
		else if (ans2 == 'R' || ans2 == 'r' && ans1 == 'S' || ans1 == 's')
		{
			cout << "Rock breaks scissors.\n";
			cout << "The winner is User 2.\n";
		}

		else if (ans2 == 'S' || ans2 == 's' && ans1 == 'P' || ans1 == 'p')
		{
			cout << "Scissors cuts paper.\n";
			cout << "The winner is User 2.\n";
		}
		else if (ans1 == ans2)
		{
			cout << "Nobody wins.\n";
		}
		
		else if (ans1 != 'R' || 'P' || 'S')
		{
			cout << "Error. Please try again.\n";
		}

		else if (ans1 != 'r' || 'p' || 's')
		{
			cout << "Error. Please try again.\n";
		}

		else if (ans2 != 'R' || 'P' || 'S')
		{
			cout << "Error. Please try again.\n";
		}
		
		else if (ans2 != 'r' || 'p' || 's')
		{
			cout << "Error. Please try again.\n";
		}
	
		cout << "Do you wish to play again?\n";
		cin >> ans3;
	
	} while (ans3 == 'y' || ans3 == 'Y');
	
	//output
	cout << "Game over.\n";
}
Your 'else if's' starting at Line 64 are broken. You can't chain evaluations like that, they need to be typed out like so:
 
else if (ans1 != 'R' || ans1 != 'P' || ans1 != 'S')
code's really messy...
you can use toupper() function (included in ctype) for answers, like

1
2
3
4
5
6
7
8
int main()
{
       char letter;
       cin >> letter;

       if(toupper(letter) == 'P')  // no need of if(letter == 'p'  ||  letter == 'P')
                 ...
}



http://www.cplusplus.com/reference/locale/toupper/
Last edited on
@rich1 Thanks for your suggestion. I appreciate it. I'm still uncertain of my code with what your comment says. Do you mean repeating that for all of the if- else if statements?

Last edited on
What rich was saying, is that by using the toupper() function (which turns a lower-case alphabetical character into its upper-case counter part), you would only have to check if the input matches a capital 'P'.
Thanks everyone. Got the problem figured out. You people rock.
Topic archived. No new replies allowed.