+8  A: 

Your comparison string are incorrect. They should be of the form "hist", not 'hist'.

In C++, 'hist' is simply a character literal (as stated in section 2.14.3 of the C++0x draft (n2914) standard), my emphasis on the last paragraph:

A character literal is one or more characters enclosed in single quotes, as in ’x’, optionally preceded by one of the letters u, U, or L, as in u’y’, U’z’, or L’x’, respectively.

A character literal that does not begin with u, U, or L is an ordinary character literal, also referred to as a narrow-character literal.

An ordinary character literal that contains a single c-char has type char, with value equal to the numerical value of the encoding of the c-char in the execution character set.

An ordinary character literal that contains more than one c-char is a multicharacter literal. A multicharacter literal has type int and implementation-defined value.

As to there being a better way, it depends on what you mean by better :-)

One possibility is to set up a functon table which is basically an array of structs, each containing a word and a function pointer.

Then you simply extract the word from your string and do a lookup in that array, calling the function if you find a match. The following C program shows how to use function tables. As to whether that's a better solution, I'll leave it up to you (it's a moderately advanced technique) - you may be better off sticking with what you understand.

#include <stdio.h>

typedef struct {         // This type has the word and function pointer
    char *word;          // to call for that word. Major limitation is
    void (*fn)(void);    // that all functions must have the same
} tCmd;                  // signature.

// These are the utility functions and the function table itself.

void hello (void) { printf ("Hi there\n"); }
void goodbye (void) { printf ("Bye for now\n"); }

tCmd cmd[] = {{"hello",&hello},{"goodbye",&goodbye}};

// Demo program, showing how it's done.

int main (int argc, char *argv[]) {
    int i, j;

    // Process each argument.

    for (i = 1; i < argc; i++) {
        //Check against each word in function table.

        for (j = 0; j < sizeof(cmd)/sizeof(*cmd); j++) {
            // If found, execute function and break from inner loop.

            if (strcmp (argv[i],cmd[j].word) == 0) {
                (cmd[j].fn)();
                break;
            }
        }

        // Check to make sure we broke out of loop, otherwise not a avlid word.

        if (j == sizeof(cmd)/sizeof(*cmd)) {
            printf ("Bad word: '%s'\n", argv[i]);
        }
    }

    return 0;
}

When run with:

pax> ./qq.exe hello goodbye hello hello goodbye hello bork

you get the output:

Hi there
Bye for now
Hi there
Hi there
Bye for now
Hi there
Bad word: 'bork'
paxdiablo
It should also be noted that multicharacter character literals have an implementation-defined endianness.
Adam Rosenfield
@Adam, I think that's covered in the quote from the standard. Interestingly, it says the *value* is implementation defined and I can't see anything that states they have to be distinct. I think that means an implementation would be free to set the value to 0 for all multicharacter literals :-)
paxdiablo
of course, wow. Thank you so much.
Silmaril89
@Adam: Multi-character literals have implemnetation-defined *everything*, just like bitfields, for example. Although why it should be noted here is not immeadeately clear to me...
AndreyT