把信变成文字
Letter into words
我将放置我的类来显示我需要使用的其他东西。如果我做打字,它会起作用吗?或者我只需要学习弦乐?
class NumberBox
{
private:
int number;
char letter;
public:
NumberBox *next_ptr;
void setNumber(int number)
{
this->number = number;
}
void setLetter(char letter)
{
this->letter = letter;
}
int getNumber()
{
return number;
}
int getLetter()
{
return letter;
}
};
int main()
cout << "Please Give the faction for the first Card" << endl;
cin >> faction[0];
if(faction [0] == 's' || faction [0] == 'c' || faction [0] == 'h' || faction [0] == 'd')
{
if(faction[0] == 's')
{
faction[0] = "spade";
factionHead_ptr->setLetter("spade");
}
}
你是怎么做到的?就像如果用户输入's'
,那么它将是Spade。
您可以为此使用std::map
。您将构建一个从char
到std::string
的映射,其中包含一个值集s
、c
、h
和d
。
如何使用它的一个小例子是:
#include <iostream>
#include <map>
typedef std::map<char, std::string> suitmap;
int main()
{
suitmap suits;
suits['s'] = "spades";
suits['h'] = "hearts";
char in;
std::cout << "Suit?" << std::endl;
if (std::cin >> in) {
suitmap::const_iterator it = suits.find(in);
if (it != suits.end())
std::cout << it->second << std::endl;
else
std::cout << "not found" << std::endl;
} else {
std::cout << "no input" << std::endl;
}
return 0;
}
根据您的问题进行调整,假设faction
是char
s:的数组
cin >> faction[0];
// see if we have a match for faction[0] in the map
suitmap::const_iterator it = suits.find(faction[0]);
if (it == suits.end()) {
// no match
// print an error or something
} else {
// match! the string is in it->second
std::string suit = it->second;
factionHead_ptr->setLetter(suit);
}
现在,这对NumberBox
类根本不起作用,因为setLetter
函数需要一个char,而letter
成员也是一个char。所以你需要把这两个改成std::string
s。
如果你只想要一个字母,那么你的letter
成员就可以了,但你不能把整个字符串放在一个字母中,所以你在main
中调用setLetter
没有多大意义。因此,如果你只需要一个字母,而不是从一个字母到一个完整的西装名称的映射,只需使用switch语句:
cin >> faction[0];
switch (faction[0]) {
case 's': case 'c': case 'h': case 'd':
factionHead_ptr->setLetter(faction[0]);
break;
default:
// invalid input given, do something about it
break;
}
此行不起作用:
if(faction [0] == 's' && 'c' && 'h' && 'd')
因为if语句的每一部分都是由它自己评估的。例如:"c"将始终计算为true,因此该语句没有任何效果。应该是这样的:
if(faction [0] == 's' || faction [0] == 'c' || faction [0] == 'h' || faction [0] == 'd')
你可以在这里完美地使用switch
语句:
switch(faction[0]) {
case 's':
factionHead_ptr->setLetter("spade");
break;
case 'c':
...
break;
...
default:
// code here if none of s, c, h, d
break;
}
编辑:还要小心您的数据类型。例如,我不确定faction[0] = "spade";
是否会像预期的那样工作,因为faction[0]
的类型似乎是char
,而不是字符串。
您不能以这种方式链接条件。条件是,如果faction
的第一个字母是s或"派系"的第一个字符是c和。。。
像这样:
if(faction [0] == 's' || faction [0] == 'c' || faction [0] == 'h' || faction [0] == 'd')
您可以使用的switch
语句:
string faction;
char type;
cin >> type;
switch (type)
{
case 's':
faction = "spade";
break;
case 'c':
faction = "clib";
break;
case 'h':
faction = "heart";
break;
case 'd':
faction = "diamond";
break;
default:
cout << "Illegal card typen";
break;
}
上面的代码还修复了您遇到的另一个问题,即将faction[0]
同时用作字符串和字符。由于您没有指定faction
的类型,我在上面的示例中将其作为字符串。
switch(functin[0]) {
case 's': faction = "spade";factionHead_ptr->setLetter("spade");break;
case 'c': faction = "clubs";factionHead_ptr->setLetter("clubs");break;
case 'h': faction = "hearts";factionHead_ptr->setLetter("hearts");break;
case 'd': faction = "diamonds";factionHead_ptr->setLetter("diamonds");break;
default: cerr<<"wrong suite entered";
}
- constexpr 函数中的非文字(通过 std::is_constant_evaluated)
- 我可以使用条件运算符初始化C风格的字符串文字吗
- 如何使用字符串文字作为宏参数
- 有没有办法从非C/C++文件中读取C++原始字符串文字的内容
- 构造<int>具有 2 个字符串文字的向量
- 将数字打印成文字
- 初始化或分配空字符串文字到指向 C 中的 char 的指针或指向 C++ 中 const char 的指针的原因是什么
- 方便地对C++中的所有字符串文字进行模糊处理
- C++17 十六进制浮点文字单精度后缀冲突?
- 常量函数,当其参数是对文字类型的引用时
- 连接 LPCSTR 变量和文字?
- 比较 std::string 和 C 样式字符串文字
- 如何从char16_t字符串文字中读取双精度?
- 指内置类型的文字
- 分配给浮点数的积分文字除法 - 为什么结果是错误的?
- std::remove() 按预期处理文字,但不能与取消引用的迭代器一起工作
- 为什么带有指针子对象的文字类类型的 constexpr 表达式不能是非类型模板参数
- 为什么文字不是常量(字符串除外)?
- 模板接受常量,但不接受文字
- 如果我在块中编写字符串文字,是否会从数据部分复制整个字符串数据?