What am I missing?

For an assignment for my C++ class, I have to develop a program that you essentially play Rock, Paper, Scissors with the computer. I have this so far, but the computer's choice is always Scissors. I feel like it's one little mistake I'm not finding. It's due tomorrow.

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
#include<iostream>
#include<cstdlib>
using namespace std;
	 
	void playerChoice();
	void compChoice();
	void winner(int playerChoice, int compChoice);
	 
	int main()
	{
	    cout<<"Welcome to Rock Paper Scissors!\n"
	            
	            <<"You will be playing against the computer.\n"
	            <<"Enter R for rock, P for paper, and S for scissors.\n"
	            <<"Let's begin!\n"
	            <<'\n';
	 
	    playerChoice();
	    compChoice();
	 
	    return 0;
	}
	 
	void playerChoice()
	{
	        char playerChoice;
	 
	        cout <<"Enter your choice: ";
	        cin >> playerChoice;
	 
	        switch(playerChoice)
	        {
	            case 'r': case 'R':
	                cout<<"Player: Rock"<<endl;
	                break;
	            case 'p': case 'P':
	                cout<<"Player: Paper"<<endl;
	                break;
	            case 's': case 'S':
	                cout<<"Player: Scissors"<<endl;
	                break;
	            default:
	                cout<<"Play option does not exist!"<<endl;
	                break;
	        }
	}
	 
	void compChoice()
	{
	 
	    int compChoice;
	 
	    compChoice = rand()%3;
	 
	    if(compChoice == 0)
	    {
	        cout<<"Computer: Rock"<<endl;
	    }
	    else if(compChoice == 1)
    {
	        cout<<"Computer: Paper"<<endl;
	    }
	    else if(compChoice == 2)
	    {
	        cout<<"Computer: Scissors"<<endl;
	    }
		
	}
closed account (zb0S216C)
Both ::playerChoice() and ::compChoice() lack interaction. In otherwords, there's no way of comparing the two choices because the result is local to each function. Instead, return the choice from each function, then pass the choices to ::winner().

Wazzak
Guess I was wrong about being a little off. I'm not 100% sure what you're trying to tell me. I've tried returning the choices, but I get an error hence the break. And passing the choices to winner I don't get. My C++ professor is absolutely horrible with teaching, and answering questions in class so I always feel behind in class. If you can't explain that in more easier terms, then it's fine haha. I'll just wing it somehow.
closed account (zb0S216C)
Since both ::playerChoice() and ::compChoice() return void (in this context, it means nothing), it cannot return anything; the compiler won't allow it. Since both functions store the choice as an int, it would be logical to return an int from both functions. So, both functions should look something like:

1
2
3
4
5
6
7
8
9
10
int playerChoice();

// ...

int playerChoice()
{
    int Choice(0); // A
    // ...
    return(Choice);
}


Now, how do you use the values the functions returned? It's quite simple. When you call ::winner(), for each parameter of ::winner(), call ::playerChoice() and ::compChoice(). For instance:

1
2
3
4
5
int main()
{
    //...
    ::winner(::playerChoice(), ::compChoice()); // B
}

In this code, when ::winner() was called, the two other functions were called to. The values returned by both functions were given to the corresponding parameter of ::winner(). I'll dumb it down:

1
2
3
4
5
6
7
8
9
int Function_a() { return(0); }
int Function_b() { return(1); }

void Function_c(int A, int B) { }

int main()
{
    Function_c(Function_a(), Function_b());
}

Before Function_c() begins, both Function_a() and Function_b() are called. When Function_a() finishes, the compiler places the value it returned into Function_c()'s A parameter. When Function_b() finishes, the compiler places the value it returned into Function_c()'s B parameter. When both functions are finished, Function_c() begins. At this point, A and B contain 0 and 1, respectively.

Additionally, data returned from a function can also be placed within variables, like so:

1
2
3
4
5
6
7
int Function() { return(0); }

int main()
{
    int My_int = Function();
    // My_int now equals zero.
}

Notes:

A) The parentheses ((...)) used in this way is called functional notation. It's equivalent to int a = 0.
B) The double colon proceeding a name indicates that the function/variable is global.

Wazzak
Last edited on
I understand what you're telling me to do. I have changed the values to int, kept the winner function void and made some other changes. Program lists no errors, but won't compile anymore. Everything seemed okay before except the fact the the computers choice wasn't being randomized and choice was always Scissors. You really don't have to give more input, just about at the point where I give up. Code was fun at first but it's just a pain in the ass now.
closed account (zb0S216C)
When std::rand() fails to produce a random number, it means you haven't seeded std::rand(). By this, I mean you have to set a range from which std::rand() can choose from. Like so:

1
2
3
4
5
6
7
#include <ctime>

int main()
{
    std::srand(std::time(0)); 
    int Choice = (std::rand() % 3);
}

Wazzak
Last edited on
Okay I now have this:
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
#include<iostream>
#include<cstdlib>
#include<ctime>
using namespace std;
	 
	int playerChoice();
	int compChoice();
	void winner(int playerChoice, int compChoice);
	 
	int main()
	{
	    cout <<"Welcome to Rock Paper Scissors!\n"
	            
	            <<"You will be playing against the computer.\n"
	            <<"Enter R for rock, P for paper, and S for scissors.\n"
	            <<"Let's begin!\n"
	            <<'\n';
	 
	    playerChoice();
	    compChoice();
		
	 
	    return 0;
	}
	 
	int playerChoice()
	{
	        char playerChoice;
	 
	        cout <<"Enter your choice: ";
	        cin >> playerChoice;
	 
	        switch(playerChoice)
	        {
	            case 'r': case 'R':
	                cout<<"Player: Rock"<<endl;
	                break;
	            case 'p': case 'P':
	                cout<<"Player: Paper"<<endl;
	                break;
	            case 's': case 'S':
	                cout<<"Player: Scissors"<<endl;
	                break;
	            default:
	                cout<<"Play option does not exist!"<<endl;
	                return 1;
	        }
	}
	 
	int compChoice()
	{
	 
		std::srand(std::time(0)); 
		
	    int compChoice = rand() % 3;
	 
	    if(compChoice == 0)
	    {
	        cout<<"Computer: Rock"<<endl;
	    }
	    else if(compChoice == 1)
    {
	        cout<<"Computer: Paper"<<endl;
	    }
	    else if(compChoice == 2)
	    {
	        cout<<"Computer: Scissors"<<endl;
	    }
		return 2;
	
	}


It generates the random selection for the computer now, which was my first problem. How would I go about printing out "You win!" or "You lose! Try again." but having the program know what beats what? Thanks for the help so far :')

Output:
1
2
3
4
5
6
7
8
9
Welcome to Rock Paper Scissors!
You will be playing against the computer.
Enter R for rock, P for paper, and S for scissors.
Let's begin!

Enter your choice: P
Player: Paper
Computer: Rock
Press any key to continue . . . 


It just states what both selections were, but doesn't declare a winner. I probably know how to do this but my brain is shot from looking at this for the past 4 hours.
Last edited on
closed account (zb0S216C)
Since ::winner() determines who actually won (assuming), then it would be logical to actually call it. Instead of calling ::playerChoice() and ::compChoice() as independent functions, call them in this way:

1
2
3
4
int main()
{
    ::winner(::playerChoice(), ::compChoice());
}

I already explained this above.

Wazzak
Topic archived. No new replies allowed.