Rock Paper Scissors!

So, I'm not yet worried about giving the option to quit, I just want to at least be able to have the program post the results to the console. I've tried a couple of different things, but it seems that the "string result(...)" subprogram is the issue here. Before, I was getting the "term cannot evaluate to a function..." error. I changed it over to string, because I want the output to be a line of text, but when I add cout << whoWins << endl; to main, whoWins being the function, it will not compile! What am I doing wrong?!?

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

using namespace std;


int userChoice() 
{ 
  char uChoice;

    cout << "Enter R for rock, P for paper, and S for scissors, or Q to quit: ";
    cin >> uChoice;

    if (toupper(uChoice) == 'R')
      {
        uChoice = '0';
      }
    else if (toupper(uChoice) == 'S')
      {
        uChoice = '1';
      }
    else if (toupper(uChoice) == 'P')
      {
        uChoice = '2';
      }

    else
      {
        cout << "Invalid choice, try again." << endl;        
      }
    return uChoice;
} //userChoice

int compChoice()
{

  srand(time(0)); 
  rand();

  int cChoice = rand() % 3;

  if (cChoice == 0)
    {
      cout << "The computer chooses 'Rock'." << endl;
    }
  if (cChoice == 1)
    {
      cout << "The computer chooses 'Paper'." << endl;
    }
  if (cChoice == 2)
    {
      cout << "The computer chooses 'Scissors'." << endl;
    }   

  return cChoice; 
}

string result(int usersChoice, int compsChoice)
{
string winner;
   if (usersChoice == 0 && compChoice == 0)
   {
     winner = "Go again!\n";
   }
   else if (usersChoice == 0 && compsChoice == 1)
   {
     winner =  "Computer wins.\n";
   }
   else if (usersChoice == 0 && compsChoice == 2)
   {
     winner =  "You win!\n";
   }
   else if (usersChoice == 1 && compsChoice == 0)
   {
     winner =  "You win!\n";
   }
   else if (usersChoice == 1 && compsChoice == 1)
   {
     winner =  "Go again!\n";
   }
   else if (usersChoice == 1 && compsChoice == 2)
   {
     winner =  "Computer wins.\n";
   }
   else if (usersChoice == 2 && compsChoice == 0)
   {
     winner =  "Computer wins.\n";
   }
   else if (usersChoice == 2 && compsChoice == 1)
   {
     winner =  "You win!\n";
   }
   else if (usersChoice == 2 && compsChoice == 2)
   {
     winner =  "Go again!\n";
   }
   return winner; 
}

int main()
{
    int usersChoice;
    int compsChoice;
    string whoWins;

    usersChoice = userChoice();
    compsChoice = compChoice();
    whoWins = result(usersChoice, compsChoice);

    cout << whoWins << endl;

    return 0;
} 
Last edited on
one problem
1
2
3
4
string result(int usersChoice, int compsChoice)
{
string winner;
   if (usersChoice == 0 && compChoice == 0)


different names.

My compiler do not like names of function to be same as variables names, but compiles.
Problem two:

1
2
3
int userChoicef() 
{ 
  char uChoice;


return uChoice;

Your function must return int type, but you give to return a char type.
Problem #1, yeah that was just a typo - thanks!
Problem #2 - I need the output to be something that I can compare with the computer's choice. If I make it an integer, then I can't accept R, P, or S as input correct?

My biggest issue here is that at line 111, I get a compile error (well a ton of them actually) because it doesn't like that one line. Take that out and it's fine, but the program terminates after accepting the user choice. It just states what the computer chose, then terminates.

Thanks again for your help!
When you get a compile error, you always need to post the exact error message.
Ahhhh... looks like i just needed to add #include <string> ....duh! It works fine now, I just need to add a scorekeeper, and an option to quit - shouldn't be to hard. I'll post if I have issues.
Problem 3:

in userChoice() paper is number 2, in compChoice() paper is 1;
Problem #2 - I need the output to be something that I can compare with the computer's choice. If I make it an integer, then I can't accept R, P, or S as input correct?

no. You can't do it. Because '1' are not equal to 1. '1' is equal to 42 or so. So you can't do it.

But you can change
uChoice = '0';
to
return 0;

uChoice = '1';
to
return 1;

uChoice = '2';
to
return 2;
Last edited on
Ahhhh... looks like i just needed to add #include <string> ....duh! It works fine now, I just need to add a scorekeeper, and an option to quit - shouldn't be to hard. I'll post if I have issues.


My compiler do not said anything about missing #include <string> :D
don't know way :S
And program worked.
Topic archived. No new replies allowed.