在C ++中有一个奇怪的问题

时间:2017-09-12 21:20:38

标签: c++ string

我对编码非常非常新。 我以为我从C ++开始,我已经写了大约3天了。 为了自学,我一直在使用Sololearn,Hackerrank和编写基于文本的冒险游戏。

这就是我的问题。

我正在为我的项目中的3个playables编写开始场景。 它们位于单独的if语句中,其中任何决策都嵌套在if语句中。 要包含角色健康状况,所有这些都在一个while循环中,当Health> = 1时为真。

然而, 我有一个奇怪的问题,在选择其中一个'选项时,我会遇到一个问题。 (嵌套if if语句' Orc'),运行时,它会输出多个选项,然后从字符创建中重复所有文本,直到那一次。

仅当if语句是字符串时才会发生这种情况。 如果我更改了代码,那么" Item"没有字符串,并设置" Bucket"," Rug"和" Bars"作为int 1,2,3,它可以像我期望的那样工作。 这是C ++中字符串的问题吗?

我已经研究了我能想到的一切,包括usung getline而不是cin用于许多cin语句,以防引起问题,但没有快乐。

请在下面找到我的代码。我错过了什么吗? 而且,对于这几乎是全新的,有没有更好的方法可以做到这一点?

最后,请注意只有' Orc'如果您觉得有必要运行它以更好地了解我所遇到的问题 - ' Human'和' Elf'目前创建无限循环,因为我还没有写到足够多的循环来打破while循环。

提前感谢您的帮助!

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;


int main()
{
//Name Entry
string Name;
cout << "Enter Name: " << endl;
getline (cin, Name);

//Age Entry
int Age;
cout << "Enter Age: " << endl;
cin >> Age;

//Race Entry
string Race;
cout << "Enter Race: " << endl;
cin.ignore();
getline (cin, Race);

//Stats
int Strength;
int Agility;
int Speech;
int Intellect;
int Social;
int Health=1;

//Racial Starts
while (Health >= 1) {

    //Human
    if (Race == "Human" || Race == "human") {

        Strength = 10;
        Agility = 10;
        Speech = 8;
        Intellect = 7;
        Social = 20;
        Health = 100;

        cout << "Human Selected." << endl;
        cout << "Starting Game!" << endl << endl;
        cout << "You are in a finely furnished bedroom." << endl;}

    //Elf
    if (Race == "Elf" || Race == "elf") {

        Strength = 8;
        Agility = 14;
        Speech = 12;
        Intellect = 13;
        Social = 10;
        Health = 80;

        cout << "Elf Selected." << endl;
        cout << "Starting Game!" << endl << endl;
        cout << "You are cleaning a finely furnished bedroom." <<endl;}

    //Orc
    if (Race == "Orc" || Race == "orc") {

        Strength = 18;
        Agility = 5;
        Speech = 6;
        Intellect = 3;
        Social = 5;
        Health = 200;

        cout << "Orc Selected" << endl;
        cout << "Starting Game!" << endl << endl;
        cout << "You are locked in a cell." << endl << "The cell is made up of three stone walls." << endl << "There are no windows." << endl;
        cout << "There is a rug on the cobbled stone floor, and a bucket in the corner." << endl << "Iron bars face into a guard's chamber." << endl << endl;
        cout << "The guard is sleeping." << endl << "A shortsword rests in a scabbard on the table in fromt of him." << endl;

        //Actions in this room
        int Item;
        cout << "Your escape lies with either the Bucket, the Rug or the Bars." << endl << "Pick an item." << endl;

        string Item;
        cin >> Item;


        if (Item == "bucket" || Item == "Bucket") {
            cout << "You place the Bucket on your head." << endl << "The bucket is full of your own excrement." << endl << "You die of disease." << endl;
            Health = Health-1000; }

        if (Item == "Rug" || Item == "rug") {
            cout << "You lie on the rug and fall asleep." << endl << "The guard wakes up." << endl << "His irrational hatred for you causes him to draw his sword, enter your cell and sever your head." << endl;
            Health = Health-1000; }

        if (Item == "Bars" || Item == "bars") {
            cout << "You try the bars." << endl << "You realise that this cell was not built to hold an Orc." << endl << "You bend the bars easily and step out of the cell." << endl; }





    }





}

cout << "Game Over";

system("pause");
return 0;
}

1 个答案:

答案 0 :(得分:1)

你的所有逻辑现在都处于while循环中,所以它会一遍又一遍地重复。它会一直说你是兽人并重新开始冒险。如果你想以更好的方式开发它,我建议你查找&#34; ticks&#34;和电子游戏中的状态机,或者你会继续遇到这个问题。或者,考虑将你的角色创建逻辑移到while循环之外,因为你不希望它继续重复。