i know there is atoi and atof for string to int, and double...but wat bout for bool?
i know there is atoi and atof for string to int, and double...but wat bout for bool?
i found it i think....i'll post it here, incase anyone want's it too....
atob()
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
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.....?
Nvm...
Last edited by JoshR; 05-22-2005 at 03:43 AM.
Something like this you mean?
Output 1: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 2:Code:Enter a boolean value (true/false): false You entered: false
The strings entered via the cin need to be in all lowercase for that to work I think.Code:Enter a boolean value (true/false): true You entered: true
"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
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