C++ C++;文本消息解码器脚本编译器“;需要不合格的id“;在if和else语句之前

C++ C++;文本消息解码器脚本编译器“;需要不合格的id“;在if和else语句之前,c++,C++,救命啊!我是C++新手。我写这段代码的目的是解码文本缩写,我的编译器不断地在if和else语句之前给我错误:“expected unqualified id”。不管我怎么努力,我似乎都无法纠正这个错误。知道我哪里出错了吗 这是我的密码: #include <iostream> #include <string> // Note: This library is needed to use the string type using namespace std; int

救命啊!我是C++新手。我写这段代码的目的是解码文本缩写,我的编译器不断地在if和else语句之前给我错误:“expected unqualified id”。不管我怎么努力,我似乎都无法纠正这个错误。知道我哪里出错了吗

这是我的密码:

#include <iostream>
#include <string> // Note: This library is needed to use the string type
using namespace std;

int main() {

string abbr;
cout << "Input the abbreviation: " ;
cin >> abbr;

string LOL = "laughing out loud";
string IDK = "I don't know";
string BFF = "best friends forever";
string IMHO = "in my humble opinion";
string TMI = "too much information";
}

if (abbr = "LOL") == 0) ||
  (abbr = "lol") == 0))
{
  cout << "Laughing out loud";
}

else if (abbr = "IDK") == 0) ||
      (abbr = "idk") == 0))
{
  cout << "I don't know";
}

else if (abbr = "BFF") == 0) ||
      (abbr = "bff") == 0))
{
  cout << "best friends forever";
}

else if (abbr = "IMHO") == 0) ||
      (abbr = "imho") == 0))
{
  cout << "in my humble opinion";
} 

else if (abbr = "TMI") == 0) ||
      (abbr = "tmi") == 0))
{
  cout << "too much information";
}

else
  cout << "Unknown";

return 0;
}
#包括
#include//注意:使用字符串类型需要此库
使用名称空间std;
int main(){
字符串缩写;
cout>abbr;
string LOL=“大笑”;
string IDK=“我不知道”;
string BFF=“永远的好朋友”;
string IMHO=“以我的拙见”;
string TMI=“信息太多”;
}
如果(abbr=“LOL”)==0||
(abbr=“lol”)==0)
{

cout所有的错误都是你的支撑。在你进入if语句之前关闭
main()
。你所需要做的就是在你声明字符串之后去掉
}
,并修复if语句上的括号。在编写代码时,尝试插入成对的括号,而不是只插入一个括号,这样问题就会少一些。以下是固定代码:

#include <iostream>
#include <string> // Note: This library is needed to use the string type
using namespace std;

int main()
{
    string abbr;
    cout << "Input the abbreviation: ";
    cin >> abbr;

    string LOL = "laughing out loud";
    string IDK = "I don't know";
    string BFF = "best friends forever";
    string IMHO = "in my humble opinion";
    string TMI = "too much information";


if((strcmp(abbr.c_str(), "LOL") == 0) ||
(strcmp(abbr.c_str(), "lol") == 0))
{
    cout << "Laughing out loud";
}

else if((strcmp(abbr.c_str(), "IDK") == 0) ||
(strcmp(abbr.c_str(), "idk") == 0))
  {
  cout << "I don't know";
  }

else if((strcmp(abbr.c_str(), "BFF") == 0) ||
(strcmp(abbr.c_str(), "bff") == 0))
  {
  cout << "best friends forever";
  }

else if((strcmp(abbr.c_str(), "IMHO") == 0) ||
(strcmp(abbr.c_str(), "imho") == 0))
  {
  cout << "in my humble opinion";
  }

else if((strcmp(abbr.c_str(), "TMI") == 0) ||
(strcmp(abbr.c_str(), "tmi") == 0))
  {
  cout << "too much information";
  }

else
{
    cout << "Unknown";
}
    return 0;
}
#包括
#include//注意:使用字符串类型需要此库
使用名称空间std;
int main()
{
字符串缩写;
cout>abbr;
string LOL=“大笑”;
string IDK=“我不知道”;
string BFF=“永远的好朋友”;
string IMHO=“以我的拙见”;
string TMI=“信息太多”;
if((strcmp(缩写为c_str(),“LOL”)==0)||
(strcmp(缩写c_str(),“lol”)=0)
{

CUT< P>不要混合C和C++,这是个坏主意。如果你想使用C函数,那么 坚持C,如果你想用C++,那么使用C++提供的函数。 如果你有适当的缩进,你会马上看到你 大括号放错地方了。在
string TMI=“太多信息”
您有一个关闭
}
,它结束了
主功能
如果在函数外部调用
,编译器会对此抱怨

如果您的
缺少第一个

另外,
strcmp
是在
string.h
中定义的C函数,以便在 C++,需要添加<代码>包含访问“代码> STRCMP < /C> > < /P>

但是就像我说的,如果你使用C++,那么使用C++提供的工具 不要把C和C混合起来,你不应该认为C++是C扩展。 <使用C++工具,你可以做这样的事情:< /P>

#include <iostream>
#include <string>
#include <algorithm>
#include <map>

int main(void)
{
    std::string abbr;
    std::cout << "Input the abbreviation: " ;
    std::cin >> abbr;

    std::map<std::string, std::string> abbr_map = {
        {"lol",  "laughing out loud"},
        {"idk",  "I don't know"},
        {"bff",  "best friends forever"},
        {"imho", "in my humble opinion"},
        {"tmi",  "too much information"},
    };

    std::string orig = abbr;

    std::transform(abbr.begin(), abbr.end(), abbr.begin(), ::tolower);

    if(abbr_map.find(abbr) == abbr_map.end())
    {
        std::cerr << "Abbreviation '" << orig << "' not found" << std::endl;
        return 1;
    }

    std::cout << abbr_map[abbr] << std::endl;

    return 0;
}
#包括
#包括
#包括
#包括
内部主(空)
{
std::字符串缩写;
std::cout>abbr;
标准::地图缩写\u地图={
{“lol”,“大声笑”},
{“idk”,“我不知道”},
{“bff”,“永远的好朋友”},
{“依我之见”},
{“tmi”,“信息太多”},
};
std::string orig=abbr;
std::transform(缩写为begin(),缩写为end(),缩写为begin(),::tolower);
if(abbr_map.find(abbr)==abbr_map.end())
{

STR::问题是你认为你在C和C++编程。我建议你删除C标签并关注C++。为什么你使用<代码> STRCMP <代码> >代码> STD::String < /C>?你可以有一些类似的东西:<代码> IF(ABBR= =“LOL”)
。请参见
std::transform
,了解如何将字符串转换为全小写或全大写,因此您只需进行“比较”。否则,“lOl”将打乱您的程序以及其他字母/大小写组合。您有一个大括号
{
在最后一个
else
语句中的
cout
之后。您还需要为
strcmp
添加
#include
#include <iostream>
#include <string>
#include <algorithm>
#include <map>

int main(void)
{
    std::string abbr;
    std::cout << "Input the abbreviation: " ;
    std::cin >> abbr;

    std::map<std::string, std::string> abbr_map = {
        {"lol",  "laughing out loud"},
        {"idk",  "I don't know"},
        {"bff",  "best friends forever"},
        {"imho", "in my humble opinion"},
        {"tmi",  "too much information"},
    };

    std::string orig = abbr;

    std::transform(abbr.begin(), abbr.end(), abbr.begin(), ::tolower);

    if(abbr_map.find(abbr) == abbr_map.end())
    {
        std::cerr << "Abbreviation '" << orig << "' not found" << std::endl;
        return 1;
    }

    std::cout << abbr_map[abbr] << std::endl;

    return 0;
}