岩石,纸,剪刀,ODD输出

时间:2013-07-27 05:12:55

标签: c++ function

对于我的课程项目,我们将使用功能显示菜单并验证输入和确定谁赢得的功能来制作“摇滚,纸张,剪刀”游戏。当我绕过第一个函数时,我的代码将编译并正常运行。但是,当我使用这两个函数并输入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";
                }
        }
}

3 个答案:

答案 0 :(得分:2)

您不是handling the newline that's still in the cin input stream,并且您没有对因此失败的cin >> selection进行任何错误检查。因为它失败了,selection没有给出任何新值,并且由于你没有初始化它,你得到这个垃圾编号-4195200

如果您执行了一些错误检查,或者通过将while初始化为{{}允许您在selection循环中已经存在的错误检查来完成其工作,那么您已经看到了这一点{1}}:

0

您也未能完全存储int selection = 0; 内调用choices()的结果。请记住,main()内的变量selection是一个局部变量,与choices()内的变量selection不同,您永远不会在其中分配值,代码:

main()

答案 1 :(得分:1)

提供selection = choices()而非choices()

答案 2 :(得分:1)

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

替换

choices();

selection = choices();