需要帮助这个文本信息解码器(c++)练习

Need help on this Text message decoder (C++) exercise

本文关键字:解码器 c++ 练习 信息 文本 帮助      更新时间:2023-10-16

说明如下:

  1. 使用getline()获取一行用户输入到字符串中。输出行。(3点)

    ,
    输入text: IDK如果我要去。今天是我最好的朋友的生日。您输入: IDK if I'll go。今天是我最好的朋友的生日。

  2. 搜索字符串(使用find())查找常见缩写,并打印每个找到的缩写及其解码的含义的列表。(3点)

    ,
    输入text: IDK如果我要去。今天是我最好的朋友的生日。您输入: IDK if I'll go。今天是我最好的朋友的生日。

支持以下缩写:

-永远的好朋友
IDK——我不知道
JK——开个玩笑
TMI—信息过多
TTYL——待会再聊

我一直在尝试编码并获得6/6可能的点,但我最终获得3/6。

#include <iostream>
#include <string>
using namespace std;
int main() {
string input;
cout << "Enter text: ";
getline(cin, input);
cout << endl <<"You entered: " << input << endl;
if (input.find("BFF") && input.find("IDK"))
{
cout << "BFF: best friend forever" << endl;
cout << "IDK: I don't know" << endl;
} 
if (input.find("JK") && input.find("TMI") && input.find("TTYL"))
{
   cout << "JK: just kidding" << endl;
   cout << "TMI: too much information" << endl;
   cout << "TTYL: talk to you later" << endl;
}
   return 0;
}

我没有得到最后3分:

比较输出0/2

输入我不知道我是否要去。今天是我闺蜜的生日。

输出

输入文字:

你输入:IDK如果我要去。今天是我闺蜜的生日。

永远的好朋友

IDK:我不知道

JK:开玩笑的

TMI: too much information

TTYL:回头聊

预期输出输入文本:

你输入:IDK如果我要去。今天是我闺蜜的生日。

永远的好朋友

IDK:我不知道

比较输出0/1

输入不错的照片,TMI哈哈JK。TTYL

输出

输入文字:

你输入:漂亮的照片,TMI哈哈JK。TTYL

永远的好朋友

IDK:我不知道

期望输出

输入文字:

你输入:漂亮的照片,TMI哈哈JK。TTYL

JK:开玩笑的

TMI: too much information

TTYL:回头聊

显然你的函数调用在某些点为真,在其他点为假。Find()返回找到的子字符串的第一个字符的位置,如果没有找到这样的子字符串,则返回npos = -1。true语句是计算结果为非零的语句。False的值为0。

在你的比较输出0/2中,所有的计算结果都是非零值。因此,所有内容都被打印出来。在0/1中,一定有一个值是0。BFF和IDK的指纹是-1,其他的没有。(只是我的2美分,也许有人可以确认你代码中的值是多少)


#include <iostream>
#include <string>
using namespace std;
int main() {
string text;
cout << "Input an abbreviation:" << endl;
getline(cin, text);
if (text.find("LOL") != string::npos){
   cout << "laughing out loud" << endl;
}
else if (text.find("IDK") != string::npos){
   cout << "I don't know" << endl;
}
else if (text.find("BFF") != string::npos){
   cout << "best friends forever" << endl;
}
else if (text.find("IMHO") != string::npos){
   cout << "in my humble opinion" << endl;
}
else if (text.find("TMI") != string::npos){
   cout << "too much information" << endl;
}
else{
   cout << "Unknown" << endl;
}
return 0;
}
  //for already defined String Value of BFF, IDK, JK, TMI and TTYL.
   if (userIn.find("BFF") != string::npos){
      cout << "BFF: " << BFF << endl;
   }
   if (userIn.find("IDK") != string::npos){
      cout << "IDK: " << IDK << endl;
   }
   if (userIn.find("JK") != string::npos){
      cout << "JK: " << JK << endl;
   }
   if (userIn.find("TMI") != string::npos){
      cout << "TMI: " << TMI << endl;
   }
   if (userIn.find("TTYL") != string::npos){
      cout << "TTYL: " << TTYL << endl;
   }

.find()不返回布尔值,所以它不是真或假。find(item)返回第一个项目出现的索引,否则返回string::npos(在字符串库中定义的常量)。因此,你将它与string::npos进行比较,看看你想要的字符串是否存在。

希望能解释清楚

删除条件:input.find("IDK"