开发者

error when I call strcmp Invalid conversion from 'int' to 'const char*'

I'm using strcmp to compare character arrays in c++, but I get the following error for every occurrence of strcmp: error: invalid conversion from 'int' to 'const char*' followed by: error: initializing argument 2 of 'int strcmp(const char*, const char*)'

I've include string, string.h, and stdio.h and here is my code, thanks to all who reply.

Also, is there a better way to check the buffer other than a bunch of if statements?


int main(int argc, char* argv[])
{
    unsigned count = 0;
    bool terminate = false;
    char buffer[128];

do {
    // Print prompt and get input
    count++;
    print_prompt(count);
    cin.getline(buffer, 128);

    // check if input was 开发者_开发技巧greater than 128, then check for built-in commands
    // and finally execute command
    if (cin.fail()) {
        cerr << "Error: Commands must be no more than 128 characters!" << endl;
    }
    else if ( strcmp(buffer, 'hist') == 0 ) {
        print_Hist();
    }
    else if ( strcmp(buffer, 'curPid') == 0 ) {
        // get curPid
    }
    else if ( strncmp(buffer, 'cd ', 3) == 0 ) {
        // change directory
    }
    else if ( strcmp(buffer, 'quit') == 0 ) {
        terminate = true;
    }
    else {
        //run external command
    }

} while(!terminate);

return 0;

}


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'
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜