0

私のクラスのプロジェクトでは、メニューを表示して入力を検証する関数と、誰が勝ったかを判断する関数を使用して、「じゃんけん」ゲームを作成します。私のコードはコンパイルされ、最初の関数をバイパスすると正しく機能します。しかし、両方の関数を使用し、選択のために12、またはを入力すると、値として表示されます。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";
                }
        }
}
4

3 に答える 3

2

入力ストリームにまだある改行を処理してcinいないためcin >> selection、結果として失敗するエラーチェックを行っていません。失敗しているため、selection新しい値は与えられず、初期化していないため、このゴミ番号を取得しています-4195200

エラーチェックを実行した場合、またはループ内にすでにあるようなエラーチェックを次のように初期化してジョブを実行できるようにした場合に、これを見たことがあるでしょwhileう。selection0

int selection = 0;

また、呼び出しの結果を内部に保存することにも完全に失敗しています。内部の変数はローカル変数であり、値を割り当てない内部の変数と同じではないことに注意してください。choices()main()selectionchoices()selectionmain()

selection = choices();
于 2013-07-27T05:15:47.580 に答える