comparing strings

Discussion in 'C++' started by BrianJones, Jul 20, 2004.

  1. BrianJones

    BrianJones Guest

    I have a problem with the int strcmp(str1,str2) function:

    When I do:

    char *pass;
    char *passv;

    pass = getpass("Please enter.....");
    passv = getpass("Please verify.....");

    if(strcmp(pass,passv)==1) // i.e. failure{
    cout<<"Verification failed!<<endl;

    Problem: Does't seem to work properly, even though the strings, pass and
    passv are exactly the same. for some reason, it doen't go in to the if
    statement Any syggestions?

    BrianJones, Jul 20, 2004
    1. Advertisements

  2. BrianJones

    Rolf Magnus Guest

    Yes, read your compiler's documentation about strcmp. strcmp returns a
    number less than zero if the first string is 'less' than the second,
    and greater than zero if the second string is 'less'. The exact value
    is not specified. It might be 1 or 10 or 100, or it might depend on the
    exact combination of strings you pass to strcmp. So comparing the
    result to 1 is wrong. Write:

    if (strcmp(pass, passv) != 0) ...

    Another (better) idea would be to use strings. Change your getpass
    function so that it returns a string and write:

    std::string pass = getpass("Please enter.....");
    std::string passv = getpass("Please verify.....");

    if (pass != passv) // i.e. failure
    cout<<"Verification failed!<<endl;
    Rolf Magnus, Jul 20, 2004
    1. Advertisements

  3. * BrianJones:
    Use std::string instead of char*.

    Btw. the if statement doesn't check for all possible outcomes, and
    it's very unclear who's got deallocation responsibility for pass and

    Both these problems are solved by using std::string.
    Alf P. Steinbach, Jul 20, 2004
  4. strcmp returns 0 if the two strings are equal. It returns a value less
    than 0 if the first string is "less than" the second or a value greater
    than 0 if the first string is "greater than" the second. I've never
    seen anyone stating it should return the value 1 in any circumstance.
    You might use:

    #include <string>
    #include <iostream>
    #include <unistd.h> //[0]

    std::string pass = getpass("Please enter...");
    std::string passv = getpass("Please verify...");

    if (pass != passv) {
    std::cout << "Doesn't match.\n";

    // End code
    [0]: According to my documentation, char *getpass(const char*) is
    declared in the nonstandard header unistd.h. It returns a pointer to
    static memory, which might get overwritten by any sequential call to
    getpass. This could make sure the two strings always compare equal in
    mr. Jones' code. The use of std::string would fix this.
    Robert Bauck Hamar, Jul 20, 2004
  5. BrianJones

    Siemel Naran Guest

    As others have pointed out, strcmp returns negative, zero, or positive. You
    can't attach significance to the particular positive or negative number
    returned, as the number varies by implementation. To test if two strings
    are not equal, just check if strcmp is not equal to zero.

    But there's another issue.

    Does getpass return a pointer to a static char[] array? If so, then you
    don't have to delete the array, but each successive call to getpass will
    overwrite the previous value in the char[] array, so by definition
    strcmp(pass, passv) will always be zero. To check if this is so, perform
    cout << (pass == passv); to compare the values of the pointers. Use
    std::string to copy the char[] array into a string, then feel free to call
    getpass again, as in Rolf's and Robert's code. As a bonus, you get to use
    !=, which is easier to read than strcmp(...)==0.
    Siemel Naran, Jul 21, 2004
  6. BrianJones

    JKop Guest

    BrianJones posted:
    Without a definition of "getpass", I can only guess at...

    Do you think there's anything wrong with the following:

    int main()
    int* jk;

    *jk = 4;

    JKop, Jul 21, 2004
  7. BrianJones

    Default User Guest

    It certainly is that way on my Solaris implementation:

    The return value points to static data whose content may be
    overwritten by each call.

    I'm not sure, but I think getpass() is POSIX and likely has the same
    behavior on other implementations. The newsgroup comp.unix.programmer
    would be a good place to discuss that if the OP needs further

    Brian Rodenborn
    Default User, Jul 21, 2004
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.