string to bool

This is a discussion on string to bool within the C++ Programming forums, part of the General Programming Boards category; i know there is atoi and atof for string to int, and double...but wat bout for bool?...

  1. #1
    Deleted Account
    Join Date
    Mar 2005
    Posts
    57

    string to bool

    i know there is atoi and atof for string to int, and double...but wat bout for bool?

  2. #2
    Deleted Account
    Join Date
    Mar 2005
    Posts
    57
    i found it i think....i'll post it here, incase anyone want's it too....
    atob()

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Not standard.

    Use a stringstream. It will parse "1" and "true" to true, "0" and "false" to false, and everything to an error state.


    Edit: atob might be in the C99 standard. But it is not in any C++ standard.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Deleted Account
    Join Date
    Mar 2005
    Posts
    57
    yea..the function didn't work..i was checkin that string consists of only a 1 or 0 then converting it to atob but didn't work...

    how exactly do use that function u mentioned.....?

  5. #5
    *this
    Join Date
    Mar 2005
    Posts
    498
    Nvm...
    Last edited by JoshR; 05-22-2005 at 04:43 AM.

  6. #6
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Something like this you mean?

    Code:
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
        bool val = true;
    
        cout << "Enter a boolean value (true/false): "
    
        cin >> boolalpha >> val;
    
        cout << "You entered: " << boolalpha << val << endl;
    
        return 0;
    }
    Output 1:
    Code:
    Enter a boolean value (true/false): false
    You entered: false
    Output 2:
    Code:
    Enter a boolean value (true/false): true
    You entered: true
    The strings entered via the cin need to be in all lowercase for that to work I think.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  7. #7
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    To be precise, the string entered needs to case-sensitively match either the return value of truename() or of falsename(), both methods of the numpunct facet of the stream locale. These return values are "true" and "false" for the C locale. A German locale might set them to "wahr" and "falsch".
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  2. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 01:56 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 07:49 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21