c++ - 当我调用 strcmp 从 'int' 到 'const char*' 的无效转换时出错

标签 c++ strcmp

我正在使用 strcmp 比较 C++ 中的字符数组,但每次出现 strcmp 时都会出现以下错误:错误:从“int”到“const char*”的无效转换后跟:错误:初始化参数 2 的'int strcmp(const char*, const char*)'

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

此外,除了一堆 if 语句之外,是否有更好的检查缓冲区的方法?


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

<pre><code>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;
</code></pre>

<p>}
</p>

最佳答案

您的比较字符串不正确。它们的格式应为 "hist",而不是 'hist'

在 C++ 中,'hist' 只是一个字 rune 字(如 C++0x 草案 (n2914) 标准的 2.14.3 部分所述),我的强调最后一段:

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.

至于有没有更好的方法,这取决于你所说的更好:-)

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

然后您只需从字符串中提取单词并在该数组中进行查找,如果找到匹配项则调用该函数。下面的 C 程序显示了如何使用函数表。至于这是否是一个更好的解决方案,我会留给你(这是一种中等高级的技术)——你最好坚持你所理解的。

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

关于c++ - 当我调用 strcmp 从 'int' 到 'const char*' 的无效转换时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1635130/

相关文章:

c++使用友好类的类型作为模板参数声明STL vector

c - 登录脚本未按预期比较字符串输入

C++ #include of file 使 std 类型未知

c++ - 我可以调用一个方法,而无需在 c++ 中传递所需的参数。怎么来的?

c - 字符串比较在 C 语言中无法按预期工作

C - Strcmp() 不工作

c - 在 C 中使用 strcspn 和 fgets 的登录功能问题

c - C语言中如何删除单链表?

c++ - Visual Studio : Disable Automatic Initialization

c++ - 嵌套的 boost::assign:list_of 在 Visual Studio 2012 中损坏 - 对重载函数的调用不明确