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