当我输入 srtike 时"access granted"没有出现

When i enter srtike "access granted" doesn't appear

本文关键字:granted access 输入 srtike      更新时间:2023-10-16
#include<iostream.h>
#include<string.h>
void main() {
    char a[20];
    cout<<"Please enter the required password"<<endl;
    cin>>a;
    if (a=="srtike"){
        cout<<"Access granted!"<<endl;
    }
    gets(a);
}

不能将数组与字符串进行比较,结果是数组衰变成指向数组第一个字符的指针,然后将该指针与指向字符串的指针进行比较,该指针始终为false。

相反,如果你想使用C字符串,你应该使用strcmp来比较字符串:

if (strcmp(a, "srtike") == 0)
{
    // the contents of a equals "srtike"
    ...
}

否则,我建议你看看std::string,它可以像你一样进行比较,因为operator==是重载的。