C++ 错误字符和常量字符

C++ 错误字符和常量字符,c++,char,constants,C++,Char,Constants,我对编程相当陌生,我编写的代码让我感到困惑。你能解释一下下面的错误吗 int getBill(char seat)// a return function. { int b=0; char FS, fs, LBS, lbs, UBS, ubs, GPS, gps; char seat; if(seat=="FS"||seat=="fs") b=15000; if(seat=="LBS"||seat=="lbs") b=10000;

我对编程相当陌生,我编写的代码让我感到困惑。你能解释一下下面的错误吗

int getBill(char seat)// a return function.
{
    int b=0;
    char FS, fs, LBS, lbs, UBS, ubs, GPS, gps;
    char seat;
    if(seat=="FS"||seat=="fs")
    b=15000;
    if(seat=="LBS"||seat=="lbs")
    b=10000;
    if(seat=="UBS"||seat=="ubs")
    b=5000;
    if(seat=="GPS"||seat=="gps")
    b=1500;

    return b;
}

错误:操作数类型不兼容(char和const char)

请将函数更改为使用
std::string
而不是单个
char
。并删除额外的
char
声明

int getBill(const std::string &seat)// a return function.
{
    int b=0;

    if(seat=="FS"||seat=="fs")
     ....

请注意,如上所示,通过
const
引用传入结构要高效得多。

您的代码看起来像C代码。在C语言中,“字符串”是字节数组。大多数函数通过所谓的“零终止符”(值为零的字节)计算出所述数组的结尾。字符串(或“数组”)的各个元素称为字符的“char”

逻辑操作只能在适合CPU寄存器的“项”上执行,即将单个字节与另一个字节、整数与另一个整数进行比较。为了比较字符串,需要执行循环,比较字符串(数组)的每个字符(字节)。幸运的是,C附带了一个标准库,其中包括许多其他有用的东西,包含用于操作字符串的函数。具体来说,
strcmp
函数将比较两个字符串,如果遇到零终止符,则返回第一个不匹配字符的差值或零

要使用strcmp实现
getBill
例程,您可以执行以下操作:

#include <string.h> /* contains definition of strcmp() */
int getBill(char *seat)
{
    int b=0;
    char seat;
    if(0 == strcmp(seat, "FS") || 0 == strcmp(seat,"fs"))
      b=15000;
    if(0 == strcmp(seat, "LBS") || 0 == strcmp(seat, "lbs"))
      b=10000;
    if(0 == strcmp(seat, "UBS") || 0 == strcmp(seat, "ubs"))
      b=5000;
    if(0 == strcmp(seat, "GPS") || 0 == strcmp(seat, "gps"))
      b=1500;

    return b;
}

/* example use: */

  getBill("FS);
#include <string.h> /* contains definition of stricmp() */

static const char* bill_types_str[] =
{
  "fs", "lbs", "ubs", "gps"
};

static const int bill_type_ids[] =
{
  15000, 10000, 5000, 1500
};

int getBill(char *seat)
{
  for(i=0; i < sizeof(bill_types_str)/sizeof(bill_types_str[0]; i++)
    if (0 == stricmp(seat, bill_types_str[i]))
      return bill_types_id[i];

  return 0;
}
#include/*包含strcmp()的定义*/
int getBill(字符*座位)
{
int b=0;
炭座;
如果(0==strcmp(座位,“FS”)| | 0==strcmp(座位,“FS”))
b=15000;
如果(0==strcmp(座位,磅)| | 0==strcmp(座位,磅))
b=10000;
如果(0==strcmp(席位,“UBS”)| | 0==strcmp(席位,“UBS”))
b=5000;
如果(0==strcmp(座椅,“GPS”)| | 0==strcmp(座椅,“GPS”))
b=1500;
返回b;
}
/*示例用法:*/
财政司司长;
更“高级”的解决方案是使用“不区分大小写”的比较函数,并将定义的值放入“表”中。类似于:

#include <string.h> /* contains definition of strcmp() */
int getBill(char *seat)
{
    int b=0;
    char seat;
    if(0 == strcmp(seat, "FS") || 0 == strcmp(seat,"fs"))
      b=15000;
    if(0 == strcmp(seat, "LBS") || 0 == strcmp(seat, "lbs"))
      b=10000;
    if(0 == strcmp(seat, "UBS") || 0 == strcmp(seat, "ubs"))
      b=5000;
    if(0 == strcmp(seat, "GPS") || 0 == strcmp(seat, "gps"))
      b=1500;

    return b;
}

/* example use: */

  getBill("FS);
#include <string.h> /* contains definition of stricmp() */

static const char* bill_types_str[] =
{
  "fs", "lbs", "ubs", "gps"
};

static const int bill_type_ids[] =
{
  15000, 10000, 5000, 1500
};

int getBill(char *seat)
{
  for(i=0; i < sizeof(bill_types_str)/sizeof(bill_types_str[0]; i++)
    if (0 == stricmp(seat, bill_types_str[i]))
      return bill_types_id[i];

  return 0;
}
#include/*包含stricmp()的定义*/
静态常量字符*账单类型\u str[]=
{
“fs”、“lbs”、“ubs”、“gps”
};
静态常量整型票据类型ID[]=
{
15000, 10000, 5000, 1500
};
int getBill(字符*座位)
{
对于(i=0;i

这样做,可以很容易地添加新的“票据类型”,并允许以后的功能列出受支持的“票据类型”。

您正在尝试将sting(或char数组)与char进行比较

你是说

char seat[某些值]

而不仅仅是

炭座


您试图将单个字符与字符串进行比较。您已经声明了同一个变量两次。并且大多数变量都未初始化。char是一个字符。如果需要多个字符,请使用数组(作为指向函数的指针传递)。使用strcmp()要比较字符串,==不会执行您期望它执行的操作。此外,您不必声明变量(
FS,FS,…
)对于你在函数中使用的字符串文字。哦,好吧。我认为使用= =仍然会和STRCMP一样。谢谢。@ GuntraMLBHM:这是C++。使用<代码> STD::String 字符串。我有过经验,通过const REF到STD:String。我还认为,现代的实现是优化的,使得通过。std:string实际上和传递一个“裸”字符指针一样便宜。@S.C.Madsen可能是一个错误的编译器或库?至于性能,请测量一下,我相信你;-)(顺便说一下,我认为C++11禁止引用计数的字符串)我同意你倾向于衡量性能声明,建议你对自己的声明也这么做。我如何将其用作指针?我是否这样写?rsrve.bill=getBill(const std::string&seat)@HanzMendez-请添加代码,说明你希望如何使用它。