为什么这个案例阻止不执行?

时间:2013-08-15 05:45:53

标签: c++

几周前我刚开始尝试使用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 个答案:

答案 0 :(得分:7)

您的switch语句会将int charDecision'1' char进行比较。

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

修正:与1比较或将charDecisionchar进行比较。

答案 1 :(得分:5)

变量charDecision被声明为int

C ++标准I / O流类(包括cincout等)相对聪明,并根据您提供的变量类型“做正确的事”

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

您需要将'1'的类型更改为charDecision 测试数字char,而不是字符1。< / p>

答案 2 :(得分:0)

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

答案 3 :(得分:-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,它可以将任何代码转换为废话。