0

This is my first ever piece of code that ive developed independently and ive run into an issue. Ive Googled the issue and no prevail. I'm making a Rock Paper Scissors game and I have an input where you choose either Rock Paper or Scissors, after you choose, the program randomly outputs either Rock Paper or Scissors, now my issue is with the If statements after the random output, it only selects:

{
   cout << ", you lose! Do you want to play again? (Yes / No)";
}         

Whereas, this is my code here:

#include <iostream>
#include <string>
#include <stdlib.h>
#include <time.h>
using namespace std;

int main()
{
    // yn = yes / no
    string yn;
    cout << "Do you want to play Rock, Paper, Scissors? (Yes / No) ";
    cin >> yn;
    if (yn == "Yes")
    {
        // rps = rock paper scissors
        string rps;
        cout << "Choose, Rock, Paper, or Scissors\n";
        cin >> rps;

        if (rps == "Rock")
        {
            // Randomizer 
            srand(time(0));
            string rpslist[3] = { "Rock", "Paper", "Scissors " };
            int rpsnumber = rand() % 3;
            cout << "I choose: " << rpslist[rpsnumber];

            if (rpslist[1] == "Rock")
            {
                cout << ", we draw! Do you want to play again? (Yes / No)";
            }
            if (rpslist[2] == "Paper");
            {
                cout << ", you lose! Do you want to play again? (Yes / No)";
            }         
            if (rpslist[3] == "Scissors")
            {
                cout << ", you win! Do you want to play again? (Yes / No)";
            }
        }
2
  • You have a semicolon after your if check for "Paper". (Unrelated, you're not comparing with the right thing when checking for a winner.) Commented Apr 27, 2020 at 4:24
  • This isn't an issue here, but that call to stand(time(0)); should come before the program does any real work. In this case it doesn't' matter, but the idea is that you call it exactly once, and as your code develops so that it can play multiple rounds you'll have to move it. Commented Apr 27, 2020 at 13:36

1 Answer 1

1

I would like to point out a few things. The most important part that might fix your issue is the 3rd and 4th one. But do read all of them, I think it would be important to know.

1) When you check the elements of an array, you always start at index 0. So in this case, "Rock" would be corresponding to rpsList[0], Paper would be corresponding to rpsList[1], so on and so forth. So when you say if(rpsList[1] == "Rock") this is clearly wrong because rpsList[1] is Paper.

2) 1201ProgramAlarm has pointed this out, you have a semicolon after the if statement for paper.

3) Your if statements should be replaced with if-else-if statements instead. if(rpList[0] == " Rock) ... else if(rpList[1] == "Paper"). What happens is if you have individual if statements, it's going to check every if statement even if the conditions are false. Doing an else if statement after your first if statement will allow you to skip the redundant checks as soon as you find any of the statements to be true.

4) Your if statements do not do anything with the random number previously generated. Instead you're checking if the first element is a Rock, if the second element is Paper, or if your third element is Scissors, in which case they ARE in that sequence since that is how you arranged your string array. Do this instead:

if (rpList[rpsNumber] == "Rock")
{
...
}
else if (rpList[rpsNumber] == "Paper")
{
...
}
else if (rpList[rpsNumber] == "Scissors")
{
...
} 
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.