1

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)

simonc
  • 41,632
  • 12
  • 85
  • 103
  • 1
    You're trying to compare a single character to a string. And you've declared the same variable twice. – chris Dec 20 '13 at 12:56
  • 1
    And most of your variables are uninitialized. – Joseph Mansfield Dec 20 '13 at 12:56
  • 2
    char is one single character. If you want more than one character, use an array (which gets passed as a pointer to your function). Use strcmp() to compare the strings, == won't do what you expect it to do. Also, you don't have to declare variables (`FS, fs, ...`) for the string literals you use in your function. – Guntram Blohm Dec 20 '13 at 12:57
  • oh. okay okay. I thought that using == will still do the same as strcmp. I'll try that. thank you! – Hanz Mendez Dec 20 '13 at 13:04
  • 1
    @GuntramBlohm: This is C++. Use `std::string` for strings. – Mike Seymour Dec 20 '13 at 13:06
  • @MikeSeymour: you're right. I should pay more attention to tags and less to the code, which didn't look very 'C++'y. – Guntram Blohm Dec 20 '13 at 13:10
  • No, it must have said "char and const char*". In C++, as in C, asterisks are significant. – molbdnilo Dec 20 '13 at 13:15
  • The error _actually_ said `char` and `const char*`. Pay attention to the `*`. You code uses `char`, which is a _single_ character. – Lightness Races in Orbit Dec 20 '13 at 14:13

3 Answers3

3

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.

Community
  • 1
  • 1
Roddy
  • 66,617
  • 42
  • 165
  • 277
  • I've had bad experiences passing const ref's to std:string. I also think that modern implementations are optimized such that passing std:string is virtually as cheap as passing a "naked" char pointer. – S.C. Madsen Dec 20 '13 at 13:11
  • @S.C.Madsen Probably a faulty compiler or library? As for performance, measure it and I'll believe you ;-) (I think C++11 prohibits reference-counted strings, by the way) – Roddy Dec 20 '13 at 13:20
  • 1
    I agree with your inclination to measure performance claims, suggesting you do the same regarding your own claims. – S.C. Madsen Dec 20 '13 at 13:23
  • how do I use this for a pointer? do I write it like this? rsrve.bill=getBill(const std::string &seat) – Hanz Mendez Dec 20 '13 at 13:25
  • @HanzMendez - Please add code showing the context of how you want to use it. – Roddy Dec 20 '13 at 14:21
  • @S.C.Madsen. This answer (and the question) are interesting. http://stackoverflow.com/a/10231768/1737 With C++11, in some *very particular* cases, pass by value may be faster. But typically pass-by-reference is around 10x faster. – Roddy Dec 20 '13 at 14:34
0

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 i.e. 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".

S.C. Madsen
  • 5,100
  • 5
  • 32
  • 50
0

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

Do you mean?

char seat[SOME_INT_VALUE];

instead of just

char seat

Digital_Reality
  • 4,488
  • 1
  • 29
  • 31