0

私は、修正するのが本当に簡単であるべきだと知っていることに行き詰まっていますが、それを理解することはできません.間違って配置したと思ったようにループを移動します。いくつかの C++ フォーラムも検索しましたが、ユーザーに「続行/再生しますか」と尋ねる例を見つけましたが、それは私がやるべきことではありません。ネクタイ。ループ内で使用するために定義/宣言されていない変数に戻ってきます-ただし、それらがどのように使用されていないのかわかりません。私はVS2010を使用しています。

これがコードです。助けていただければ幸いです。これを自分で修正できないのはちょっとばかげていると思います。これは私の最初のプログラミング クラスであり、同時にビジュアル ベーシックも取っています。面白かったです。

私が得ているデバッグ エラーは、cpuChoice と userChoice の両方で C2065 の「宣言されていない識別子」です。

[code]
#include <iostream>
#include <ctime>
using namespace std;


void outputChoice(int c)
{
  switch(c)
  {
    case 1:
        cout << "Rock";
        break;
    case 2:
        cout << "Paper";
        break;
    case 3:
        cout << "Scissors";
        break;
    case 4:
        cout << "Lizard";
        break;
    case 5:
        cout << "Spock";
        break;
  }
}

//bool for determining if win or if draw -- loss will be elseif

bool isWin(int userChoice, int cpuChoice)
{
  bool result = 
    (   (userChoice == 1 && cpuChoice == 3) ||
        (userChoice == 1 && cpuChoice == 4) ||
        (userChoice == 2 && cpuChoice == 1) ||
        (userChoice == 2 && cpuChoice == 5) ||
        (userChoice == 3 && cpuChoice == 2) ||
        (userChoice == 3 && cpuChoice == 4));
  return result;
}

bool isDraw(int userChoice, int cpuChoice)
{
  bool result = 
        ( (userChoice == cpuChoice));
  return result;
}

int main()
{
    do{

        srand(time(NULL));

        cout << "Welcome to Rock Paper Scissors Lizard Spock!" << endl;
        cout << "The rules are the same as traditional Rock Paper Scissors with the additions as follows: Lizard";
        cout << " beats Paper & Spock; Spock defeats Rock & Scissors.\n\n" << endl;
        cout << endl;

        {
            int userChoice;

                cout << "Please choose your move.  Select 1-5: \n\n";
                cout << "1) Rock" << endl;
                cout << "2) Paper" << endl;
                cout << "3) Scissors" << endl;
                cout << "4) Lizard" << endl;
                cout << "5) Spock\n\n" << endl;

                cin >> userChoice;

            if (!(userChoice >= 1 && userChoice <= 5))
            {
                cout << "Please choose 1, 2, 3, 4 or 5!" << endl;
            }

            else
            {
                int cpuChoice = rand() % 5 + 1;
                cout << "You chose... ";
                outputChoice(userChoice);
                cout << endl;

                cout << "The computer chose... ";
                outputChoice(cpuChoice);
                cout << endl;
                cout << endl;

                cout << "The result is..." << endl;
            }

            if (isWin(userChoice, cpuChoice))
            {
                cout << "You chose wisely!  WINNER!!!!!" << endl;
            }

            else if (isDraw(userChoice, cpuChoice))
            {
                cout << "You chose well, but so did I - TIE!" << endl;
            }

            else
            {
                cout << "You chose poorly!  You loose!" << endl;
            }

    }
    while (userChoice == cpuChoice);    

    return 0;
}
[/code]
4

2 に答える 2

1

問題は可変スコープです。内部の最初の行を変更しますmain()

int main()
{
  int userChoice, cpuChoice;
    do {

次に、これらの変数を宣言する代わりに、内部で値を割り当てます。

            int cpuChoice = rand() % 5 + 1;

する必要があります

            cpuChoice = rand() % 5 + 1;

userChoiceそして、完全に他の宣言を取り除きます。

それはそれを行う必要があります。

于 2013-09-22T21:42:24.303 に答える
0

これらの変数を間違ったスコープで宣言しました。ループの前に両方の宣言を移動します

int main()
{
    int userChoice;
    int cpuChoice;
    do{

        srand(time(NULL));

        cout << "Welcome to Rock Paper Scissors Lizard Spock!" << endl;
        cout << "The rules are the same as traditional Rock Paper Scissors with the additions as follows: Lizard";
        cout << " beats Paper & Spock; Spock defeats Rock & Scissors.\n\n" << endl;
        cout << endl;

        {

                cout << "Please choose your move.  Select 1-5: \n\n";
                cout << "1) Rock" << endl;
                cout << "2) Paper" << endl;
                cout << "3) Scissors" << endl;
                cout << "4) Lizard" << endl;
                cout << "5) Spock\n\n" << endl;

                cin >> userChoice;

            if (!(userChoice >= 1 && userChoice <= 5))
            {
                cout << "Please choose 1, 2, 3, 4 or 5!" << endl;
            }

            else
            {
                cpuChoice = rand() % 5 + 1;
                cout << "You chose... ";
                outputChoice(userChoice);
                cout << endl;

                cout << "The computer chose... ";
                outputChoice(cpuChoice);
                cout << endl;
                cout << endl;

                cout << "The result is..." << endl;
            }

            if (isWin(userChoice, cpuChoice))
            {
                cout << "You chose wisely!  WINNER!!!!!" << endl;
            }

            else if (isDraw(userChoice, cpuChoice))
            {
                cout << "You chose well, but so did I - TIE!" << endl;
            }

            else
            {
                cout << "You chose poorly!  You loose!" << endl;
            }

    }
    while (userChoice == cpuChoice);    

    return 0;
}
于 2013-09-22T21:41:01.223 に答える