4

几周前我刚刚开始尝试 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;
}
4

4 回答 4

7

您的 switch 语句将 aint charDecision'1'which is a进行比较char

您从标准输入读取到一个intwhich 意味着charDecision将包含 1。然后您将这个 1 与'1'转换为 49 时转换为 49 进行比较int。因此,您的输入永远不会匹配(除非您输入 49)。

修复:比较1或制作charDecision一个char.

于 2013-08-15T05:56:55.210 回答
5

该变量charDecision被声明为int.

C++ 标准 I/O 流类(包括cincout等)相对智能,并且根据您给它的变量类型“做正确的事”。

执行表达式时cin >> charDecisioncin读取所有看起来像数字的内容并将其转换为数字的本机表示。因此,当您1在该菜单上键入时,存储的是文字数字 1。您的开关正在测试文字字符 '1',它的整数值为 49,因此它将不匹配,因为 49 != 1。

您将需要更改 to 的类型charDecisionchar 测试数字1而不是字符'1'

于 2013-08-15T05:56:24.723 回答
0

while (playerHealth != 0 || orcHealth != 0) 我认为这就是问题所在。

于 2013-08-15T10:12:04.910 回答
-2

您的主要问题是使用 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,它可以把任何代码变成废话。

于 2013-08-15T09:03:09.380 回答