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. C / C++ / MFC
  4. convert string to bool

convert string to bool

Scheduled Pinned Locked Moved C / C++ / MFC
help
9 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.
  • P Offline
    P Offline
    pl_kode
    wrote on last edited by
    #1

    Following is what I am doing... string str1="true"; string str2="false"; bool str; I want to assign the values to str by taking 'true' or false' from str1 or str2. When I do.. str=str1; or str=str2; this does not work and shows an error. Is there any other alternative to this. THANKS

    C M J 3 Replies Last reply
    0
    • P pl_kode

      Following is what I am doing... string str1="true"; string str2="false"; bool str; I want to assign the values to str by taking 'true' or false' from str1 or str2. When I do.. str=str1; or str=str2; this does not work and shows an error. Is there any other alternative to this. THANKS

      C Offline
      C Offline
      CPallini
      wrote on last edited by
      #2

      string str1="true";
      string str2="false";
      bool b;
      b = str1 == "true" ? true : false;
      b = str1 == "true";

      :) [added] modified according to Jijo Ray [^] observation. [/added]

      If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
      This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

      1 Reply Last reply
      0
      • P pl_kode

        Following is what I am doing... string str1="true"; string str2="false"; bool str; I want to assign the values to str by taking 'true' or false' from str1 or str2. When I do.. str=str1; or str=str2; this does not work and shows an error. Is there any other alternative to this. THANKS

        M Offline
        M Offline
        Mukesh Kumar
        wrote on last edited by
        #3

        if you can....:) do this: if(str1=="true") str=1; if(str2=="false") str=0;

        Mukesh Kumar Software Engineer

        P 1 Reply Last reply
        0
        • M Mukesh Kumar

          if you can....:) do this: if(str1=="true") str=1; if(str2=="false") str=0;

          Mukesh Kumar Software Engineer

          P Offline
          P Offline
          pl_kode
          wrote on last edited by
          #4

          on doing that I get the following. error C2664: '__thiscall std::basic_string,class std::allocator >::std::basic_string,class std::allocator >(const cla ss std::allocator &)' : cannot convert parameter 1 from 'bool' to 'const class std::allocator &' Reason: cannot convert from 'bool' to 'const class std::allocator' No constructor could take the source type, or constructor overload resolution was ambiguous I get the same even if I do the way Pallini said. :(

          P 1 Reply Last reply
          0
          • P pl_kode

            on doing that I get the following. error C2664: '__thiscall std::basic_string,class std::allocator >::std::basic_string,class std::allocator >(const cla ss std::allocator &)' : cannot convert parameter 1 from 'bool' to 'const class std::allocator &' Reason: cannot convert from 'bool' to 'const class std::allocator' No constructor could take the source type, or constructor overload resolution was ambiguous I get the same even if I do the way Pallini said. :(

            P Offline
            P Offline
            pl_kode
            wrote on last edited by
            #5

            I got it correct. THANKS.. ;)

            M 1 Reply Last reply
            0
            • P pl_kode

              I got it correct. THANKS.. ;)

              M Offline
              M Offline
              Mukesh Kumar
              wrote on last edited by
              #6

              Thank God :)

              Mukesh Kumar Software Engineer

              1 Reply Last reply
              0
              • P pl_kode

                Following is what I am doing... string str1="true"; string str2="false"; bool str; I want to assign the values to str by taking 'true' or false' from str1 or str2. When I do.. str=str1; or str=str2; this does not work and shows an error. Is there any other alternative to this. THANKS

                J Offline
                J Offline
                Jijo Raj
                wrote on last edited by
                #7

                A bit more tuned one.

                string str1="true";
                string str2="false";
                bool str;

                // The == operator already return bool.
                str = str1 == "true";

                Thanks & Regards, Jijo.

                ________________________________ http://weseetips.com - Visual C++ technical tips.

                C 1 Reply Last reply
                0
                • J Jijo Raj

                  A bit more tuned one.

                  string str1="true";
                  string str2="false";
                  bool str;

                  // The == operator already return bool.
                  str = str1 == "true";

                  Thanks & Regards, Jijo.

                  ________________________________ http://weseetips.com - Visual C++ technical tips.

                  C Offline
                  C Offline
                  CPallini
                  wrote on last edited by
                  #8

                  Jijo raj wrote:

                  // The == operator already return bool.

                  Good point, you're right. :)

                  If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                  This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                  J 1 Reply Last reply
                  0
                  • C CPallini

                    Jijo raj wrote:

                    // The == operator already return bool.

                    Good point, you're right. :)

                    If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
                    This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke

                    J Offline
                    J Offline
                    Jijo Raj
                    wrote on last edited by
                    #9

                    Thanks Pallini. :)

                    _____________________________________________________ http://weseetips.com[^] Visual C++ tips and tricks. Updated daily.

                    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