Hello All,

I am new to this so I am not sure if this is going to post correctly. I am trying to create a rock, paper, scissors game. My issue with my code is I only want the code to repeat if the computer picks the same choice as the user, however, my code is repeating everytime. If anyone could help me with this I would appreciate it!

Thanks,

Taylor

//This program let a user play rock, paper, scissors with a computer.
#include <iostream>
#include <cstdlib> //For rand and srand
#include <ctime> //For the time function
using namespace std;

int main()
{
    //Get a random number.
    unsigned seed= time(0);
    //Seed the random number generator
    srand(seed);
    int userChoice;
    int computerChoice = rand()%3+1;
    cout << "Rock, Paper, Scissors Game\n";
    cout << "You are playing against the computer\n";
    cout << "Type 1 for rock\n";
    cout << "Type 2 for paper\n";
    cout << "Type 3 for scissors\n";
    cin >> userChoice;                      //User enters choice here
    cout <<computerChoice<<endl;            



    if (userChoice == 1) 
    {
              if (computerChoice == 1)
              cout << "We have made the same choice. Rematch!\n\n\n\n";
              else if (computerChoice == 2)
              cout << "Paper wraps rock, computer wins\n";
              else if (computerChoice == 3)
              cout << "Rock smashes scissors, you win\n";
    }   
    if (userChoice == 2)
    {
               if (computerChoice == 2)
               cout << "We have made the same choice. Rematch!\n\n\n\n";
               else if (computerChoice ==1)
               cout << "Paper wraps rock, you win\n";
               else if (computerChoice == 3)
               cout << "Scissors cut paper, computer wins\n";
   }  
   if (userChoice == 3)
   {
              if (computerChoice == 3)
              cout << "We have made the same choice. Rematch!\n\n\n\n";
              else if (computerChoice == 2)
              cout << "Scissors cut paper, you win\n";
              else if (computerChoice == 1)
              cout << "Rock smashes scissors, computer wins\n";

   }
   return main();

}

Recommended Answers

All 6 Replies

use a loop that checks user vs computer input for equality before doing test for winner/loser.

int userInput;
int computerSelection;
//get user input
//start with computerSelection equal to userInput
computerSelection = userInput;
//now change selection until they are different
while(computerSelection == userInput)
  //get new computerSelection
//then compare userInput and computerSelection with regard to rock/paper/scissors now that they are different.

I tried to work that into the code but when I do it does not display anything after the user puts in a number.

use a loop that checks user vs computer input for equality before doing test for winner/loser.

int userInput;
int computerSelection;
//get user input
//start with computerSelection equal to userInput
computerSelection = userInput;
//now change selection until they are different
while(computerSelection == userInput)
  //get new computerSelection
//then compare userInput and computerSelection with regard to rock/paper/scissors now that they are different.

Just incase what you have in a do..while() loop.

do{//start the loop here
cout << "Rock, Paper, Scissors Game\n";
cout << "You are playing against the computer\n";
cout << "Type 1 for rock\n";
cout << "Type 2 for paper\n";
cout << "Type 3 for scissors\n";
cin >> userChoice; //User enters choice here
cout <<computerChoice<<endl;



if (userChoice == 1)
{
if (computerChoice == 1)
cout << "We have made the same choice. Rematch!\n\n\n\n";
else if (computerChoice == 2)
cout << "Paper wraps rock, computer wins\n";
else if (computerChoice == 3)
cout << "Rock smashes scissors, you win\n";
}
if (userChoice == 2)
{
if (computerChoice == 2)
cout << "We have made the same choice. Rematch!\n\n\n\n";
else if (computerChoice ==1)
cout << "Paper wraps rock, you win\n";
else if (computerChoice == 3)
cout << "Scissors cut paper, computer wins\n";
}
if (userChoice == 3)
{
if (computerChoice == 3)
cout << "We have made the same choice. Rematch!\n\n\n\n";
else if (computerChoice == 2)
cout << "Scissors cut paper, you win\n";
else if (computerChoice == 1)
cout << "Rock smashes scissors, computer wins\n";

}
    }while(userChoice==computerChoice);//The loop continues until there is a winner

And also change the return value of the main method back to 0.

This worked, thank you so much for your help!!!

Just incase what you have in a do..while() loop.

do{//start the loop here
cout << "Rock, Paper, Scissors Game\n";
cout << "You are playing against the computer\n";
cout << "Type 1 for rock\n";
cout << "Type 2 for paper\n";
cout << "Type 3 for scissors\n";
cin >> userChoice; //User enters choice here
cout <<computerChoice<<endl;



if (userChoice == 1)
{
if (computerChoice == 1)
cout << "We have made the same choice. Rematch!\n\n\n\n";
else if (computerChoice == 2)
cout << "Paper wraps rock, computer wins\n";
else if (computerChoice == 3)
cout << "Rock smashes scissors, you win\n";
}
if (userChoice == 2)
{
if (computerChoice == 2)
cout << "We have made the same choice. Rematch!\n\n\n\n";
else if (computerChoice ==1)
cout << "Paper wraps rock, you win\n";
else if (computerChoice == 3)
cout << "Scissors cut paper, computer wins\n";
}
if (userChoice == 3)
{
if (computerChoice == 3)
cout << "We have made the same choice. Rematch!\n\n\n\n";
else if (computerChoice == 2)
cout << "Scissors cut paper, you win\n";
else if (computerChoice == 1)
cout << "Rock smashes scissors, computer wins\n";

}
    }while(userChoice==computerChoice);//The loop continues until there is a winner

And also change the return value of the main method back to 0.

Please give me some code that rock,paper and scissors

2 player will game and that who will raise or reach the 3 points will win!!!

its really a very nice struggle.its also vry helping.thanks and keep it up.<a href="http://jeuxdecuisine.webstarts.com/">jeux de cuisine</a>
<a href="http://jeuxdecuisine.yolasite.com/">jeux de cuisine</a>
<a href="http://jeuxdecuisine.iconosites.com/">jeux de cuisine</a>
<a href="http://jeuxdecuisine.blog.com/">jeux de cuisine</a>
<a href="http://jeuxdecuisine.edublogs.org/">jeux de cuisine</a>

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.