有没有办法将布尔值与字符串一起使用?我想在一个字符串中搜索一个单词,如果有就输出意思
Is there a way to use bools with strings? I want to search for a word in a string, and if it's there, output the meaning
我对编码还很陌生,这是我的第一门语言 C++。我有一个作业真的很混乱。
作业要我搜索一行用户输入的俚语。例如:TMI 或 BFF。但是,我不知道如何使用条件表达式来执行此操作,并且我已经尝试过 if-else 语句,但它们最终会读取所有语句而不是每个适用的语句。代码总是会出现条件表达式的问题,因为老实说,我不知道如何以这种方式将它们用于字符串。感谢您的帮助!
(俚语需要大写才能阅读。)
#include <iostream>
#include <cctype>
#include <iomanip>
#include <string>
using namespace std;
int main() {
string userInput;
int textSlang = '0';
cout << "Welcome to the Text Message Decoder!" << endl;
cout << "WARNING! Slang is case-sensitive. Please make sure all slang is fully capitalized!" << endl;
cout << "Enter a single line text message: " << endl;
getline(cin, userInput);
cout << "You entered: " << userInput << endl;
if (textSlang == string::npos) {
cout << "Error" << endl;
}
else if ((textSlang = userInput.find("IDK" ))) {
cout << "IDK: I don't know" << endl;
}
else if ((textSlang = userInput.find("TTYL" ))) {
cout << "TTYL: Talk to you later" << endl;
}
else if ((textSlang = userInput.find("TMI" ))) {
cout << "TMI: Too much information" << endl;
}
else if ((textSlang = userInput.find("JK" ))) {
cout << "JK: Just kidding" << endl;
}
else if ((textSlang = userInput.find("BFF" ))) {
cout << "BFF: Best friends forever" << endl;
}
else if ((textSlang = userInput.find("LOL" ))) {
cout << "LOL: Laugh out loud" << endl;
}
else if ((textSlang = userInput.find("TYSM" ))) {
cout << "TYSM: Thank you so much" << endl;
}
else if ((textSlang = userInput.find("OMG" ))) {
cout << "OMG: Oh my god" << endl;
}
cout << "END." << endl;
//textSlang = userInput.find("TTYL");
//textSlang = userInput.find("TMI");
//textSlang = userInput.find("JK");
//textSlang = userInput.find("BFF");
//textSlang = userInput.find("LOL");
//textSlang = userInput.find("TYSM");
//textSlang = userInput.find("OMG");
}
此代码:
else if ((textSlang = userInput.find("IDK" ))) {
cout << "IDK: I don't know" << endl;
}
将 textSlang
分配给 0
IFF userInput
以 IDK
开头。在所有其他情况中,它分配的值比0
.
other
和 所有 int
值 0
计算结果为真。
你想要:
else if ((textSlang = userInput.find("IDK" )) != string::npos) {
cout << "IDK: I don't know" << endl;
}
我对编码还很陌生,这是我的第一门语言 C++。我有一个作业真的很混乱。
作业要我搜索一行用户输入的俚语。例如:TMI 或 BFF。但是,我不知道如何使用条件表达式来执行此操作,并且我已经尝试过 if-else 语句,但它们最终会读取所有语句而不是每个适用的语句。代码总是会出现条件表达式的问题,因为老实说,我不知道如何以这种方式将它们用于字符串。感谢您的帮助!
(俚语需要大写才能阅读。)
#include <iostream>
#include <cctype>
#include <iomanip>
#include <string>
using namespace std;
int main() {
string userInput;
int textSlang = '0';
cout << "Welcome to the Text Message Decoder!" << endl;
cout << "WARNING! Slang is case-sensitive. Please make sure all slang is fully capitalized!" << endl;
cout << "Enter a single line text message: " << endl;
getline(cin, userInput);
cout << "You entered: " << userInput << endl;
if (textSlang == string::npos) {
cout << "Error" << endl;
}
else if ((textSlang = userInput.find("IDK" ))) {
cout << "IDK: I don't know" << endl;
}
else if ((textSlang = userInput.find("TTYL" ))) {
cout << "TTYL: Talk to you later" << endl;
}
else if ((textSlang = userInput.find("TMI" ))) {
cout << "TMI: Too much information" << endl;
}
else if ((textSlang = userInput.find("JK" ))) {
cout << "JK: Just kidding" << endl;
}
else if ((textSlang = userInput.find("BFF" ))) {
cout << "BFF: Best friends forever" << endl;
}
else if ((textSlang = userInput.find("LOL" ))) {
cout << "LOL: Laugh out loud" << endl;
}
else if ((textSlang = userInput.find("TYSM" ))) {
cout << "TYSM: Thank you so much" << endl;
}
else if ((textSlang = userInput.find("OMG" ))) {
cout << "OMG: Oh my god" << endl;
}
cout << "END." << endl;
//textSlang = userInput.find("TTYL");
//textSlang = userInput.find("TMI");
//textSlang = userInput.find("JK");
//textSlang = userInput.find("BFF");
//textSlang = userInput.find("LOL");
//textSlang = userInput.find("TYSM");
//textSlang = userInput.find("OMG");
}
此代码:
else if ((textSlang = userInput.find("IDK" ))) {
cout << "IDK: I don't know" << endl;
}
将 textSlang
分配给 0
IFF userInput
以 IDK
开头。在所有其他情况中,它分配的值比0
.
和 所有 int
值 0
计算结果为真。
你想要:
else if ((textSlang = userInput.find("IDK" )) != string::npos) {
cout << "IDK: I don't know" << endl;
}