为什么这个案例块不执行
Why does this case block not execute?
几周前我才开始尝试C++。在尝试C++之前,我对Java有了相当不错的掌握。很多人告诉我,它们在语法意义上非常相似。
底部有一个开关语句,启动了打斗场景。每当我选择战斗选项时,它都会关闭程序。
这是我的代码:
#include "stdafx.h"
#include <iostream>
#include <cstdlib> // For rand()
#include <string>
#include <sstream>
#include <algorithm> // transform()
#include <cctype> // toupper(), tolower()
#include <functional> // ptr_fun()
#include <time.h>
// PUT S*** BELOW THIS POINT
//____________________________________________________________________________
using namespace std;
int main()
{
/* Expirimental text based adventure game.
* Mainly being used for practice methods.
* Developed by Zack Cook.
Generic title. Bad story. Bad interactions.
Lots and lots of bad, bad code. Be warned.
*/
string charName;
string charChoice;
int charDecision;
int playerHealth = 100;
int randomNumber;
int orcHealth = 100;
srand (time(NULL));
cout << "_____________________________________________" << endl;
cout << "| ##### ######### ### ### ### |" << endl;
cout << "| ### ### ### ### ### ### ### |" << endl;
cout << "| ### ### ### ### ##### |" << endl;
cout << "| ### ### ######### ### |" << endl;
cout << "| ### ### ### ### ### ### |" << endl;
cout << "| ##### ### ### ### ### |" << endl;
cout << "_____________________________________________" << endl;
cout << "" << endl;
cout << "" << endl;
cout << "Welcome player. What is your name?" << endl;
getline(cin, charName);
yesOrNo:
cout << "Hello " << charName << ". Are you ready to begin?" << endl;
getline(cin, charChoice);
transform( charChoice.begin(), charChoice.end(), charChoice.begin(), toupper );
cout << charChoice << endl;
if(charChoice == "YES"){
cout << "Good. Let's begin." << endl;
}
else if(charChoice == "NO"){
system ("exit");
}
else
{
cout << "That is not a good answer." << endl;
goto yesOrNo;
}
cout << "Our story begins with a wanderer named " << charName << " passing through the small town of Hark's Pass." << endl;
cout << "A little cozy village with no more than 30 or so home stayers.nThe village men work hard on the farms during the day." << endl;
cout << "The women cater to the children, and other house hold chores.nIn the evening, most of the village turns to The Rusty Trough for a bit of drink." << endl;
cout << "As the sun starts to set, our wanderer, " << charName << ", starts foot towards The Rusty Trough." << endl;
cout << "As " << charName << " enters the tavern, a heavily drunken Orc man stumbles towards the entrance." << endl;
cout << ""I've never seen you 'round here!" The orc says to our wanderer. "I think it's time to teach these adventure types what we really think about 'em"" << endl;
cout << "" << endl;
cout << "What will you do?" << endl;
cout << "1| Get ready to fight!" << endl;
cout << "2| Call for help!" << endl;
cout << "3| Try to run!" << endl;
cout << "4| Do nothing at all!" << endl;
cout << "5| Try to reason!" << endl;
cin >> charDecision;
switch(charDecision)
{
case '1':
do{
cout << "FIGHT" << endl;
randomNumber = rand() % 100 + 1;
if(randomNumber >= 50){
orcHealth = orcHealth - (randomNumber - (randomNumber / 5));
cout << "You hit the orc! He now has " << orcHealth << " life left!" << endl;
}
else
{
playerHealth = playerHealth - (randomNumber - (randomNumber / 5));
cout << "The orc hit you! You now have " << playerHealth << " life left!" << endl;
}
}while(playerHealth || orcHealth != 0);
break;
default:
break;
}
return 0;
}
您的 switch 语句将int charDecision
与'1'
进行比较,这是一个char
。
您从标准输入读取到int
这意味着charDecision
将包含 1。然后,您将此 1 与 '1'
进行比较,当转换为 int
时,转换为 49。因此,您的输入将永远不会匹配(除非您输入 49(。
修复:与1
进行比较或charDecision
成为char
。
变量charDecision
声明为int
。
C++标准 I/O 流类(包括 cin
和 cout
等(相对智能,并且根据您提供给它的变量类型"做正确的事"。
执行表达式 cin >> charDecision
时,cin
读取看起来像数字的所有内容,并将其转换为数字的本机表示形式。因此,当您在该菜单上键入1
时,存储的是文字数字 1。您的交换机正在测试文字字符 '1'
,其整数值为 49,因此它不匹配,因为 49 != 1。
您需要将charDecision
类型更改为char
或测试数字1
而不是字符'1'
。
while (playerHealth != 0 || orcHealth != 0)
我认为这就是问题所在。
您的主要问题是使用goto,摆脱它!
yesOrNo:
cout << "Hello " << charName << ". Are you ready to begin?" << endl;
getline(cin, charChoice);
transform( charChoice.begin(), charChoice.end(), charChoice.begin(), toupper );
cout << charChoice << endl;
if(charChoice == "YES"){
cout << "Good. Let's begin." << endl;
}
else if(charChoice == "NO"){
system ("exit");
}
else
{
cout << "That is not a good answer." << endl;
goto yesOrNo;
}
可以替换为
do
{
cout << "Hello " << charName << ". Are you ready to begin?" << endl;
getline(cin, charChoice);
transform( charChoice.begin(), charChoice.end(), charChoice.begin(), toupper );
cout << charChoice << endl;
if(charChoice == "YES"){
cout << "Good. Let's begin." << endl;
startGame();//it is the function where you should place your game logic, the "switch" block in your case
//if you want to get back to main menu after the end of the game you can delete next line:
break;//leave this loop
}
else if(charChoice == "NO"){
break;//leave this loop
}
else
{
cout << "That is not a good answer." << endl;
}
} while (true);
同样这样做,您可以在游戏结束后返回主菜单。
我还建议你将游戏逻辑拆分为不同的函数,代码将更容易编写和维护。另外,我建议您在完成后以面向对象的方式重写游戏,这对您来说可能是一个很好的做法。忘记goto
,它可以将任何代码变成一段废话。
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- C++,系统无法执行指定的程序
- 使用C++中的模板和运算符重载执行矩阵运算
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 执行函数时导致崩溃的变量
- 无论条件是否为true,if总是在c++中执行
- 当函数模板参数是具有默认参数的类模板时,函数模板参数的推导如何执行
- 在C++中对T*类型执行std::move的意外行为
- 使用QProcess执行命令,并将结果存储在QStringList中
- 如何在没有信号的情况下从C++执行QML插槽
- 如何确认我的constexpr表达式实际上已经在编译时执行
- C++17中的并行执行策略
- QML按钮点击功能执行顺序
- 程序在执行程序的其余部分之前退出
- 为什么catch中的代码没有被执行
- C++从其他 constexpr 创建 lambda 不能按顺序执行 Constexpr
- 案例未执行
- 执行默认值:在C 中的开关案例选择语句中
- 案例语句不执行
- 为什么这个案例块不执行