我對編碼很陌生,這是我的第一門語言 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");
}
uj5u.com熱心網友回復:
這段代碼:
else if ((textSlang = userInput.find("IDK" ))) {
cout << "IDK: I don't know" << endl;
}
分配textSlang給0IFFuserInput以IDK. 在所有其他情況下,它分配一定的價值等相比0。
以及所有 int其他0評估為真的值。
你要:
else if ((textSlang = userInput.find("IDK" )) != string::npos) {
cout << "IDK: I don't know" << endl;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/396864.html
上一篇:如何將double轉換為String并洗掉點值后面的所有尾隨零?
下一篇:用逗號將字串拆分成列
