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;
}
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 '}'
#include <iostream>
#include <iomanip>
#include <cstdlib>
usingnamespace std;
//Symbolic constants
constint ROCK = 1;
constint PAPER = 2;
constint 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 );
}
elseif( (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<unsignedint>(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.