-
paper rock scissors
I am using this on Windows and I can't get it to run properly, although it compiles without any problem. The idea of the program is simple: it first is supposed to ask player 1 to enter paper, rock or scissors, and then after they input their answer, it will ask player 2 the same thing and is supposed to tell you who wins, but the program keeps closing after player 2 enters their choice! Any suggestions or advice?
Code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
string rock;
string paper;
string scissors; // declare rock, paper, and scissors
string player1;
string player2; // declare value of player 2
cout<<"Player One, enter paper, rock, or scissors: "; // output to player one to enter
// declare value of player 1
cin>>player1; //input from player 1
cout<<endl;
cout<<"Player Two, enter paper, rock or scissors: "; // output to player two to enter
cin>>player2; // input from player 2
cout<<endl;
if (player1==rock && player2==rock)
{
cout<<"It's a tie!";
}
else if (player1==rock && player2==paper)
{
cout<<"Player Two wins!";
}
else if (player1==rock && player2==scissors)
{
cout<<"Player One wins!";
}
else if (player1==paper && player2==paper)
{
cout<<"It's a tie!";
}
else if (player1==paper && player2==rock)
{
cout<<"Player One wins!";
}
else if (player1==paper && player2==scissors)
{
cout<<"Player Two wins!";
}
else if (player1==scissors && player2==scissors)
{
cout<<"It's a tie!";
}
else if (player1==scissors && player2==rock)
{
cout<<"Player Two wins!";
}
else if (player1==scissors && player2==paper)
{
cout<<"Player One wins!";
}
else
{
cout<<"Please try again..."<<endl;
}
system("pause");
return 0;
}
-
rock paper and scissors are not initialized.
-
Instead of string variables rock, paper, and scissors, just use "rock", "paper", and "scissors", for example
Code:
if (player1=="rock" && player2=="rock")
Also, if you were willing to take input as integer values 0, 1, and 2, or convert to them, you could shrink this code down a bit - for example, if you let paper == 2, rock == 1, and scissors == 0, then if the inputs are n1 and n2,
n1 beats n2 if (n1 - n2 + 3)%3 == 1
tie if (n1 - n2 + 3)%3 == 0
n2 beats n1 if (n1 - n2 + 3)%3 == 2
-
What you need at the end of your program is
because that will allow you to see who wins.
-
Thanks!!
Thank you robatino, that did the trick!