Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I compare C- string with C++ string?

I want to find out why compare function doesn't give me correct result ?

As I know it should return 0 if two string are the same!

bool validatePassword(char id[], string password) {

    // cannot be the same as the id string
    if(password.compare(id) == 0) {
        cout << "password can not be as same as id\n";
        return false;
    }

    return true;
}
like image 377
MissDesire Avatar asked Jan 17 '16 00:01

MissDesire


1 Answers

As Matteo Italia mentioned in another answer's comment. Use the std::string's operator== like this:

bool validatePassword(char id[], string password) {
    return password == id;
}

This function is really unnecessary because your caller should call operator== directly instead.

like image 165
George Houpis Avatar answered Sep 27 '22 22:09

George Houpis