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. Converting string to bool

Converting string to bool

Scheduled Pinned Locked Moved C / C++ / MFC
18 Posts 6 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 pl_kode

    I am getting a string from map that can be either true or false. Is there any function or some alternative to convert this string into bool. So that I can store it in a bool variable.

    string temp;
    temp=MyMap.find("Check")->second; // give either true or false

    I want to convert temp to bool. THANKS.

    T Offline
    T Offline
    toxcct
    wrote on last edited by
    #3

    what kind of strings are supposed to be stored in MyMap.find("Check")->second ? I mean, you'll have to test programmatically the string to assign another boolean variable to true or false...

    [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

    C 1 Reply Last reply
    0
    • P pl_kode

      I am getting a string from map that can be either true or false. Is there any function or some alternative to convert this string into bool. So that I can store it in a bool variable.

      string temp;
      temp=MyMap.find("Check")->second; // give either true or false

      I want to convert temp to bool. THANKS.

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

      what about

      bool f = temp == "true";

      :)

      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 Cedric Moonen

        bool bChecked = false;
        if (temp == "true")
        bChecked = true;

        :~. You have to check for the case also but you got the idea.

        Cédric Moonen Software developer
        Charting control [v1.4]

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

        Thanks for the reply. This can be done. But instead I am looking for some function that does this like atoi() or itoa() for string to int and vice versa. Is there any function for strings to bool.

        T C C 3 Replies Last reply
        0
        • T toxcct

          what kind of strings are supposed to be stored in MyMap.find("Check")->second ? I mean, you'll have to test programmatically the string to assign another boolean variable to true or false...

          [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #6

          toxcct wrote:

          what kind of strings are supposed to be stored in MyMap.find("Check")->second ?

          From his snippet of code, I would say std::string.

          Cédric Moonen Software developer
          Charting control [v1.4]

          T 1 Reply Last reply
          0
          • C Cedric Moonen

            toxcct wrote:

            what kind of strings are supposed to be stored in MyMap.find("Check")->second ?

            From his snippet of code, I would say std::string.

            Cédric Moonen Software developer
            Charting control [v1.4]

            T Offline
            T Offline
            toxcct
            wrote on last edited by
            #7

            you misunderstood me. I meant what is contained within that string ? 1/0 ? true/false ? checked/unchecked ? white/black ?

            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

            C P 2 Replies Last reply
            0
            • P pl_kode

              Thanks for the reply. This can be done. But instead I am looking for some function that does this like atoi() or itoa() for string to int and vice versa. Is there any function for strings to bool.

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #8

              please reply to this question[^] first before we can reply to you efficiently

              [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

              1 Reply Last reply
              0
              • T toxcct

                you misunderstood me. I meant what is contained within that string ? 1/0 ? true/false ? checked/unchecked ? white/black ?

                [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                C Offline
                C Offline
                Cedric Moonen
                wrote on last edited by
                #9

                true/false :) (well, that what he said in his post)

                Cédric Moonen Software developer
                Charting control [v1.4]

                T 1 Reply Last reply
                0
                • C Cedric Moonen

                  true/false :) (well, that what he said in his post)

                  Cédric Moonen Software developer
                  Charting control [v1.4]

                  T Offline
                  T Offline
                  toxcct
                  wrote on last edited by
                  #10

                  I prefer to be sure... :-D

                  [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                  1 Reply Last reply
                  0
                  • C CPallini

                    what about

                    bool f = temp == "true";

                    :)

                    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
                    #11

                    You still remember it. :laugh: Regards, Jijo.

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

                    C 1 Reply Last reply
                    0
                    • P pl_kode

                      Thanks for the reply. This can be done. But instead I am looking for some function that does this like atoi() or itoa() for string to int and vice versa. Is there any function for strings to bool.

                      C Offline
                      C Offline
                      Cedric Moonen
                      wrote on last edited by
                      #12

                      pl_kode wrote:

                      But instead I am looking for some function that does this like atoi() or itoa() for string to int and vice versa

                      Why ? That's quite simple, why do you want to use an existing function for that ?

                      pl_kode wrote:

                      Is there any function for strings to bool.

                      Not as far as I know.

                      Cédric Moonen Software developer
                      Charting control [v1.4]

                      1 Reply Last reply
                      0
                      • J Jijo Raj

                        You still remember it. :laugh: Regards, Jijo.

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

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

                        Well I forgot for a while, but then suddenly I spotted you vulture ;P ready for hitting me! :-D

                        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
                        • P pl_kode

                          Thanks for the reply. This can be done. But instead I am looking for some function that does this like atoi() or itoa() for string to int and vice versa. Is there any function for strings to bool.

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

                          Good news: it exists, is atob. The bad news is that you have to code it (anyway you got so many hints about). :-D

                          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
                          • T toxcct

                            you misunderstood me. I meant what is contained within that string ? 1/0 ? true/false ? checked/unchecked ? white/black ?

                            [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

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

                            true/ false is stored in that

                            1 Reply Last reply
                            0
                            • P pl_kode

                              I am getting a string from map that can be either true or false. Is there any function or some alternative to convert this string into bool. So that I can store it in a bool variable.

                              string temp;
                              temp=MyMap.find("Check")->second; // give either true or false

                              I want to convert temp to bool. THANKS.

                              S Offline
                              S Offline
                              sudhir_Kumar
                              wrote on last edited by
                              #16

                              bool bVal = (temp == "true"?true:false);

                              -@SuDhIrKuMaR@-

                              T 1 Reply Last reply
                              0
                              • S sudhir_Kumar

                                bool bVal = (temp == "true"?true:false);

                                -@SuDhIrKuMaR@-

                                T Offline
                                T Offline
                                toxcct
                                wrote on last edited by
                                #17

                                why do you need the ?: operator ? the std::string::operator==() already returns a bool. in the same direction, why wouldn't you write this then :

                                bool bVal = ((temp == "true") == true ? true : false)

                                or

                                bool bVal = (((temp == "true") == true) == true ? true : false)

                                or just

                                bool bVal = (temp == "true")

                                ;P

                                [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                                1 Reply Last reply
                                0
                                • C CPallini

                                  Well I forgot for a while, but then suddenly I spotted you vulture ;P ready for hitting me! :-D

                                  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
                                  #18

                                  hehehhehe!!! :laugh:

                                  _____________________________________________________ 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