对于我的课堂项目,我们将使用一个显示菜单和验证输入的函数和一个确定谁获胜的函数来制作一个“石头剪刀布”游戏。当我绕过第一个函数时,我的代码将编译并正常运行。但是,当我同时使用这两个函数并输入1
、2
或3
进行选择时,它会给我-4195200
一个值。
有什么帮助吗?这是我的代码:
// 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";
}
}
}