C++ 石头、布、剪刀、奇数输出

C++ 石头、布、剪刀、奇数输出,c++,function,C++,Function,对于我的班级项目,我们将制作一个“石头、布、剪刀”游戏,使用一个函数来显示菜单并验证输入和一个函数来确定谁赢了。当我绕过第一个函数时,我的代码将编译并正常运行。但当我同时使用这两个函数并输入1、2或3进行选择时,它会将-4195200作为一个值 有什么帮助吗?这是我的密码: // This project will be a game of Rock, Paper, Scissors #include <iostream> #include <cstdlib> #inc

对于我的班级项目,我们将制作一个“石头、布、剪刀”游戏,使用一个函数来显示菜单并验证输入和一个函数来确定谁赢了。当我绕过第一个函数时,我的代码将编译并正常运行。但当我同时使用这两个函数并输入
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";
                }
        }
}
//这个项目将是一场石头、布、剪刀的游戏
#包括
#包括
#包括
使用名称空间std;
//功能原型
int选项();
int userselect(int,int);
int main()
{
//变量和种子
int选择;
再次烧焦;
无符号种子=时间(0);
srand(种子);
int compselect=(rand()%3)+1;
//菜单选项的常量
岩石常数=1,
纸张=2,
剪刀=3;
做
{
//显示菜单选项并进行验证
选择();
不能您没有,并且您没有对
cin>>选择进行任何错误检查,结果失败。因为它失败,
选择
没有被赋予任何新值,并且,由于您没有初始化它,您得到的是这个垃圾号
-4195200

如果您执行了一些错误检查,或者允许
while
循环中已有的错误检查通过将
选择初始化为
0
来完成其工作,您就会看到这一点:

int selection = 0;
main()
内部调用
choices()
的结果也无法完全存储。请记住,变量
selection
inside
choices()
是一个局部变量,与变量
selection
inside
main()不同
,在此代码中,您从未为其赋值:

selection = choices();
您没有,您也没有对
cin>>选择进行任何错误检查,结果失败。因为它失败,
选择
没有被赋予任何新值,并且,由于您没有初始化它,您得到的是这个垃圾号
-4195200

如果您执行了一些错误检查,或者允许
while
循环中已有的错误检查通过将
选择初始化为
0
来完成其工作,您就会看到这一点:

int selection = 0;
main()
内部调用
choices()
的结果也无法完全存储。请记住,变量
selection
inside
choices()
是一个局部变量,与变量
selection
inside
main()不同
,在此代码中,您从未为其赋值:

selection = choices();

给出
selection=choices()
而不仅仅是
choices()

给出
selection=choices()
而不仅仅是
choices()

在函数
choices();
中将
选择定义为局部变量,但在
main()
中输出全局
选择

替换

choices();


在函数
choices();
中将
selection
定义为局部变量,但在
main()
中输出全局
selection

替换

choices();


石头,布,剪刀,奇怪的问题标题听起来像下一个。显然问题在于用户选择了。石头,布,剪刀,奇怪的问题标题听起来像下一个。显然问题在于用户选择了。