char gamerCentral::getGamerTag( )
{
switch(gamerTag)
{
case '1': return gamerTag = "Diamond";
break;
case '2': return gamerTag = "Silver";
break;
case '3': return gamerTag = "Bronze";
break;
case '4': return gamerTag = "Wood";
break;
default: return gamerTag = "Uninstall";
break;
}
char gamerTag;
GamerClub::GamerClub(
char tag)
{
gamerTag = tag;
}
我正在尝试返回gamerTag,但它说它无法转换为字符串。他们的方法是将char转换为switch语句中的字符串吗?
答案 0 :(得分:3)
让你的函数返回一个字符串,它仍然可以作为输入操作单个字符。这是一个例子。
std::string getGamerTag(char t)
{
switch(t)
{
case '1': return "Diamond";
case '2': return "Silver";
// ...
}
return "";
}
// prints "Silver"
cout << getGamerTag('2');
答案 1 :(得分:0)
使用字符串变量或使用char *
char* getGamerTag(int t)
{
switch(t)
{
case 1: return "Diamond";
case 2: return "Silver";
}
}
int main()
{
cout << getGamerTag(2);
}
答案 2 :(得分:0)
我建议使用字符串数组:
std::string getGamerTag(unsigned int t)
{
static const char * tag_names[] =
{
"Diamond", "Silver", "Bronze", "Wood", "Unistall",
};
static const unsigned int name_quantity =
sizeof(tag_names) / sizeof(tag_names[0]);
std::string name;
if ((t > 0) && (t <= name_quantity))
{
name = tag_names[t - 1];
}
return name;
}