-1

我目前正在使用 Visual Studio 2010 在 C++ 中制作一个基于控制台的小型文本游戏。我遇到了以下问题;当我输入我的名字并选择难度时,我开始制作介绍性文字并输入:

cout <<"Welcome "<<userName<<"... You are a lone: "<<pickRace<<" Your journey will be a "<<difficulty<<" one.";

我希望它显示为:欢迎布莱克......你是一个孤独的人类/兽人,你的旅程将是一个容易/中等/艰难的旅程。

但我以 Welcome Blake 的身份出现……你是一个孤独的 1/2,你的 jouney 将是一个 1/2/3 的。

我认为这是一个问题,因为我的开关有谁能告诉我我需要如何重写它们以使其显示为名称而不是数字?

原始代码:

cout <<"Please pick your race: \n";
cout <<"1 - Human\n";
cout <<"2 - Orc\n";
int pickRace;
cout <<"Pick your race: ";
cin >>pickRace;

switch (pickRace)
{
case 1:
    cout <<"You picked the Human race.\n";
    break;
case 2:
    cout <<"You Picked the Orc race\n";
    break;
default:
    cout <<"Error - Invalid imput; only 1 or 2 allowed.\n";
}


int difficulty;
cout <<"\nPick your level diffuculty: \n";
cout <<"1 - Easy\n";
cout <<"1 - Medium\n";
cout <<"3 - Hard\n";

cout <<"Pick your level difficulty: ";
cin >>difficulty;

switch (difficulty)
{
case 1:
    cout <<"You picked Easy.\n\n";
    break;
case 2:
    cout <<"You picked Medium.\n\n";
    break;
case 3:
    cout <<"You picked Hard.\n\n";
    break;
default:
    cout <<"Error - Invalid imut; only 1,2 or 3 allowed.\n";
}
4

5 回答 5

2

您正在存储pickRacedifficulty作为integers. 尝试做类似的事情:

int pickRace;
string raceText;    //we will store the race type using this
cout <<"Pick your race: ";
cin >>pickRace;

switch (pickRace)
{
    case 1:
        cout <<"You picked the Human race.\n";
        raceText = "Human";
        break;
    case 2:
        cout <<"You Picked the Orc race\n";
        raceText = "Orc";
        break;
    default:
        cout <<"Error - Invalid imput; only 1 or 2 allowed.\n";
}

注意raceText字符串变量。

重复这个难度。

然后使用raceText 和难度文本打印您的信息:

out <<"Welcome "<<userName<<"... You are a lone: "<<raceText<<" Your journey will be a "<<difficultyText<<" one.";
于 2013-10-10T20:46:40.457 回答
1

考虑使用enums 和重载operator<<operator>>为它们:

#include <iostream>
#include <cassert>

enum difficulty { EASY = 1, MEDIUM = 2, HARD = 3 };

std::istream& operator>>( std::istream& is, difficulty& d )
{
     int i;
     is >> i;
     assert( i > 0 && i < 4 ); // TODO: Use real error handling, throw an exception
     d = difficulty( i );
     return is;
}

std::ostream& operator<<( std::ostream& os, difficulty d )
{
    switch( d ) {
    case EASY: return os << "easy";
    case MEDIUM: return os << "medium";
    case HARD: return os << "hard";
    }
    return os << "unknown[" << (int)d << "]";
}

int main()
{
    difficulty d;
    std::cout << "Pick difficulty: 1-easy, 2-medium, 3-hard: ";
    std::cin >> d;
    std::cout << "You picked difficulty: " << d << std::endl;
}
于 2013-10-10T20:49:50.290 回答
1

您可能希望使用查找表在enums 或数字标识符 (ID) 和它们所代表的文本之间进行转换。

例如:

struct Race_Text_Entry
{
    const char *  text;
    unsigned int  id;
};

static const Race_Text_Entry race_name_table[] =
{
  {"Unknown", 0},
  {"Human",   ID_HUMAN_RACE},
  {"Orc",     ID_ORC_RACE},
  {"Elf",     ID_ELF_RACE},
};
static const unsigned int NUM_RACE_ENTRIES =
    sizeof(race_name_table) / sizeof(race_name_table[0]);

std::string Race_ID_To_Text(unsigned int id)
{
    unsigned int i = 0;
    std::string race_name = "Race unknown";
    for (i = 0; i < NUM_RACE_ENTRIES; ++i)
    {
       if (race_name_table[i].id == id)
       {
           race_name = race_name_table.text;
           break;
       }
    }
    return race_name;
}

int main(void)
{
    std::cout << "My race: " << Race_ID_To_Text(ID_RACE_HUMAN) << "\n";
    return 0;
}

常量查找表作为数组的一个很好的优点是它可以存储在程序的只读数据部分并加载常量数据。与在初始化期间创建std::map变量相比,初始化时间可以忽略不计。

于 2013-10-11T00:06:28.637 回答
1

string当您将选择存储为ints时,为什么希望它打印出来?

您可以使用std::map

#include <map>


std::map<int, std::string> difficulty;

difficulty[1] = "easy";
difficulty[2] = "medium";
difficulty[3] = "hard";

int choice_difficulty;
std::cin>>choice_difficulty;

/*Check if user entered correct number*/
std::map<int, std::string>::iterator it = difficulty.find(choice_difficulty);
if(it == difficulty.end())
    std::cout << "wrong choice";

cout <<"Welcome "<<userName<<" Your journey will be a "<<difficulty[choice_difficulty];
于 2013-10-10T20:59:54.120 回答
0

pickRace 和难度是整数。您正在打印整数而不是实际难度。您需要以某种方式在逻辑上表示难度(和pickRace)

于 2013-10-10T20:46:37.307 回答