Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. ATL / WTL / STL
  4. String problem

String problem

Scheduled Pinned Locked Moved ATL / WTL / STL
helpquestionc++
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    david bagaturia
    wrote on last edited by
    #1

    hi all I have problem im my program, I have: string str_1, str_2; //......... if( strcmp(str_1.data(),str_1.data()) == 0) { cout<<"equals"; } else { cout<<"Not equals"; } problem is that in 'str_1' and 'str_2' I have same text and rezult is "Not equals", can you help me and say how can I compeer two STL string's?

    J J 2 Replies Last reply
    0
    • D david bagaturia

      hi all I have problem im my program, I have: string str_1, str_2; //......... if( strcmp(str_1.data(),str_1.data()) == 0) { cout<<"equals"; } else { cout<<"Not equals"; } problem is that in 'str_1' and 'str_2' I have same text and rezult is "Not equals", can you help me and say how can I compeer two STL string's?

      J Offline
      J Offline
      Jonas Larsson
      wrote on last edited by
      #2

      Hello, If you refer to your STL implementation doc's, you should find that data(): "Returns a pointer to an array of characters, not necessarily null-terminated, representing the string's contents", so it's probably a bad idea to pass that to a function that expects 0-terminated 'strings'. You should use c_str() instead, if you want to use strcmp. A better way, if you're just testing for equality, would be to use std::string's operator ==(), as in if (str_1 == str_1) HTH Jonas

      --- "Man will never be free until the last king is strangled with the entrails of the last priest". -- Denis Diderot

      D 1 Reply Last reply
      0
      • J Jonas Larsson

        Hello, If you refer to your STL implementation doc's, you should find that data(): "Returns a pointer to an array of characters, not necessarily null-terminated, representing the string's contents", so it's probably a bad idea to pass that to a function that expects 0-terminated 'strings'. You should use c_str() instead, if you want to use strcmp. A better way, if you're just testing for equality, would be to use std::string's operator ==(), as in if (str_1 == str_1) HTH Jonas

        --- "Man will never be free until the last king is strangled with the entrails of the last priest". -- Denis Diderot

        D Offline
        D Offline
        david bagaturia
        wrote on last edited by
        #3

        if (str_1 == str_1) ? :confused: there is not operator ==()

        S J 2 Replies Last reply
        0
        • D david bagaturia

          if (str_1 == str_1) ? :confused: there is not operator ==()

          S Offline
          S Offline
          Stephen Hewitt
          wrote on last edited by
          #4

          If you're using STL's std::string there is indeed an operator ==.

          Steve

          1 Reply Last reply
          0
          • D david bagaturia

            if (str_1 == str_1) ? :confused: there is not operator ==()

            J Offline
            J Offline
            Jonas Larsson
            wrote on last edited by
            #5

            In your example code you write if( strcmp(str_1.data(),str_1.data()) == 0) so I just went with the same comparison. std::string has the == operator, please refer to your documentation. If your implementation doesn't, consider updating to a proper implementation. You can also use str_1.compare(str_1) (or str_2 if it's just a type-o) /J

            --- "Man will never be free until the last king is strangled with the entrails of the last priest". -- Denis Diderot

            D 1 Reply Last reply
            0
            • J Jonas Larsson

              In your example code you write if( strcmp(str_1.data(),str_1.data()) == 0) so I just went with the same comparison. std::string has the == operator, please refer to your documentation. If your implementation doesn't, consider updating to a proper implementation. You can also use str_1.compare(str_1) (or str_2 if it's just a type-o) /J

              --- "Man will never be free until the last king is strangled with the entrails of the last priest". -- Denis Diderot

              D Offline
              D Offline
              david bagaturia
              wrote on last edited by
              #6

              thanks. it help me. :)

              1 Reply Last reply
              0
              • D david bagaturia

                hi all I have problem im my program, I have: string str_1, str_2; //......... if( strcmp(str_1.data(),str_1.data()) == 0) { cout<<"equals"; } else { cout<<"Not equals"; } problem is that in 'str_1' and 'str_2' I have same text and rezult is "Not equals", can you help me and say how can I compeer two STL string's?

                J Offline
                J Offline
                John R Shaw
                wrote on last edited by
                #7

                Don’t do that! If you want to compare two string object use “str_1 == str_2”. If you must use the C comparison function then use “strcmp(str_1.c_str(), str_2.c_str())”. Of course the example you are giving will always be true, provide the system does not object, because both arguments are the same.

                INTP "Program testing can be used to show the presence of bugs, but never to show their absence."Edsger Dijkstra

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups