Trying to implement functions to identify user/computer selections

Hi, I'm trying to make a program that allows the user to play rock paper scissors against the computer. I have it working perfectly without the functions, but I wanted to add them in to help the user see what the selections in the output were. ie: instead of just saying "You won!" say, "You won! Rock beats scissors!"

I'm not sure if I'm not formatting the functions correctly or if I'm not using the right kind of function since I keep getting error messages when I try and compile it. I've tried changing identifyH's syntax in the main function (lines 45, 49, 54, 59)these ways so far: identifyH(human), identifyH(), identifyH(x), x
and the same for the identifyC function.

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

using namespace std;

void pause(double dur); //enables pausing
int identifyH(float x); //supposed to identify if the user input rock paper or scissors based upon number selection
int identifyC(float y); //supposed to identify the computer's selection

int main(int argc, char *argv[])
{
   srand(time(0)); //enables the computer to pick a random number
   
   int yon, human, computer;  //yon lets the user decide if they want to play (yes or no)
                              //human is the user's selection
                              //computer is the computer's selection
                              
   float x=human, y=computer; //unsure if needed or not, I tried using it for the functions since they kept on not being recognized
   
   //prompts user if they want to play
   cout<<"Lets play rock, paper, scissors!"<<endl;
   cout<<"Do you want to play? (Enter 1 for yes, or 0 for no): ";
   cin>>yon;
   //as long as the user says yes(1) it starts a game of rock paper scissors
   while(yon == 1)
   {
      cout<<"Okay! Choose 1 for Rock, 2 for Paper, or 3 for Scissors, then press [ENTER]."<<endl;
      pause(2);
      cout<<"Get ready to play!"<<endl;
      pause(0.5);
      cout<<"1..."<<endl;
      pause(0.5);
      cout<<"2..."<<endl;
      pause(0.5);
      cout<<"3..."<<endl;
      pause(0.5);
      cout<<"Shoot!: ";
      cin>>human;    //user input
      
      computer=rand()%3+1; //computer's random selection from 1 to 3
      
      //if the user and computer pick the same number
      if(human==computer)  cout<<"Huh, seems neither of us wins... I mean, since "<<identifyH(human)<<" vs "<<identifyC(computer)<<" is like you punching yourself in the face! Pretty funny, but nothing happens. ;)"<<endl;
      //if the computer has the winning number
      else if((human==1 && computer==2) || (human==2 && computer==3) || (human==3 && computer==1))
      {
         cout<<"Ha, ha! Looks like I win! "<<identifyC(computer)<<" beats "<<identifyH(human)<<" :P"<<endl<<"Better luck next time!"<<endl;
      }
     //if the user has the winning number
      else if((human==1 && computer==3) || (human==2 && computer==1) || (human==3 && computer==2))
      {
         cout<<"Well played, you sneaky devil! "<<identifyH(human)<<" beats "<<identifyC(computer)<<"!"<<endl<<"I'll beat you next time for sure!"<<endl;
      }
      // if the user selects a number other than 1, 2, or 3
      else
      {
         cout<<"Well, this wasn't supposed to happen! But, I'm pretty sure one of us is cheating. "<<identifyH(human)<<" vs "<<identifyC(computer)<<" doesn't seem like a fair match."<<endl;
      }
   
   
   //prompts user for input if they want to play again, 1 for yes, and anything else really for no.
   cout<<"Do you want to play again? (Enter 1 for yes or 0 for no): ";
   cin>>yon;
   
   }
   //Signals the end of the program
   cout<<"Goodbye!"<<endl;
   
   cout<<"Press the [ENTER] key to continue: ";
   cin.get();
   
   return(0);
}
//pause function
void pause(double dur)
{
   double temp = time(NULL) + dur;
   while(temp > time(NULL));
}
//function to identify user's choice
int identifyH(float x)
{
   if(human==1) x='rock';
   else if(human==2) x='paper';
   else if(human==3) x='scissors';
   else x='dynamite';
   return(x);
}
//function to identify computer's choice
int identifyC(float y)
{ 
   if(computer==1) y='rock';
   else if(computer==2) y='paper';
   else if(computer==3) y='scissors';
   else y='explosions';
   return(y);
}


When I try compiling it I get these error messages:

 ----jGRASP exec: c++ -o rockpaperscissors rockpaperscissors.cpp
rockpaperscissors.cpp:82:7: error: 
      use
      of
      undeclared
      identifier
      'human'
   if(hu...
      ^
rockpaperscissors.cpp:83:12: error: 
      use
      of
      undeclared
      identifier
      'human'
  ...hu...
     ^
rockpaperscissors.cpp:83:24: warning: 
      multi-character
      character
      constant
      [-Wmultichar]
  ...'p...
     ^
rockpaperscissors.cpp:83:24: warning: 
      character
      constant
      too
      long
      for
      its
      type
rockpaperscissors.cpp:84:12: error: 
      use
      of
      undeclared
      identifier
      'human'
  ...hu...
     ^
rockpaperscissors.cpp:84:24: warning: 
      multi-character
      character
      constant
      [-Wmultichar]
  ...'s...
     ^
rockpaperscissors.cpp:84:24: warning: 
      character
      constant
      too
      long
      for
      its
      type
rockpaperscissors.cpp:85:11: warning: 
      multi-character
      character
      constant
      [-Wmultichar]
  ...'d...
     ^
rockpaperscissors.cpp:85:11: warning: 
      character
      constant
      too
      long
      for
      its
      type
rockpaperscissors.cpp:91:7: error: 
      use
      of
      undeclared
      identifier
      'computer'
   if(co...
      ^
rockpaperscissors.cpp:92:12: error: 
      use
      of
      undeclared
      identifier
      'computer'
  ...co...
     ^
rockpaperscissors.cpp:92:27: warning: 
      multi-character
      character
      constant
      [-Wmultichar]
  ...'p...
     ^
rockpaperscissors.cpp:92:27: warning: 
      character
      constant
      too
      long
      for
      its
      type
rockpaperscissors.cpp:93:12: error: 
      use
      of
      undeclared
      identifier
      'computer'
  ...co...
     ^
rockpaperscissors.cpp:93:27: warning: 
      multi-character
      character
      constant
      [-Wmultichar]
  ...'s...
     ^
rockpaperscissors.cpp:93:27: warning: 
      character
      constant
      too
      long
      for
      its
      type
rockpaperscissors.cpp:94:11: warning: 
      multi-character
      character
      constant
      [-Wmultichar]
  ...'e...
     ^
rockpaperscissors.cpp:94:11: warning: 
      character
      constant
      too
      long
      for
      its
      type
12 warnings and 6 errors generated.

 ----jGRASP wedge: exit code for process is 1.
 ----jGRASP: operation complete.


From past programing, the warnings don't hinder it running (usually), only the errors are what stop it from executing properly (Though I'm sure it would help if I didn't have any warnings either). Any help would be greatly appreciated!

Edit: I forgot to mention that this is the latest compile that has produced the fewest error's and warnings so far (down from around 25 warnings and 20 errors).
Last edited on
Inside a free function, such as identifyH, the following variables exist:

1) Any global variables that exist everywhere
2) Any variables passed in as a parameter
3) Any variables created in that function

The variable human is not one of these. Inside the function identifyH, the variable human does not exist.


While I'm here, you can use the ' ' to wrap a single char. Just one. 'rock' is trying to put FOUR char values into a single char? Makes no sense. Don't do that.
Last edited on
Thanks, that makes sense. Ok, so I guess the problem I'm having now is figuring out how to pass the variables between the identifyH / identifyC functions and the main function.

I've tried playing around with the return() at the end of the functions and program, but it just elicits more errors.

Would I also just use regular quotes to wrap words instead of char's? "something"
Last edited on
I'm having now is figuring out how to pass the variables between the identifyH / identifyC functions and the main function

for small programs like this use global variables like @Repeater said

I've tried playing around with the return() at the end of the functions and program, but it just elicits more errors
if you could show us your "return code" we could help.

Would I also just use regular quotes to wrap words instead of char's? "something"

if the word length is more than 1 alphabet you have to use regular quotes (aka double quotes)

HOPE IT HELPS
For the main function I've tried:
return(int human, int computer, 0);
return(int human; int computer; 0);
return(human, computer, 0);
return(human; computer; 0);
return(0);

For the identifyH and identifyC functions, I've only tried return(x); and return(y); respectively, since those are the only options that made sense.
I figured it out! I was declaring the functions and the variables horribly wrong, using int's and float's instead of string's.

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
string identifyH(int x);
string identifyC(int y);

int main()
{
   blah
   blah
   blah
}

string identifyH(int human)
{
   string x;
   if(human==1) x="Rock";
   else if(human==2) x="Paper";
   else if(human==3) x="Scissors";
   else x="Dynamite";
   return(x);
}

string identifyC(int computer)
{ 
   string y;
   if(computer==1) y="Rock";
   else if(computer==2) y="Paper";
   else if(computer==3) y="Scissors";
   else y="Explosions";
   return(y);
}


Thanks for everyone who replied!
Topic archived. No new replies allowed.