Ideas for loop for rock paper scissors program?

Note: this isn't done yet. I have to put in my if-then statements. Though, that's unrelated to my question. I just wanted to make sure it was known.

One of the things we have to do is that if the string inputed from the user is the same as the string produced by the computer (or basically if the rock paper scissors game is a draw) is make it so that each time it's a draw it makes the user input a string again and have the process repeat all over.

This is my original code.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//Ashton Dreiling
//Rock paper scissors exercise
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
using namespace std;
const int ONE_FOR_CAL=1;
const int THREE_FOR_CAL=3;
const int TWO_FOR_CAL=2;

void switchStatements(int number, string &rockPaperScissorsStatus, string rock, string paper, string scissors);
bool ifThenStatements(string userInput, string rockPaperScissorsStatus);


int main()
{
    string userInput="rock";
    int number=0;
    bool status;
    string rockPaperScissorsStatus="rock";
    string rock;
    string paper;
    string scissors;

    cout << "Today, you will be playing rock, paper, scissors against a computer!" << endl;

    srand(time(0));

    number=rand()% THREE_FOR_CAL + ONE_FOR_CAL;

    cout <<"Please enter rock, paper, or scissors." << endl;
    cin >> userInput;

    switchStatements(number, rockPaperScissorsStatus, rock, paper, scissors);
    status=ifThenStatements(userInput, rockPaperScissorsStatus);
    

    return 0;

}//end main

void switchStatements(int number, string &rockPaperScissorsStatus, string rock, string paper, string scissors)

{
    switch(number)

    {
    case ONE_FOR_CAL:
        cout << "The computer has chosen rock." << endl;
        rockPaperScissorsStatus=rock;
        break;

    case TWO_FOR_CAL:
        cout << "The computer has chosen paper." << endl;
        rockPaperScissorsStatus=paper;
        break;
    case THREE_FOR_CAL:
        cout << "The computer has chosen scissors." << endl;
        rockPaperScissorsStatus=scissors;
        break;
    }//end switch statements
}//end of switchStatements

bool ifThenStatements(string userInput, string rockPaperScissorsStatus)
{


}//end ifThenStatements 


Now, I had an idea for this. I made a do while loop and focused on a small section of my code.

1
2
3
4
5
6
7
8
9
 
do
{
    cout <<"Please enter rock, paper, or scissors." << endl;
    cin >> userInput;

    switchStatements(number, rockPaperScissorsStatus, rock, paper, scissors);
    status=ifThenStatements(userInput, rockPaperScissorsStatus);
}while(userInput==rockPaperScissorsStatus);


I'm aware this would work. However, I want to make it to where the user will be informed why they're being asked to put in a string input again, so I tried to make it like this.

1
2
3
4
5
6
7
8
9
10
do
{
    cout << "It was a draw! You must play until it's not." << endl;

    cout <<"Please enter rock, paper, or scissors." << endl;
    cin >> userInput;

    switchStatements(number, rockPaperScissorsStatus, rock, paper, scissors);
    status=ifThenStatements(userInput, rockPaperScissorsStatus);
}while(userInput==rockPaperScissorsStatus);


The only issue with this is since it's a do while loop, that cout statement will execute once even regardless of the condition meaning when the user FIRST plays the cout statement will show.

I only want it to show after the condition of (userInput==rockPaperScissorsStatus) is true.

I've tried different variations of loops with do while and while with if-then statements, but I can't seem to make it to where the cout statement shows only after one game has been played and the userInput and the rockPaperScissorsStatus is the same.

Hopefully I'm not being too confusing.


You can put your code in a while loop that ends only when the result is not a draw.

1
2
3
4
5
6
7
bool gameRunning = true;
while(gameRunning)
{
   // play game
   if game is not draw
      gameRunning = false;
};
Thanks for replying so quickly. <3

Do you have any other ideas that could easily be implemented in my code?

I'm not sure how I'd put my entire code in a function according to what you said or perhaps I'm a bit confused by what you mean.

Line 22-24: rock, paper, scissors are all empty strings.

Line 35: You're passing these empty strings to switchStatements.

Line 46: Regardless of what number is chosen, you going to set rockPaperScissorsStatus to an empty string.
Last edited on
Thanks for pointing that out.
I set rock="rock"
paper="paper"
and scissors="scissors"

I believe that should fix the problem.

Do you have any ideas for my original post?
Oh, and by the way anon, this is my edited code with my if-then statements completed.

Edit:

I got it to do what I wanted, but now when I execute it, the program keeps choosing rock.

Edit:

I fixed it! srand was in the do loop scope. I also took Thomas's advice and put a part of my code in a function and reused some functions to make it work the way asked.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
//Ashton Dreiling
//Rock paper scissors exercise
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <stdlib.h>

using namespace std;
const int ONE_FOR_CAL=1;
const int THREE_FOR_CAL=3;
const int TWO_FOR_CAL=2;

void switchStatements(int number, string &rockPaperScissorsStatus, string rock, string paper, string scissors);
void playGameAgain (string userInput, string rockPaperScissorsStatus, string rock, string paper, string scissors, int number);
void ifThenStatements(string userInput, string rockPaperScissorsStatus);


int main()
{
    int number=0;
    string rockPaperScissorsStatus;
    string rock="rock";
    string paper="paper";
    string scissors="paper";
    string doAgain;
    string userInput;

    cout << "Today, you will be playing rock, paper, scissors against a computer!" << endl;

    do
    {
        srand(time(0));

        number=rand()% THREE_FOR_CAL + ONE_FOR_CAL;

        cout <<"Please enter rock, paper, or scissors." << endl;
        cin >> userInput;

        switchStatements(number, rockPaperScissorsStatus, rock, paper, scissors);
        ifThenStatements(userInput, rockPaperScissorsStatus);

        if(userInput==rockPaperScissorsStatus)
        {
            playGameAgain(userInput, rockPaperScissorsStatus, rock, paper, scissors, number);
        }

        cout << "Would you like to play again?" << endl;
        cin >> doAgain;
    }
    while (doAgain=="yes" || "YES" || "Yes");

    system("Pause");

    return 0;

}//end main

void switchStatements(int number, string &rockPaperScissorsStatus, string rock, string paper, string scissors)

{
    switch(number)

    {
    case ONE_FOR_CAL:
        cout << "The computer has chosen rock." << endl;
        rockPaperScissorsStatus="rock";
        break;

    case TWO_FOR_CAL:
        cout << "The computer has chosen paper." << endl;
        rockPaperScissorsStatus="paper";
        break;
    case THREE_FOR_CAL:
        cout << "The computer has chosen scissors." << endl;
        rockPaperScissorsStatus="scissors";
        break;
    }//end switch statements
}//end of switchStatements

void ifThenStatements(string userInput, string rockPaperScissorsStatus)
{
    if (userInput==rockPaperScissorsStatus)
    {
        cout << "It's a draw." << endl;
    }

    else if (userInput=="rock" && rockPaperScissorsStatus=="scissors")
    {
        cout << "You won! Rock smashes scissors." << endl;
    }

    else if (userInput=="rock" && rockPaperScissorsStatus=="paper")
    {
        cout << "You lose. Paper covers rock." << endl;
    }

    else if (userInput=="scissors" && rockPaperScissorsStatus=="paper")
    {
        cout << "You win. Scissors cuts paper." << endl;
    }

    else if (userInput=="scissors" && rockPaperScissorsStatus=="rock")
    {
        cout << "You lose. Rock crushes scissors." << endl;
    }

    else if (userInput=="paper" && rockPaperScissorsStatus=="rock")
    {
        cout << "You win. Paper covers rock." << endl;
    }

    else if (userInput=="paper" && rockPaperScissorsStatus=="scissors")
    {
        cout << "You lose. Scissors cuts paper." << endl;
    }



}//end ifThenStatements

void playGameAgain(string userInput, string rockPaperScissorsStatus, string rock, string paper, string scissors, int number)
{
    do
    {
        cout << "You must continue to play until it's no longer a draw." << endl;
        cout <<"Please enter rock, paper, or scissors." << endl;
        cin >> userInput;

        switchStatements(number, rockPaperScissorsStatus, rock, paper, scissors);
        ifThenStatements(userInput, rockPaperScissorsStatus);
    }
    while(userInput==rockPaperScissorsStatus);
}
Last edited on
Line 33: Do not call srand() within a loop or a random number function. srand() sets the RNG to a particular starting point. Calling srand() repeatedly can cause the RNG to return the same random numbers. srand() should be called ONCE at the beginning of main().
http://www.cplusplus.com/reference/cstdlib/srand/
Per your edit, you've fixed this. Not reflected in your posted code though.

Line 51: C++ does not support implied left hand side in conditionals. You must fully specify the conditions.
Example:
if (ans == 'Y' || 'y') evaluates as if ((ans == 'Y') || ('y')) ('y') always evaluates to 1 (true), therefore the if statement is always true.

Line 38,128: You don't edit the user input to determine if it is valid or not. e.g. If I enter "Rock", that's not detected as a valid response. If I enter xxx, it simply asks if I want to play again.

Line 117: You should have an else clause in case none of the if/else clauses are executed.

IMO, switchStatements and ifThenStatements are poorly named. They should be named for what they DO. Not how they are implemented. Suggestion: ComputerMove() and DetermineWinner().
Last edited on
Thanks for the advice. I tried to answer this rock question in another post, so it's nice you replied.
I believe I fixed 38 and 128.
I believed I fixed 51.
I fixed 33.
i'm not sure yet what you mean by I should have an else clause.

Sorry if it's not obvious to me, yet.

If I type in rock, the computer continues to type in rock. That's still not fixed which is odd because it should, so I must be missing something.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163

//Ashton Dreiling
//Rock paper scissors exercise
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <stdlib.h>

using namespace std;

//global constants 
const int ONE_FOR_CAL=1;
const int THREE_FOR_CAL=3;
const int TWO_FOR_CAL=2;

//function prototypes 
void switchStatements(int number, string &rockPaperScissorsStatus, string rock, string paper, string scissors);
void playGameAgain (string userInput, string rockPaperScissorsStatus, string rock, string paper, string scissors, int number);
void ifThenStatements(string userInput, string rockPaperScissorsStatus);
void inputValidation (string &userInput);


int main()
{
    //some variables 
    int number=0;
    string rockPaperScissorsStatus;
    string rock="rock";
    string paper="paper";
    string scissors="paper";
    string doAgain;
    string userInput;
    srand(time(0));


    cout << "Today, you will be playing rock, paper, scissors against a computer!" << endl;

    do
    {
        //number for switch statements
        number=rand()% THREE_FOR_CAL + ONE_FOR_CAL;

        cout <<"Please enter rock, paper, or scissors." << endl;
        cin >> userInput;
        
        //to convert what user types in first to lower case
        for(int i = 0; i < userInput.length(); i++)
            userInput[i] = tolower(userInput[i]);
        //function to validate if user types rock paper scissors 
        inputValidation(userInput);
        //function to set randomly generated numbers 1-3 t strings
        switchStatements(number, rockPaperScissorsStatus, rock, paper, scissors);
        //function to determine who is the winner
        ifThenStatements(userInput, rockPaperScissorsStatus);
        
        if(userInput==rockPaperScissorsStatus)
        {
            playGameAgain(userInput, rockPaperScissorsStatus, rock, paper, scissors, number);
        }//if then statements to determine if game is played again based off of input

        cout << "Would you like to play again?" << endl;
        cin >> doAgain;
    }//end of do while loop to determine if user plays again
    while (doAgain=="yes" || doAgain=="YES" || doAgain=="Yes");

    system("Pause");

    return 0;

}//end main

void switchStatements(int number, string &rockPaperScissorsStatus, string rock, string paper, string scissors)

{
    switch(number)

    {
    case ONE_FOR_CAL:
        cout << "The computer has chosen rock." << endl;
        rockPaperScissorsStatus="rock";
        break;

    case TWO_FOR_CAL:
        cout << "The computer has chosen paper." << endl;
        rockPaperScissorsStatus="paper";
        break;
    case THREE_FOR_CAL:
        cout << "The computer has chosen scissors." << endl;
        rockPaperScissorsStatus="scissors";
        break;
    }//end switch
}//end of switchStatements

void ifThenStatements(string userInput, string rockPaperScissorsStatus)
{
    if (userInput==rockPaperScissorsStatus)
    {
        cout << "It's a draw." << endl;
    }//end if then statement 

    else if (userInput=="rock" && rockPaperScissorsStatus=="scissors")
    {
        cout << "You won! Rock smashes scissors." << endl;
    }// end else if statement

    else if (userInput=="rock" && rockPaperScissorsStatus=="paper")
    {
        cout << "You lose. Paper covers rock." << endl;
    }// end else if statement 

    else if (userInput=="scissors" && rockPaperScissorsStatus=="paper")
    {
        cout << "You win. Scissors cuts paper." << endl;
    }//end else if statement

    else if (userInput=="scissors" && rockPaperScissorsStatus=="rock")
    {
        cout << "You lose. Rock crushes scissors." << endl;
    }//end else if statement 

    else if (userInput=="paper" && rockPaperScissorsStatus=="rock")
    {
        cout << "You win. Paper covers rock." << endl;
    }//end else if statement 

    else if (userInput=="paper" && rockPaperScissorsStatus=="scissors")
    {
        cout << "You lose. Scissors cuts paper." << endl;
    }//end else if statement 



}//end ifThenStatements

void playGameAgain(string userInput, string rockPaperScissorsStatus, string rock, string paper, string scissors, int number)
{
    do
    {
        cout << "You must continue to play until it's no longer a draw." << endl;
        cout <<"Please enter rock, paper, or scissors." << endl;
        cin >> userInput;

        switchStatements(number, rockPaperScissorsStatus, rock, paper, scissors);
        ifThenStatements(userInput, rockPaperScissorsStatus);
    }//end do while loop to determine if user is entering rock paper or scissors 
    while(userInput==rockPaperScissorsStatus);
}//end playGameAgain

void inputValidation(string &userInput)
{

    while(userInput!="paper" && userInput !="rock" && userInput!="scissors")
    {
        cout << "Sorry, but you must enter rock, paper, or scissors." << endl;
        cin >> userInput;
        
        //convert user input to all lower case
        for(int i = 0; i < userInput.length(); i++)
            userInput[i] = tolower(userInput[i]);
    }//end while loop inputValidation 

}// end inputvalidation  
Last edited on
Line 130: regarding else clause. Now that you have an inputValidation function, the else clause isn't really needed since all possible combinations are covered by the if/else tree.

Line 142: inputValidation not called. Nor is userInput downshifted.

You're now getting userInput in three different places. I strongly suggest that you get userInput in only one place. I think this is the reason that you missed the call to inputValidation() at line 142.

If I type in rock, the computer continues to type in rock.

I played several rounds of your game, and the computer always seemed to come up with a random selection. I don't see a problem.

Last edited on
Sorry I didn't understand. I got it now!

Alright, so, in the future I'll make sure there is only one place where you put input. For now, I'll leave it the way it is.

The rock problem occurs when it's a draw.

http://prntscr.com/bsf27y

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//Ashton Dreiling
//Rock paper scissors exercise
#include <iostream>
#include <string>
#include <cstdlib>
#include <ctime>
#include <stdlib.h>

using namespace std;

//global constants
const int ONE_FOR_CAL=1;
const int THREE_FOR_CAL=3;
const int TWO_FOR_CAL=2;

//function prototypes
void switchStatements(int number, string &rockPaperScissorsStatus, string rock, string paper, string scissors);
void playGameAgain (string userInput, string rockPaperScissorsStatus, string rock, string paper, string scissors, int number);
void ifThenStatements(string userInput, string rockPaperScissorsStatus);
void inputValidation (string &userInput);


int main()
{
    //some variables
    int number=0;
    string rockPaperScissorsStatus;
    string rock="rock";
    string paper="paper";
    string scissors="scissors";
    string doAgain;
    string userInput;
    srand(time(0));


    cout << "Today, you will be playing rock, paper, scissors against a computer!" << endl;

    do
    {
        //number for switch statements
        number=rand()% THREE_FOR_CAL + ONE_FOR_CAL;

        cout <<"Please enter rock, paper, or scissors." << endl;
        cin >> userInput;

        //to convert what user types in first to lower case
        for(int i = 0; i < userInput.length(); i++)
            userInput[i] = tolower(userInput[i]);
        //function to validate if user types rock paper scissors
        inputValidation(userInput);
        //function to set randomly generated numbers 1-3 t strings
        switchStatements(number, rockPaperScissorsStatus, rock, paper, scissors);
        //function to determine who is the winner
        ifThenStatements(userInput, rockPaperScissorsStatus);

        if(userInput==rockPaperScissorsStatus)
        {
            playGameAgain(userInput, rockPaperScissorsStatus, rock, paper, scissors, number);
        }//if then statements to determine if game is played again based off of input

        cout << "Would you like to play again?" << endl;
        cin >> doAgain;
    }//end of do while loop to determine if user plays again
    while (doAgain=="yes" || doAgain=="YES" || doAgain=="Yes");

    system("Pause");

    return 0;

}//end main

void switchStatements(int number, string &rockPaperScissorsStatus, string rock, string paper, string scissors)

{
    switch(number)

    {
    case ONE_FOR_CAL:
        cout << "The computer has chosen rock." << endl;
        rockPaperScissorsStatus="rock";
        break;

    case TWO_FOR_CAL:
        cout << "The computer has chosen paper." << endl;
        rockPaperScissorsStatus="paper";
        break;
    case THREE_FOR_CAL:
        cout << "The computer has chosen scissors." << endl;
        rockPaperScissorsStatus="scissors";
        break;
    }//end switch
}//end of switchStatements

void ifThenStatements(string userInput, string rockPaperScissorsStatus)
{
    if (userInput==rockPaperScissorsStatus)
    {
        cout << "It's a draw." << endl;
    }//end if then statement

    else if (userInput=="rock" && rockPaperScissorsStatus=="scissors")
    {
        cout << "You won! Rock smashes scissors." << endl;
    }// end else if statement

    else if (userInput=="rock" && rockPaperScissorsStatus=="paper")
    {
        cout << "You lose. Paper covers rock." << endl;
    }// end else if statement

    else if (userInput=="scissors" && rockPaperScissorsStatus=="paper")
    {
        cout << "You win. Scissors cuts paper." << endl;
    }//end else if statement

    else if (userInput=="scissors" && rockPaperScissorsStatus=="rock")
    {
        cout << "You lose. Rock crushes scissors." << endl;
    }//end else if statement

    else if (userInput=="paper" && rockPaperScissorsStatus=="rock")
    {
        cout << "You win. Paper covers rock." << endl;
    }//end else if statement

    else if (userInput=="paper" && rockPaperScissorsStatus=="scissors")
    {
        cout << "You lose. Scissors cuts paper." << endl;
    }//end else if statement



}//end ifThenStatements

void playGameAgain(string userInput, string rockPaperScissorsStatus, string rock, string paper, string scissors, int number)
{
    do
    {
        cout << "You must continue to play until it's no longer a draw." << endl;
        cout <<"Please enter rock, paper, or scissors." << endl;
        cin >> userInput;


        inputValidation(userInput);
        switchStatements(number, rockPaperScissorsStatus, rock, paper, scissors);
        ifThenStatements(userInput, rockPaperScissorsStatus);


    }//end do while loop to determine if user is entering rock paper or scissors
    while(userInput==rockPaperScissorsStatus);
}//end playGameAgain

void inputValidation(string &userInput)
{

    while(userInput!="paper" && userInput !="rock" && userInput!="scissors")
    {
        cout << "Sorry, but you must enter rock, paper, or scissors." << endl;
        cin >> userInput;

        //convert user input to all lower case
        for(int i = 0; i < userInput.length(); i++)
            userInput[i] = tolower(userInput[i]);
    }//end while loop inputValidation

}// end inputvalidation 
Last edited on
The rock problem occurs when it's a draw.

Ah. playGameAgain() doesn't generate a new random number.

That fixed everything! Thanks for all your help!
Topic archived. No new replies allowed.