== evaluation problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Gary Wessle

    #1

    == evaluation problem

    Hi

    I ran my code in gdb after it was giving me un-expected results, to
    find out why and I found where the problem is but I don't understand
    why it happened.

    if( username == us && password == pa ) {
    cout << "connected. " << endl;
    on_off = true;
    } else {
    cout << "wrong username or password" << endl;
    }

    *************** *************** *************** *************** ****
    (gdb) p username
    $6 = 0x805114c "sam"
    (gdb) p us
    $7 = 0x8051235 "sam"
    (gdb) p username == us
    $8 = false
    (gdb) p password
    $9 = 0x8051150 "jesse"
    (gdb) p pa
    $10 = 0x8051239 "jesse"
    (gdb) p password == pa
    $11 = false
    (gdb) p username == us && password == pa
    $12 = false
    (gdb)
    *************** *************** *************** *************** ****
    I was expecting "True" instead of false above.

    thanks
  • Alf P. Steinbach

    #2
    Re: == evaluation problem

    * Gary Wessle:
    >
    I ran my code in gdb after it was giving me un-expected results, to
    find out why and I found where the problem is but I don't understand
    why it happened.
    >
    if( username == us && password == pa ) {
    cout << "connected. " << endl;
    on_off = true;
    } else {
    cout << "wrong username or password" << endl;
    }
    >
    *************** *************** *************** *************** ****
    (gdb) p username
    $6 = 0x805114c "sam"
    (gdb) p us
    $7 = 0x8051235 "sam"
    (gdb) p username == us
    $8 = false
    (gdb) p password
    $9 = 0x8051150 "jesse"
    (gdb) p pa
    $10 = 0x8051239 "jesse"
    (gdb) p password == pa
    $11 = false
    (gdb) p username == us && password == pa
    $12 = false
    (gdb)
    *************** *************** *************** *************** ****
    I was expecting "True" instead of false above.
    It seems you're comparing pointers instead of strings.

    A good solution is to use std::string.

    A bad solution is to keep the raw arrays and use std::strcmp instead of ==.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?

    Comment

    • Jim Langston

      #3
      Re: == evaluation problem


      "Gary Wessle" <phddas@yahoo.c omwrote in message
      news:m364dk1n49 .fsf@localhost. localdomain...
      Hi
      >
      I ran my code in gdb after it was giving me un-expected results, to
      find out why and I found where the problem is but I don't understand
      why it happened.
      >
      if( username == us && password == pa ) {
      cout << "connected. " << endl;
      on_off = true;
      } else {
      cout << "wrong username or password" << endl;
      }
      >
      *************** *************** *************** *************** ****
      (gdb) p username
      $6 = 0x805114c "sam"
      0x805114C looks like a pointer, so I'm guessing username is a char* (char
      array).

      You can't comapre character arrays using ==, using == on 2 character arrays
      will state if the pointers are the same, which they usually won't be. If
      you must use character arrays, you need to use strcmp (string compare) whice
      will evaluate to 0 if the strings are 0. So:

      char username[] = "John Smith";
      char us[] = "John Smith";
      if ( strcmp( username, us ) == 0 && strcmp( password, pa ) == 0 )
      // yada yada

      There is also a strncmp to cmpare n characters.

      It is much better to use std::string so you don't have to do this type of
      thing. Then you can use ==

      std::string username = "John Smith";
      std::string us "John Smith";

      if ( username == us )
      // yada yada
      (gdb) p us
      $7 = 0x8051235 "sam"
      (gdb) p username == us
      $8 = false
      (gdb) p password
      $9 = 0x8051150 "jesse"
      (gdb) p pa
      $10 = 0x8051239 "jesse"
      (gdb) p password == pa
      $11 = false
      (gdb) p username == us && password == pa
      $12 = false
      (gdb)
      *************** *************** *************** *************** ****
      I was expecting "True" instead of false above.
      >
      thanks

      Comment

      • Gary Wessle

        #4
        Re: == evaluation problem

        "Jim Langston" <tazmaster@rock etmail.comwrite s:
        "Gary Wessle" <phddas@yahoo.c omwrote in message
        news:m364dk1n49 .fsf@localhost. localdomain...
        Hi

        I ran my code in gdb after it was giving me un-expected results, to
        find out why and I found where the problem is but I don't understand
        why it happened.

        if( username == us && password == pa ) {
        cout << "connected. " << endl;
        on_off = true;
        } else {
        cout << "wrong username or password" << endl;
        }

        *************** *************** *************** *************** ****
        (gdb) p username
        $6 = 0x805114c "sam"
        >
        0x805114C looks like a pointer, so I'm guessing username is a char* (char
        array).
        >
        You can't comapre character arrays using ==, using == on 2 character arrays
        will state if the pointers are the same, which they usually won't be. If
        you must use character arrays, you need to use strcmp (string compare) whice
        will evaluate to 0 if the strings are 0. So:
        >
        char username[] = "John Smith";
        char us[] = "John Smith";
        if ( strcmp( username, us ) == 0 && strcmp( password, pa ) == 0 )
        // yada yada
        >
        There is also a strncmp to cmpare n characters.
        >
        It is much better to use std::string so you don't have to do this type of
        thing. Then you can use ==
        I am limited because the library provided "by Big Bucks Inc." uses
        const char* username and same for password. so I have to supply the
        same data type in-order to use their methods.

        Comment

        • Gavin Deane

          #5
          Re: == evaluation problem


          Gary Wessle wrote:
          "Jim Langston" <tazmaster@rock etmail.comwrite s:
          It is much better to use std::string so you don't have to do this type of
          thing. Then you can use ==
          >
          I am limited because the library provided "by Big Bucks Inc." uses
          const char* username and same for password. so I have to supply the
          same data type in-order to use their methods.
          You might not be as limited as you think. If the library you are using
          ever gives you a const char* you can immediately put it in a
          std::string. Whenever you need to give a const char* to the library,
          use the c_str() member function of std::string. You can use std::string
          throughout your own code. Use of C style strings need not propogate
          outside the interface to the library.

          Gavin Deane

          Comment

          • BobR

            #6
            Re: == evaluation problem


            Gary Wessle wrote in message ...
            >"Jim Langston" <tazmaster@rock etmail.comwrite s:
            >
            >You can't comapre character arrays using ==, using == on 2 character
            arrays
            >will state if the pointers are the same, which they usually won't be. If
            >you must use character arrays, you need to use strcmp (string compare)
            whice
            >will evaluate to 0 if the strings are 0. So:
            >>
            >char username[] = "John Smith";
            >char us[] = "John Smith";
            >if ( strcmp( username, us ) == 0 && strcmp( password, pa ) == 0 )
            >// yada yada
            >>
            >There is also a strncmp to cmpare n characters.
            >>
            >It is much better to use std::string so you don't have to do this type of
            >thing. Then you can use ==
            >
            >I am limited because the library provided "by Big Bucks Inc." uses
            >const char* username and same for password. so I have to supply the
            >same data type in-order to use their methods.
            {
            char bust[] = "Issimple";
            char const *busted = bust;
            std::string Name( bust );
            std::string Name2 = busted;
            cout <<"\n string Name ="<<Name<< std::endl;
            cout <<" string Name2 ="<<Name2<< std::endl;
            }
            // out: string Name =Issimple
            // out: string Name2 =Issimple

            // void BigBucks( char const *dude);

            BigBucks( Name.c_str() );


            Now, what were you saying? <G>

            --
            Bob R
            POVrookie


            Comment

            Working...