convert string to bool
-
Following is what I am doing... string str1="true"; string str2="false"; bool str; I want to assign the values to str by taking 'true' or false' from str1 or str2. When I do.. str=str1; or str=str2; this does not work and shows an error. Is there any other alternative to this. THANKS
-
Following is what I am doing... string str1="true"; string str2="false"; bool str; I want to assign the values to str by taking 'true' or false' from str1 or str2. When I do.. str=str1; or str=str2; this does not work and shows an error. Is there any other alternative to this. THANKS
string str1="true";
string str2="false";
bool b;
b = str1 == "true" ? true : false;
b = str1 == "true";:) [added] modified according to Jijo Ray [^] observation. [/added]
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 -
Following is what I am doing... string str1="true"; string str2="false"; bool str; I want to assign the values to str by taking 'true' or false' from str1 or str2. When I do.. str=str1; or str=str2; this does not work and shows an error. Is there any other alternative to this. THANKS
if you can....:) do this:
if(str1=="true") str=1; if(str2=="false") str=0;
Mukesh Kumar Software Engineer
-
if you can....:) do this:
if(str1=="true") str=1; if(str2=="false") str=0;
Mukesh Kumar Software Engineer
on doing that I get the following. error C2664: '__thiscall std::basic_string,class std::allocator >::std::basic_string,class std::allocator >(const cla ss std::allocator &)' : cannot convert parameter 1 from 'bool' to 'const class std::allocator &' Reason: cannot convert from 'bool' to 'const class std::allocator' No constructor could take the source type, or constructor overload resolution was ambiguous I get the same even if I do the way Pallini said. :(
-
on doing that I get the following. error C2664: '__thiscall std::basic_string,class std::allocator >::std::basic_string,class std::allocator >(const cla ss std::allocator &)' : cannot convert parameter 1 from 'bool' to 'const class std::allocator &' Reason: cannot convert from 'bool' to 'const class std::allocator' No constructor could take the source type, or constructor overload resolution was ambiguous I get the same even if I do the way Pallini said. :(
-
Thank God :)
Mukesh Kumar Software Engineer
-
Following is what I am doing... string str1="true"; string str2="false"; bool str; I want to assign the values to str by taking 'true' or false' from str1 or str2. When I do.. str=str1; or str=str2; this does not work and shows an error. Is there any other alternative to this. THANKS
A bit more tuned one.
string str1="true";
string str2="false";
bool str;// The == operator already return bool.
str = str1 == "true";Thanks & Regards, Jijo.
________________________________ http://weseetips.com - Visual C++ technical tips.
-
A bit more tuned one.
string str1="true";
string str2="false";
bool str;// The == operator already return bool.
str = str1 == "true";Thanks & Regards, Jijo.
________________________________ http://weseetips.com - Visual C++ technical tips.
Jijo raj wrote:
// The == operator already return bool.
Good point, you're right. :)
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 -
Jijo raj wrote:
// The == operator already return bool.
Good point, you're right. :)
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