代码之家  ›  专栏  ›  技术社区  ›  adhanlon

调用strcmp时出错从“int”到“const char*”的转换无效

  •  2
  • adhanlon  · 技术社区  · 16 年前

    我使用StcMP来比较C++中的字符数组,但是每次发生STRCMP:错误:从“int”到“const char”的无效转换是:错误:初始化int 2的int int(cconst char,const char)

    我已经包括了string、string.h和stdio.h,这是我的代码,感谢所有回复我的人。

    另外,除了一堆if语句外,还有更好的方法检查缓冲区吗?

    
    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;
    

    }

    1 回复  |  直到 16 年前
        1
  •  8
  •   Community Mohan Dere    6 年前

    "hist" 不 'hist'

    “历史的” 只是一个字符文字(如第节所述) 2.14.3

    字符文字是用单引号括起来的一个或多个字符,如在x中,可以选择前面加上字母u、u或L中的一个,如在uy、Uz或Lx中。

    不以u、u或L开头的字符文字是普通字符文字,也称为窄字符文字。

    包含单个c字符的普通字符文字具有char类型,其值等于执行字符集中c字符编码的数值。

    包含多个c字符的普通字符文字是多字符文字。多字符文字具有int类型和实现定义的值。

    至于有更好的方法,这取决于你所说的更好是什么意思:-)

    一种可能性是建立一个functon表,它基本上是一个结构数组,每个结构包含一个单词和一个函数指针。

    更好的 解决方案,我将把它留给您(这是一种中等先进的技术)-您可能会更好地坚持您所理解的内容。

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

    在运行时使用:

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

    您将获得以下输出:

    Hi there
    Bye for now
    Hi there
    Hi there
    Bye for now
    Hi there
    Bad word: 'bork'