Problem Initializing Loop And Returning Values Rock Paper Scissors

When i return 0; at the function int userselect (int user, int comp)my output displays twice and when i ask the user to run it again the computers selection of rock, paper or scissors remains the same it doesn't find a new random number. So i made the function userselect return to main(). This works and my output is displayed once, but when i run it it skips my while part of my do while loop completely so it loops the program without asking the user if they wish to run it again.

but when i return main() the computers choice re-randomizes every time so i need to find a way to get the while part of my do while to initialize and the program to not loop without the option of ending it. Help?!

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
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <ctime>

using namespace std;
char again = 'y';
int userselect (int, int);

int main()
{
	//list of any global variables or constants
	unsigned seed = time(0);
        srand(seed);
        int compselect = rand() % 3 + 1;
		const int rock = 1;
		const int paper = 2;
		const int scissors = 3;
	do
	{
		//list variables for program
		//variables needed for rock, paper, scissosrs choices, random number generator, 
		//computer name, user name, and the table   
		
		int choice = 0;
		char compname = 'X'; 	

cout << "Welcome to Professor X's Rock, Paper, Scissors Game. " << endl;
cout << "Your presence here means you wish to challenge the Professor. " << endl; 
cout << "Well then, let's begin. " << endl << endl;


		cout << "\txxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n";
		cout << "\t*         Make your choice        *\n";
		cout << "\t*        -------------------      *\n";
		cout << "\t*                                 *\n";
		cout <<	"\t*             1 - Rock            *\n";
		cout << "\t*             2 - Paper           *\n";
		cout << "\t*             3 - Scissors        *\n";
		cout << "\t*                                 *\n";
		cout << "\txxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" << endl << endl;
		cin >> choice;
		cout << endl << endl;
                
                //Display the results
                userselect(choice, compselect);

		//input validation
	while (choice < 1 || choice > 3)
	{
		cout << "\tThere is no tricking the Professor." << endl << endl;
		cout << "\txxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n";
		cout << "\t*         Re-Enter your choice    *\n";
		cout << "\t*        -------------------      *\n";
		cout << "\t*                                 *\n";
		cout <<	"\t*             1 - Rock            *\n";
		cout << "\t*             2 - Paper           *\n";
		cout << "\t*             3 - Scissors        *\n";
		cout << "\t*                                 *\n";
		cout << "\txxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n" << endl << endl;
		cin >> choice;
		cout << endl << endl;
	}

                //Display the results
                userselect(choice, compselect);

	cout << "Do you wish to challenge the Professor once more?" << endl;
	cout << "Type Y/y for yes and N/n for no" << endl << endl;
	cin >> again;
	}while(again == 'Y' || again == 'y');

	system("pause");
	return 0;

}

int userselect (int user, int comp)
{
         // If user enters Rock
        if (user == 1)
        {
                if (comp == 1)
                {
                        cout << "You picked Rock \n";
                        cout << "The computer picked Rock \n";
                        cout << "TIE! \n\n";
						cout << "-------------------------------------------------------------\n" << endl;
				}
                else if (comp == 2)
                {
                        cout << "You picked Rock \n";
                        cout << "The computer picked Paper \n";
                        cout << "Paper beats Rock. Bummer. YOU LOSE! \n\n";
						cout << "-------------------------------------------------------------\n" << endl;
                }
                else if (comp == 3)
                {
                        cout << "You picked Rock \n";
                        cout << "The Professor picked Scissors \n";
                        cout << "Rock beats Scissors. Awesome. YOU WIN! \n\n";
						cout << "-------------------------------------------------------------\n" << endl;
                }
        }

        // If user enters Paper
        if (user == 2)
        {
                if (comp == 1)
                {
                        cout << "You picked Paper \n";
                        cout << "The computer picked Rock \n";
                        cout << "Paper beats Rock. Ding Ding Ding! YOU WIN! \n\n";
						cout << "-------------------------------------------------------------\n" << endl;
                }
                else if (comp == 2)
                {
                        cout << "You picked Paper \n";
                        cout << "The computer picked Paper \n";
                        cout << "TIE! \n\n";
						cout << "-------------------------------------------------------------\n" << endl;
                }
                else if (comp == 3)
                {
                        cout << "You picked Paper \n";
                        cout << "The computer picked Scissors \n";
                        cout << "Scissors beats Paper. Winners win. YOU LOSE! \n\n";
						cout << "-------------------------------------------------------------\n" << endl;
                }
}

        // If user enters Scissors
        if (user == 3)
        {
                if (comp == 1)
                {
                        cout << "You picked Scissors \n";
                        cout << "The computer picked Rock \n";
                        cout << "Rock beats Scissors. Do better. YOU LOSE! \n\n";
						cout << "-------------------------------------------------------------\n" << endl;
                }
                else if (comp == 2)
                {
                        cout << "You picked Scissors \n";
                        cout << "The computer picked Paper \n";
                        cout << "Scissors beat Paper. Surprise. YOU WIN! \n\n";
						cout << "-------------------------------------------------------------\n" << endl;
                }
                else if (comp == 3)
                {
                        cout << "You picked Scissors \n";
                        cout << "The computer picked Scissors \n";
                        cout << "TIE! \n\n";
						cout << "-------------------------------------------------------------\n" << endl;
                }
        }
		
return main();
}
Last edited on
I got it, when you return 0, you get the output twice because you call the function twice: line 48 and line 68. I would delete line 48, and then you can use return 0 at the end of your userselect() function.

But what you should really do is declare your function void. Because your function should not return anything. Declaring the function void, you don't have any return statement in it.

It kind of work when you returned main(), because you were calling the main function inside the main function, and so everything was recursive, and you were actually not seeing the second call to userselect()

Hope it helps.
Topic archived. No new replies allowed.