简体   繁体   中英

ERROR char and const char

I am quite new to programming and the code that I made has confused me. can you please explain the following errors.

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;
}

ERROR: Operand types are incompatible( char and const char)

Change your function to take a std::string rather than a single char . And remove the extra char declarations.

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

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

Note that it's usually much more efficient to pass in structures by const reference as shown above.

Your code looks like C code. In C "strings" are arrays of bytes. Most functions figure out the end of said array by whats known as a "zero terminator" (a byte with the value of zero). Individual elements of the string (or "array") are referred to as "char" for character.

Logic operations can only be performed on "items" which fit into CPU registers ie comparing a single byte to another byte, an integer to another integer. In order to compare strings, you need to perform a loop, comparing each character (byte) of the strings (arrays). Luckily C comes with a standard library, which among lots of other useful stuff, contains functions for manipulating strings. Specifically the function strcmp will compare two strings, and return the difference of first non-matching character or zero if a zero-terminator is encountered.

To implement you getBill routine using strcmp, you would do something like:

#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);

A little more "advanced" solution, would be to use a "case-insensitive" compare function, and put definied values in a "table". Something like:

#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;
}

Doing it like this, makes it really easy to add new "bill types", and also allows for later functionality to list the supported "bill types".

You are trying to compare sting (or char array) with char.

Do you mean?

char seat[SOME_INT_VALUE];

instead of just

char seat

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM