0

我被困在我知道应该很容易解决的事情上,但我就是想不通我一直在谷歌搜索并浏览了我的文本(Gaddis C++ Intro,第 6 章)并尝试了一些事情 - 主要是移动循环,因为我认为我放错了;我也搜索了几个 C++ 论坛,我找到了一些例子,你问用户 j“你想继续/再玩一次吗”,但这不是我要做的,如果有,我会让它自动重启一个领带。它不断回到未在循环中定义/声明使用的变量——除了我不明白它们是如何不使用的。我正在使用 VS 2010。

这是代码。我很感激任何帮助。我觉得自己无法解决这个问题有点愚蠢。这是我的第一堂编程课,同时我也在学习 Visual Basic。这很有趣。

我得到的调试错误都是 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 回答