[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y ] [Search | Free Show | Home]

c++ code help

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 33
Thread images: 3

File: acca.jpg (317KB, 1200x675px) Image search: [Google]
acca.jpg
317KB, 1200x675px
it is posible to convert a string into boolean like Convert.ToBoolean in c# ?
>if yes could you post the code please
>>
1mn bump
>>
What's your use case? That sounds weird
>>
>>57297637
You can test string if its "True" or "False" then return the bool acordingly, is that what you meant?
>>
>>57297637
On my phone, and don't c++, but here is the shit way (don't use this for real):

Bool blah;

If (string == "true")
{
Blah = true;
}
Else if (string == "false)
{
Blah = false;
}

// Error check
If (null == blah)
Return;
Else
Do stuff
>>
>>57297637
Who are you quoting?
>>
>>57297637
Maybe something like this.
#include <algorithm>
#include <string>

bool string_to_bool(std::string input, bool& result) {
std::transform(input.begin(), input.end(), input.begin(), ::tolower);
if (input == "true") {
result = true;
return true;
}
else if (input == "false"){
result = false;
return true;
}
else{
return false;
}
}
>>
bool Convert_ToBoolean(string input)
{
return (string=="true"||string=="True"||string=="TRUE");
}


Surely?
>>
>>57297637
return input == "true";
>>
>>57297980
yeah but how do i check if "1>0" for example =true?
>>
>>57298037
return (input==...)
My bad.
>>
>>57298045
Convert.ToBoolean can't do that in the first place; you'd need to write a custom handler for such examples.
>>
>>57298045
Sounds like some awful spaghetti code you're writing there, I'd reconsider whatever approach you're taking right now
>>
>>57298045
I'm not sure what you are trying to accomplish. I'd say you need to use the regex std lib to parse the left hand value and right hand value and find what the operator is.
>>
>>57298037
What about "TRue" and "trUe" and "tRuE" you pleb? Convert input to lowercase first PLEBIAN
>>
>>57298045
You switch to some language other than sepples.
>>> eval("1>0")
True
>>
>>57297946
>>57297917
>>57297980
>>57298037

CS grad meme tier shit. You should be ashamed

>>57298042

this
>>
>>57297637
Boolean'Value(Your_String);
>>
>>57298037
So... return input.Equals ("true", StringComparison.InvariantCultureIgnoreCase)

Dumbass
>>
File: FoCpG.jpg (185KB, 890x670px) Image search: [Google]
FoCpG.jpg
185KB, 890x670px
>>57298037
>>
>>57300514
What about compiler which will turn all the examples above into same instructions you "non CS grad meme tier shit" clever guy?
>>
>>57297980
Why have an out parameter as well as a return bool? Noone does that.
>>
>>57297637
yes
>>
>>57302014
For exception free code. Converting a string to bool have 3 possible outcomes. A bool has only 2 possible values.
>>
>>57298037
This is the better way.
private static bool Convert2BooleanFunction( string input )
{
return ((( string[0] == 'T' || string[0] == 't' ) &&
( string[1] == 'R' || string[1] == 'r' ) &&
( string[2] == 'U' || string[2] == 'u' ) &&
( string[3] == 'E' || string[3] == 'e' )) ?
true :
((( string[0] == 'F' || string[0] == 'f' ) &&
( string[1] == 'A' || string[1] == 'a' ) &&
( string[2] == 'L' || string[2] == 'l' ) &&
( string[3] == 'S' || string[3] == 's' ) &&
( string[4] == 'E' || string[4] == 'e' )) ?
false :
( throw "Invalid string!" )));
}
>>
>mfw java

return Boolean.parse(boolStr);

no need to worry about case
>>
>>57302014

because exception-handling is a performance hit

>>57302232
you forgot to check that the string is only four characters long
>>
First you have to define if leading and following spaces is valid.
>>
>>57297637

I got you

http://lmgtfy.com/?s=d&q=string+to+boolean
>>
>>57302402

fuck. here it is:
http://lmgtfy.com/?q=string+to+boolean+c%2B%2B
>>
File: 1477766832950.png (1MB, 1200x675px) Image search: [Google]
1477766832950.png
1MB, 1200x675px
>>
>>57301957

they are still retarded

also who knows how well different compilers will optimize it.
>>
>>57297637
Why are /g/fags so ugly?
Thread posts: 33
Thread images: 3


[Boards: 3 / a / aco / adv / an / asp / b / bant / biz / c / can / cgl / ck / cm / co / cock / d / diy / e / fa / fap / fit / fitlit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mlpol / mo / mtv / mu / n / news / o / out / outsoc / p / po / pol / qa / qst / r / r9k / s / s4s / sci / soc / sp / spa / t / tg / toy / trash / trv / tv / u / v / vg / vint / vip / vp / vr / w / wg / wsg / wsr / x / y] [Search | Top | Home]

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
All trademarks and copyrights on this page are owned by their respective parties.
Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.