Thread: paper rock scissors

  1. #1
    UpTooLate
    Join Date
    Feb 2008
    Location
    New York
    Posts
    32

    Question 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;
    }
    Last edited by Amyaayaa; 02-11-2008 at 11:23 PM. Reason: forgot to enter more info

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    rock paper and scissors are not initialized.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    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)&#37;3 == 1
    tie if (n1 - n2 + 3)%3 == 0
    n2 beats n1 if (n1 - n2 + 3)%3 == 2

  4. #4
    Registered User
    Join Date
    Feb 2008
    Posts
    26
    What you need at the end of your program is
    Code:
    cin.ge(t()
    because that will allow you to see who wins.

  5. #5
    UpTooLate
    Join Date
    Feb 2008
    Location
    New York
    Posts
    32

    Lightbulb Thanks!!

    Thank you robatino, that did the trick!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. rock paper Scissors
    By jackstify in forum C++ Programming
    Replies: 3
    Last Post: 12-14-2007, 10:16 PM
  2. Rock Paper Scissors Game
    By tbca in forum C++ Programming
    Replies: 12
    Last Post: 07-09-2007, 12:16 PM
  3. need help with rock paper scissors program
    By Mshock in forum C Programming
    Replies: 3
    Last Post: 04-22-2006, 07:44 AM
  4. Another rock paper scissor question
    By mattflick in forum C Programming
    Replies: 11
    Last Post: 09-29-2005, 09:41 PM
  5. PRS Help
    By SpudNuts in forum C Programming
    Replies: 10
    Last Post: 08-07-2005, 01:14 PM