卡在 do while 循环C++的石头/纸/剪刀/蜥蜴/斯波克游戏 - 未声明的标识符
stuck on do while loop for C++ rock/paper/scissors/lizard/spock game - undeclared identifiers
我被困在我知道应该非常简单修复的东西上,但我就是想不通,我一直在谷歌搜索并浏览我的文本(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;
}
相关文章:
- 在C++中,是否可以基于给定的标识符创建基类的新实例,反之亦然
- #ifdef和未声明的标识符
- 在C++游戏中与库存系统作斗争
- f2、f3、f4标识符未找到
- 换位表导致测试失败(但在游戏中运行良好)
- Craps游戏问题,忽略if语句
- 设置 Visual Studio for MPI: 找不到标识符错误
- 未声明的标识符编译暗黑破坏神 2 程序"muleview"
- 矢量下标超出SFML游戏中的范围
- 在顶点着色器中使用 OpenGl 的未声明标识符,我在顶点着色器中绘制三角形时遇到问题
- Google protobuf 时间戳未声明标识符,在 Windows 上具有C++
- 我是 c++ 的新手.学习基本知识后,我想做井字游戏.对于印刷板,我在下面写代码,但它显示错误
- 如何让 GCC/Clang 在保留标识符上出错
- 错误 C2760:语法错误:映射迭代器上意外的标记"标识符",预期的";"
- 当简单捕获中的标识符显示为参数的声明符 ID 时,没有编译器诊断
- 我在贪吃蛇游戏中收到了错误代码 -1073741571
- 使用说明符 extern 声明的C++中的标识符链接
- C++:枚举:错误:应使用标识符而不是"}"
- 卡在 do while 循环C++的石头/纸/剪刀/蜥蜴/斯波克游戏 - 未声明的标识符
- 骰子游戏c++错误未声明的标识符/代码审查