I am currently reading from an ini file with a key/value pair. i.e.
isValid = true
When get the key/value pair I need to convert a string
#include
#include
#include
#include
bool
string2bool (const std::string & v)
{
return !v.empty () &&
(strcasecmp (v.c_str (), "true") == 0 ||
atoi (v.c_str ()) != 0);
}
int
main ()
{
std::string s;
std::cout << "Please enter string: " << std::flush;
std::cin >> s;
std::cout << "This is " << (string2bool (s) ? "true" : "false") << std::endl;
}
An example input and output:
$ ./test
Please enter string: 0
This is false
$ ./test
Please enter string: 1
This is true
$ ./test
Please enter string: 3
This is true
$ ./test
Please enter string: TRuE
This is true
$