c++ - 简单的 if 条件与字符串

标签 c++ strcmp

<分区>

Possible Duplicate:
How do I properly compare strings in C?

#include <iostream>
using namespace std;

int main(){

    char name[100];
    cout<<"Enter: ";
    cin>>name;
    if(name == "hello"){
        cout<<"Yes it works!";
    }

    return 0;
}

为什么当我在提示中输入 hello 时,我没有得到“Yes it works!”消息?

最佳答案

您需要使用strcmp 来测试是否相等。

name 是一个数组,不是 std::stringhello 是一个字符串文字,即 const char *。您正在比较指针,而不是字符串。

关于c++ - 简单的 if 条件与字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10373017/

相关文章:

c - Linux登录和strcmp

c - 将字符串与链表数据元素进行比较时,strcmp 不会返回 0

c - strcmp 总是产生相同的结果

c++ - FFMPEG(C++)从缓冲区转换和压缩单个图像

c++ - QT-菜单的退出选项

php - 相同的字符串加盐代码给出不同的结果

C - strcmp() 数组中的两个字符串

c - C 中的 If 语句总是返回 false

c++ - 如何在 Cygwin 文件结构中正确放置非默认 C++ 包

c++ - __NULL_IMPORT_DESCRIPTOR 在 boost_system 和 boost_filesystem