why does the 'else' statement on this code only work on the 'if' statement before it. I want it to work for all the 'if' statements in the code (that is, if none of the 'if' statements is true, then the else statement should execute). If that is not possible, then how it can be done? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

why does the 'else' statement on this code only work on the 'if' statement before it. I want it to work for all the 'if' statements in the code (that is, if none of the 'if' statements is true, then the else statement should execute). If that is not possible, then how it can be done?

cout<<"Type R for Rock\n"<<"Type P for Paper\n"<<"Type S for Scissors"<<endl; string P="Paper"; string R="Rock"; string S="Scissors"; string Player1; string Player2; cout<<"Player 1 Choose your Weapon\n"; cin>>Player1; cout<<"Player 2 Choose your Weapon\n"; cin>>Player2; if (Player1=="P"){ if (Player2=="R"){ cout<<"Player 1 acquires "<<P<<endl<<"Player 2 acquires "<<R<<endl<<"Player 1 wins ";}} if (Player1=="P"){ if (Player2=="S"){ cout<<"Player 1 acquires "<<P<<endl<<"Player 2 acquires "<<S<<endl<<"Player 2 wins "; }} if (Player1=="R"){ if (Player2=="P"){ cout<<"Player 1 acquires "<<R<<endl<<"Player 2 acquires "<<P<<endl<<"Player 2 wins "; }} if (Player1=="R"){ if (Player2=="S"){ cout<<"Player 1 acquires "<<R<<endl<<"Player 2 acquires "<<S<<endl<<"Player 1 wins "; }} if (Player1=="S"){ if (Player2=="P"){ cout<<"Player 1 acquires "<<S<<endl<<"Player 2 acquires "<<P<<endl<<"Player 1 wins "; }} if (Player1=="S"){ if (Player2=="R"){ cout<<"Player 1 acquires "<<S<<endl<<"Player 2 acquires "<<R<<endl<<"Player 2 wins "; }} if (Player1==Player2){ cout<<"It's a tie!. Try again";} else cout<<"Something is wrong. Try again";

3rd Nov 2016, 7:30 PM
Cody Arthur
Cody Arthur - avatar
6 Answers
+ 3
instead of writing like this this should be asked like adding add if( p1== p && p2==r) {cout<<"player 1 wins";} however the game designed is awesome.
4th Nov 2016, 1:53 AM
Sandeep Chatterjee
+ 2
I'll see if I can find any solutiona to your problem
3rd Nov 2016, 7:40 PM
Abdelaziz Abubaker
Abdelaziz Abubaker - avatar
+ 2
when tour trying to use the else statement have you tried to do this: if (Player1=="S"){ if (Player2=="P"){ cout<<"Player 1 acquires "<<S<<endl<<"Player 2 acquires "<<P<<endl<<"Player 1 wins "; } //placing the else statement here }
3rd Nov 2016, 7:44 PM
Abdelaziz Abubaker
Abdelaziz Abubaker - avatar
+ 2
@frederick Charlotte I thought the switch statement(case selection) only works with integers?. this is string
4th Nov 2016, 1:16 PM
Cody Arthur
Cody Arthur - avatar
+ 1
Because all your other ifs are independent of the last one. if you want them all to be dependent, you need to enter else if. Alternatively, it would be much easier to do a case selection instead of ifs..
3rd Nov 2016, 11:01 PM
Frédéric Charette
Frédéric Charette - avatar
0
no, you can use it with char using single quote... it doesn't actually work with strings ; but you can use switch with char variables...
4th Nov 2016, 1:23 PM
Frédéric Charette
Frédéric Charette - avatar