Need help with a nested if else statement

I've been trying to get this to work for hours, and I can't seem to figure out how to make this run successfully. It compiles and runs yet does not follow the logic I'm trying to make work.

Any help would be appreciated.

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
 #include <iostream>
using namespace std;

int main( )
{
	char player1, player2;
	
	cout << "Rock = r or R" << endl;
	cout << "Paper = p or P" << endl;
	cout << "Scissors = s or S" << endl;
	cout << "Player 1 please choose your weapon." << endl;
	cin >> player1;
	cout << "Player 2 please choose your weapon." << endl;
	cin >> player2;
	
	if (player1 == ('p') || player1 == ('P') && (player2 == ('r') || player2 == ('R')))
	{
		cout << "Paper covers rock, player 1 wins." << endl;
	}
		else if (player1 == ('r') || player1 == ('R') && (player2 == ('p') || player2 == ('P')))
		{
			cout << "Paper covers rock, player 2 wins." << endl;
		}
			else if (player1 == ('s') || player1 == ('S') && (player2 == ('p') || player2 == ('P')))
			{
				cout << "Scissors cut paper, player 1 wins" << endl;
			}
				else if (player1 == ('p') || player1 == ('P') && (player2 == ('s') || player2 == ('S')))
				{
					cout << "Scissors cut paper, player 2 wins" << endl;
				}
					else if (player1 == ('r') || player1 == ('R') && (player2 == ('s') || player2 == ('S')))
					{
						cout << "Rock breaks scissors, player 1 wins" << endl;
					}
						else if (player1 == ('s') || player1 == ('S') && (player2 == ('r') || player2 == ('R')))
						{
							cout << "Rock break scissors, player 2 wins" << endl;
						}
							else if (player1 == ('s') || player1 == ('S') && (player2 == ('s') || player2 == ('S')))
							{
								cout << "Tie, nobody wins" << endl;
							}
								else if (player1 == ('p') || player1 == ('P') && (player2 == ('p') || player2 == ('P')))
								{
									cout << "Tie, nobody wins" << endl;
								}
									else if (player1 == ('r') || player1 == ('R') && (player2 == ('r') || player2 == ('R')))
									{
										cout << "Tie, nobody wins" << endl;
									}
cout << "Thanks for playing! Good-Bye!" << endl;
}
Last edited on
For every if statement, you have this.

if (player1 == ('p') || player1 == ('P') && (player2 == ('r') || player2 == ('R')))

First, the char does not need parentheses.

1
2
 if (player 1 == 'p')
 // this is perfectly fine 


Two, your main problem lies in your placement of parentheses.

(1 or 2 and 3 or 4)

is not the same as

((1 or 2) and (3 or 4))
Thank you so much, that fixed it.
And you don't need to indent after each if statement. You are not using a nested statement. A nested statement would look like this:

1
2
3
4
5
if(dogs are purple) {
    if(cows are green) {
          //do some action
   }
}


you are doing this
1
2
3
4
5
6
7
if(dogs are purple) {
    //perform some action
}

else if(cows are green) {
    //perform some action
}


In the first example the logic goes like this: "If dogs are purple, check if cows are green, and if cows are green, do some action." In the second example: "If dogs are green, do some action. If not, check if cows are green, and if they are, do some action."
Last edited on
Topic archived. No new replies allowed.