rand function

Hey guys, I am writing a rock, paper, scissors program. I've got it pretty much hammered out, except I'm having troubles with the rand function. This is what i have, and I'm getting a whole lot of errors.

1
2
3
4
5
6
7
8
  int GetComputerChoice()
{

   int choice;
   srand(static_castunsigned int> (time(0)));
   choice = 1 + rand() % 3;
   return choice;
}
srand(static_castunsigned int> (time(0)));

did you mean?
srand(static_cast<unsigned int> (time(0)));
Ah, yes. However, it still brings up many errors. Gives me a redefinition error of int getComputerChoice, telling me it was previously declared on the top of the program where i put my function prototypes. Expected primary-expression before 'int', expected '}' before int, expected constructor, destructor, or type conversion before '(', compChoice does not name a type, and lastly, expected declaration before '}'
Give us your full code, because the error seems to be from somewhere else.
People can't help you on code they can't see. Please post the troubling code.
Here is my program.

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

using namespace std;

//Symbolic constants

const int ROCK = 1;
const int PAPER = 2;
const int SCISSORS = 3;

//Function prototypes go after this line
int getCompChoice;

int getUserChoice;

void rockWins (int userChoice);

void paperWins ( int userChoice);

void scissorsWins(int userChoice);

void tieGame();



int main()
{
int userChoice,     //The users choice in the game
    compChoice;     //The computer's "choice" in the game

char again = 'y';   //Used to determine if the game will be played another
                    //time. Initialized to 'y' to make the loop execute at
                    //least one time

//Set the seed value for the random number generator that will be used for
//the computer to "make a choice"
srand(0);


//while the user wants to play the game
while( again == 'y' or again == 'Y' )
  {

  //Get the choices for the computer and the user that is playing the game
  compChoice = getCompChoice;
  userChoice = getUserChoice;

  //Display the computer's "choice"
  cout << endl << "The computer picked "
       << ((compChoice == ROCK) ? "rock" : ((compChoice == PAPER) ? "paper" : "scissors"))
	   << endl;
  

  if( userChoice == compChoice )
    {
    tieGame();
    }
    
 
  else
    {
    	
   
    if( (userChoice == ROCK or compChoice == ROCK) and
        (userChoice == SCISSORS or compChoice == SCISSORS) )
      {
      rockWins( userChoice );
      }

    
    else if( (userChoice == PAPER or compChoice == PAPER) and
        (userChoice == SCISSORS or compChoice == SCISSORS) )
      {
      scissorsWins( userChoice );
      }

    
    else
      {
      paperWins( userChoice );
      }
    
    
    cout << endl << endl << "Would you like to play again (y for yes)? ";
    cin >> again;
    }
  }


return 0;
}

//******************* Code your functions BELOW this line *******************

int getCompChoice
{
	int compChoice;
	srand(static_cast<unsigned int>(time0)));
	compChoice = 1 + rand() % 3;
	
}

// will get choice from computer.

int getUserChoice 
{
 int userChoice;	
	cout << "1) Rock" << setw(10) << "2) Paper" << setw(10) << "3) Scissors" << endl;
	cout << "What is your choice? ";		
	cin >> userChoice;
if (userChoice < 1 or userChoice > 3)	
	cout << "ERROR: Your choice must be between 1 and 3. Please try again. ";
	cin >> userChoice;
}
// Will get choice from user
void rockWins (int userChoice)
{
	int userChoice, compChoice;
if (userChoice == 1 && compChoice == 3)
{
	cout << "Rock smashes Scissors! User wins!";
}
if (userChoice == 3 && compChoice ==1 )
{
	cout << "Rock smashes Scissors! Computer Wins!";
}
}

// will display when rock wins, and who wins.

void paperWins (int userChoice)
{
int userchoice;
if (userChoice == 2 && compChoice == 1)	
{
	cout << "Paper cover Rock! User wins!"
}	
if (userChoice == 1 && compChoice == 2)	
{
	cout << "Paper covers Rock! Computer wins!"
}	
}

// Will display when paper wins, and who wins.

void scissorsWins (int userChoice)
{
int userChoice;	
if (userChoice == 3 && compChoice == 2)	
{
	cout << "Scissors cuts Rock! User Wins!"
	}	
if (userChoice == 2 && userChoice == 3)	
{
	cout << "Scissors cuts Rock! Computer wins!"
}
}

// will display when scissors wins, and who wins.

void tieGame
{
	cout << "It's a tie. Pick again.";
}
// shows it's a tie game. 
getCompChoice is not a function in your code. It is simply an int variable. Function declaratin/definition always contain braces ()
So how do I fix that? I've added braces to the function prototype, but it still comes up with the error
Which line?
Did you add braces to definition?
Topic archived. No new replies allowed.