【发布时间】:2020-05-06 20:52:15
【问题描述】:
大家好,我的问题是如何将 char 数组转换为字符串。这是我的代码:
#include<iostream>
using namespace std;
int main()
{
while (true) {
char lol[128];
cout << "you say >> ";
cin.getline(lol,256);
cout << lol << endl;;
}
return 0;
}
所以我想将 lol 转换为像“stringedChar”这样的字符串变量(如果那是英语 lol) 所以我可以做这样的事情:
string badwords[2] = {"frick","stupid"};
for (int counter = 0; counter < 2;counter++) {
if(strigedChar == badwords[counter]) {
bool isKicked = true;
cout << "Inappropriate message!\n";
}
}
对不起,我只是一个 C++ 初学者,哈哈
【问题讨论】:
-
为什么不用字符串开头
string lol; getline(cin, lol);简单不? -
但是如果因为某种原因你真的需要转换,那也不难,
string stringedChar = lol; -
还会修复将 256 个字符读入 128 个字符数组的错误
-
另外,将
badwords设为std::set<std::string>,然后您就可以测试badwords.count(lol) != 0。 -
对我不起作用。它也忽略空格吗?就像它在看到空格时不会停止存储东西