簡體   English   中英

卡在C ++搖滾/紙/剪刀/蜥蜴/ Spock游戲的do while循環中-未聲明的標識符

[英]stuck on do while loop for C++ rock/paper/scissors/lizard/spock game - undeclared identifiers

我被困在一些我知道應該很容易修復的東西上,但是我無法弄清楚我一直在使用Google並閱讀了我的文字(Gaddis C ++ Intro,第6章),並嘗試了一些方法-主要是我以為自己錯了,就把循環移動了一下; 我也在幾個C ++論壇中進行了搜索,並且找到了一些示例,在這些示例中,您詢問userj“是否要繼續/播放”,但這不是我要做的,如果有的話,我希望它自動重新啟動一個領帶。 它會一直返回未在循環中定義/聲明的變量-除非我不知道它們不是。 我正在使用VS 2010。

這是代碼。 感謝您的幫助。 我覺得自己無法解決這個問題有點愚蠢。 這是我的第一門編程課,同時我也在學習視覺基礎。 真有趣。

我得到的調試錯誤都是c20Choice和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]

您的問題是可變范圍。 更改main()的第一行:

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

然后在內部,而不是聲明這些變量,只需分配一個值:

            int cpuChoice = rand() % 5 + 1;

應該

            cpuChoice = rand() % 5 + 1;

並完全擺脫userChoice的其他聲明。

那應該做。

您將這些變量聲明為錯誤的作用域。 將兩個聲明移到循環之前

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;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM