我目前正在使用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";
}
答案 0 :(得分:2)
您将pickRace
和difficulty
存储为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和difficultyText打印您的消息:
out <<"Welcome "<<userName<<"... You are a lone: "<<raceText<<" Your journey will be a "<<difficultyText<<" one.";
答案 1 :(得分:1)
考虑使用enum
并重载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;
}
答案 2 :(得分:1)
为什么在将选项存储为string
时,您希望打印int
...
您可以使用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];
答案 3 :(得分:1)
您可能希望使用查找表在enum
或数字标识符(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
变量相比,初始化时间可以忽略不计。
答案 4 :(得分:0)
pickRace和难度是整数。你打印整数而不是实际的难度。你需要以某种方式逻辑地表示难度(和pickRace)