Reputation: 1
For my class project, we are to make a game of "rock, paper, scissors" using a function to show the menu and validate the input and a function to determine who won. My code will compile and it functions properly when I bypass the first function. But when I use both functions and I enter 1
, 2
, or 3
for my selection, it gives me -4195200
as a value.
Any help? Here is my code:
// This project will be a game of Rock, Paper, Scissors
#include <iostream>
#include <cstdlib>
#include <ctime>
using namespace std;
// Function Prototypes
int choices();
int userselect (int, int);
int main()
{
//Variables and Seed
int selection;
char again;
unsigned seed = time(0);
srand(seed);
int compselect = (rand()%3)+1;
//Constants for the menu choices
const int ROCK = 1,
PAPER = 2,
SCISSORS = 3;
do
{
//Display the menu choices and validate
choices();
cout << "You picked " << selection << endl;
cout << "The computer picked " << compselect << endl;
//Display the results
userselect(selection, compselect);
//Replay loop
cout << "Do you want to play again? (Y/N)";
cin >> again;
}
while (again == 'Y' || again == 'y');
return 0;
}
//***********************************************************
// This function will display the menu choices and validate *
//***********************************************************
int choices()
{
int selection;
cout << "********************************\n";
cout << "* Please Select A Choice *\n";
cout << "* 1 - Rock *\n";
cout << "* 2 - Paper *\n";
cout << "* 3 - Scissors *\n";
cout << "********************************\n";
cin >> selection;
//Validate the selection
while (selection < 1 || selection > 3)
{
cout << "ERROR: Enter a valid choice.";
cin >> selection;
}
return selection;
}
//***********************************************************
// This function will display the menu choices and validate *
//***********************************************************
int userselect (int num1, int num2)
{
// If user enters Rock
if (num1 == 1)
{
if (num2 == 1)
{
cout << "You picked Rock \n";
cout << "The computer picked Rock \n";
cout << " TIE! \n";
}
else if (num2 == 2)
{
cout << "You picked Rock \n";
cout << "The computer picked Paper \n";
cout << "Paper beats Rock. YOU LOSE! \n";
}
else if (num2 == 3)
{
cout << "You picked Rock \n";
cout << "The computer picked Scissors \n";
cout << " Rock beats Scissors. YOU WIN! \n";
}
}
// If user enters Paper
if (num1 == 2)
{
if (num2 == 1)
{
cout << "You picked Paper \n";
cout << "The computer picked Rock \n";
cout << " Paper beats Rock. YOU WIN! \n";
}
else if (num2 == 2)
{
cout << "You picked Paper \n";
cout << "The computer picked Paper \n";
cout << " TIE! \n";
}
else if (num2 == 3)
{
cout << "You picked Paper \n";
cout << "The computer picked Scissors \n";
cout << " Scissors beats Paper. YOU LOSE! \n";
}
}
// If user enters Scissors
if (num1 == 3)
{
if (num2 == 1)
{
cout << "You picked Scissors \n";
cout << "The computer picked Rock \n";
cout << " Rock beats Scissors. YOU LOSE! \n";
}
else if (num2 == 2)
{
cout << "You picked Scissors \n";
cout << "The computer picked Paper \n";
cout << "Scissors beat Paper. YOU WIN! \n";
}
else if (num2 == 3)
{
cout << "You picked Scissors \n";
cout << "The computer picked Scissors \n";
cout << " TIE! \n";
}
}
}
Upvotes: 0
Views: 2766
Reputation: 385254
You're not handling the newline that's still in the cin
input stream, and you're not doing any error checking on the cin >> selection
that's consequently failing. Because it's failing, selection
is not given any new value and, since you didn't initialise it, you're getting this rubbish number -4195200
.
You'd have seen this if you'd performed some error checking, or allowed the error checking that you sort of already have in your while
loop to do its job by initialising selection
to 0
:
int selection = 0;
You're also failing entirely to store the result of calling choices()
, inside main()
. Remember, the variable selection
inside choices()
is a local variable, and is not the same as the variable selection
inside main()
, to which you never assign a value, in this code:
selection = choices();
Upvotes: 2
Reputation: 2783
You define selection
in function choices();
as local variable, but output the global selection
in main()
.
Replace
choices();
with
selection = choices();
Upvotes: 1