卡在 do while 循环C++的石头/纸/剪刀/蜥蜴/斯波克游戏 - 未声明的标识符

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

本文关键字:游戏 标识符 未声明 蜥蜴 while do 循环 C++ 卡在 石头 剪刀      更新时间:2023-10-16

我被困在我知道应该非常简单修复的东西上,但我就是想不通,我一直在谷歌搜索并浏览我的文本(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.nn" << endl;
        cout << endl;
        {
            int userChoice;
                cout << "Please choose your move.  Select 1-5: nn";
                cout << "1) Rock" << endl;
                cout << "2) Paper" << endl;
                cout << "3) Scissors" << endl;
                cout << "4) Lizard" << endl;
                cout << "5) Spocknn" << 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.nn" << endl;
        cout << endl;
        {
                cout << "Please choose your move.  Select 1-5: nn";
                cout << "1) Rock" << endl;
                cout << "2) Paper" << endl;
                cout << "3) Scissors" << endl;
                cout << "4) Lizard" << endl;
                cout << "5) Spocknn" << 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;
}