gpt4 book ai didi

c++ - 为什么这个案例 block 不执行?

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:14:43 26 4
gpt4 key购买 nike

几周前我才开始尝试 C++。在尝试 C++ 之前,我对 Java 有了相当不错的掌握。很多人告诉我,它们在语法意义上非常相似。

在底部有一个 switch 语句来启动战斗场景。每当我选择战斗选项时,它只会关闭程序。

这是我的代码:

#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

关于c++ - 为什么这个案例 block 不执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18247000/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com