i know there is atoi and atof for string to int, and double...but wat bout for bool?
Printable View
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.
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...
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
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".