0

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

3 回答 3

2

您没有处理仍在cin输入流中的换行符,并且您没有对cin >> selection因此失败的内容进行任何错误检查。因为它失败了,selection所以没有给出任何新值,而且由于你没有初始化它,你得到了这个垃圾数字-4195200

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

int selection = 0;

您也完全无法将调用,的结果存储在. 记住,里面的变量是一个局部变量,和里面的变量不同,你从不给它赋值,在这段代码中:choices()main()selectionchoices()selectionmain()

selection = choices();
于 2013-07-27T05:15:47.580 回答
1

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

于 2013-07-27T05:16:46.837 回答
1

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

代替

choices();

selection = choices();
于 2013-07-27T05:18:08.527 回答