If/else inside of a do-while loop?

Not sure why my game is not working - any advice?
The game properly loops as much as the user wants to play but it says Nobody wins no matter what the inputs are

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
     string userInput1, userInput2, keepPlaying;
      do{
     cout << "This is a rock-paper-scissor game - Enter a R for rock, P for "
             "paper, or S for scissors";
     cout << "Player one enter your choice: ";
     cin >> userInput1;
     cout << "Player two enter your choice: ";
     cin >> userInput2;
     if(userInput1 == "p" || userInput1 == "P" and userInput2 == "p" ||
             userInput2 == "P")
     {
         cout << "Nobody wins.";
     }
     else if(userInput1 == "r" || userInput1 == "R" and userInput2 == "r" ||
             userInput2 == "R")
     {
         cout << "Nobody wins.";
     }
     else if(userInput1 == "s" || userInput1 == "S" and userInput2 == "s" ||
             userInput2 == "S")
     {
         cout << "Nobody wins.";
     }
     else if(userInput1 == "p" || userInput1 == "P" and userInput2 == "r" ||
             userInput2 == "R")
     {
         cout << "Player one wins - paper covers rock.";
     }
     else if(userInput1 == "r" || userInput1 == "R" and userInput2 == "p" ||
             userInput2 == "P")
     {
         cout << "Player two wins - paper covers rock.";
     }
     else if(userInput1 == "r" || userInput1 == "R" and userInput2 == "s" ||
             userInput2 == "S")
     {
         cout << "Player one wins - rock breaks scissors";
     }
     else if(userInput1 == "s" || userInput1 == "S" and userInput2 == "r" ||
             userInput2 == "R")
     {
         cout << "Player two wins - rock breaks scissors";
     }
     else if(userInput1 == "s" || userInput1 == "S" and userInput2 == "p" ||
             userInput2 == "P")
     {
         cout << "Player one wins - scissors cut paper";
     }
     else if(userInput1 == "p" || userInput1 == "P" and userInput2 == "s" ||
             userInput2 == "S")
     {
         cout << "Player two wins - scissors cut paper";
     }
     
     cout << "Do you still want to play? Enter Y for yes or N for no: ";
     cin >> keepPlaying;
     
     }while(keepPlaying == "Y" || keepPlaying == "y");
     
     
By precedence of Boolean operators, x || y && z is equivalent to x || (y && z), not to (x || y) && z. In Boolean algebra, || is approximately analogous to addition, and && is approximately analogous to multiplication.
Like Helios said, you're a gonna need some ( ) to make sure your boolean algebra is correctly defined.

Change this :
1
2
3
4
5
if(userInput1 == "p" || userInput1 == "P" and userInput2 == "p" ||
             userInput2 == "P")
{
...
}


To something like this :

1
2
3
4
5
if ((userInput1 == "p" || userInput1 == "P") && (userInput2 == "p" ||
             userInput2 == "P")) // Note the added Parentheses around each sub-conditions
{
...
}
Last edited on
Topic archived. No new replies allowed.