Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/144.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
卡在C+;的do while循环上+;石头/布/剪刀/蜥蜴/斯波克游戏-未声明的标识符 我被困在我知道应该很简单的事情,但我无法理解我已经谷歌和我的文本(加迪斯C++ +介绍,ChH 6),并尝试了一些事情-主要是移动循环,因为我认为我把它错了;我已经搜索了几个C++论坛,我找到了一些例子,你问USER J“你想继续/再玩”,但这不是我要做的,我要让它自动重新启动,如果有一个领带。它总是回到那些没有被定义/声明在循环中使用的变量——除非我不知道它们是如何被定义/声明的。我用的是VS2010_C++ - Fatal编程技术网

卡在C+;的do while循环上+;石头/布/剪刀/蜥蜴/斯波克游戏-未声明的标识符 我被困在我知道应该很简单的事情,但我无法理解我已经谷歌和我的文本(加迪斯C++ +介绍,ChH 6),并尝试了一些事情-主要是移动循环,因为我认为我把它错了;我已经搜索了几个C++论坛,我找到了一些例子,你问USER J“你想继续/再玩”,但这不是我要做的,我要让它自动重新启动,如果有一个领带。它总是回到那些没有被定义/声明在循环中使用的变量——除非我不知道它们是如何被定义/声明的。我用的是VS2010

卡在C+;的do while循环上+;石头/布/剪刀/蜥蜴/斯波克游戏-未声明的标识符 我被困在我知道应该很简单的事情,但我无法理解我已经谷歌和我的文本(加迪斯C++ +介绍,ChH 6),并尝试了一些事情-主要是移动循环,因为我认为我把它错了;我已经搜索了几个C++论坛,我找到了一些例子,你问USER J“你想继续/再玩”,但这不是我要做的,我要让它自动重新启动,如果有一个领带。它总是回到那些没有被定义/声明在循环中使用的变量——除非我不知道它们是如何被定义/声明的。我用的是VS2010,c++,C++,这是密码。谢谢你的帮助。我觉得自己有点傻,因为我自己没能解决这个问题。这是我的第一节编程课,同时我也在学习VisualBasic。这很有趣 我得到的调试错误都是cpuChoice和userChoice的C2065“未声明标识符” [code] #include <iostream> #include <ctime> using namespace std; void outputChoice(int c) { switch(c) { case 1:

这是密码。谢谢你的帮助。我觉得自己有点傻,因为我自己没能解决这个问题。这是我的第一节编程课,同时我也在学习VisualBasic。这很有趣

我得到的调试错误都是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]
[代码]
#包括
#包括
使用名称空间std;
无效输出选项(INTC)
{
开关(c)
{
案例1:

cout您在错误的范围内声明了这些变量。请将这两个声明移到循环之前

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;
}
intmain()
{
int用户选择;
int cpuChoice;
做{
srand(时间(空));

cout您的问题是变量范围。更改
main()中的第一行。

然后在内部,不声明这些变量,只需指定一个值:

            int cpuChoice = rand() % 5 + 1;
应该是

            cpuChoice = rand() % 5 + 1;
并且完全去掉
userChoice
的另一个声明


这应该可以做到。

在循环中定义
int userChoice
,然后想知道它的值,而不是该循环。当您使用
while
语句访问声明时,将声明移动到它们在范围内!只是一个提示;使用枚举来保存岩石、纸、剪刀、蜥蜴、斯波克值。Re:“已解决”在标题中-通常我们不会这样做,您单击对您帮助最大的答案旁边的勾号。这会将问题标记为已在系统中解决。谢谢!请不要在标题中添加“已解决”或类似内容。如果部分答案解决了您的问题,请接受它们。或者,将
while
更改为
while(1)
并在赢和输的路径中添加一个
断点。@DavidSchwartz-你是对的,这将是一个非常干净的解决方法。谢谢你的建议。我正试图向OP解释为什么他会得到一个“未声明的标识符”。非常感谢!我只是看不到。现在效果很好。
            cpuChoice = rand() % 5 + 1;