(C++ Beginner) I hate to post here, but... why won't my "if" statement evaluate a string?

Started by
34 comments, last by Sarmon 11 years, 9 months ago

Using this to not store the permutations is not saving space.


Actually, it saves a quarter of a meg ;-)

Which is eight times as much memory as my first computer had!
Advertisement
[size=2][Edit:] Double-posted for some reason. Can't seem to find the 'delete post' button.

I seriously hope you're jocking.
Using this to not store the permutations is not saving space.
Rather, doing the permutation thing is brain damaged. And don't even get me started on checking the match. I'm sure I've seen it on the daily WTF.
Thus, not storing them is not about saving space but rather doing things right.

I'm confused about what you are disagreeing with.

Are you saying that this...

if(name == "person mcperson" || name == "Person Mcperson" || name == "Person McPerson"
|| name == "PERSON MCPERSON" || name == "person MCPERSON" || name == "PERSON mcperson" || ...etc... )
{
//...
}


...is better than this:
std::string name = "Person McPerson";
std::transform(name.begin(), name.end(), name.begin(), ::tolower);
if(name == "person mcperson")
{
//...
}


Or are you just saying to cache the lowercase name when possible, so you don't have to convert it every function call?

As for 'saving space', I fully agree that any 'space' you save is absurdly small and unimportant if developing for a modern PC. For me, the benefit of the second one is about writing cleaner and easier-to-maintain code, and also ensuring you catch all the valid possibilities and not just some of them.


Actually, it saves a quarter of a meg ;-)
Which is eight times as much memory as my first computer had!

How does it save a quarter of a megabyte? Worst case scenario, with the example I gave ("Person McPerson"), it'd save 32 kilabytes I think - and only if someone bothered to type out every possible lowercase vs uppercase version of "Person McPerson", starting with "person mcperson", "Person mcperson", "PErson mcperson", "PERson mcperson", and so on, which is unlikely.
2^14 permutations * 16 bytes (assuming ascii, 14 characters + a space + a null) = 256kb!


if (username == "Tiffany" || "tiffany" || "Tiffany McClure" || "tiffany McClure" || "Tiffany Mcclure" || "tiffany mcclue")





Am I the only one thinking that looks pretty elegant? Is there any language that implements this way of checking the same variable for different boolean cases?

It looks to me like "Tiffany" is the real string to look for here, so I would convert to lowercase and check for "tiffany".. what if she wrotes "Tiffany Cupcake" ? :P You'll miss that.

Stefano Casillo
TWITTER: [twitter]KunosStefano[/twitter]
AssettoCorsa - netKar PRO - Kunos Simulazioni


[quote name='bls61793' timestamp='1342363560' post='4959267']

if (username == "Tiffany" || "tiffany" || "Tiffany McClure" || "tiffany McClure" || "Tiffany Mcclure" || "tiffany mcclue")





Am I the only one thinking that looks pretty elegant? Is there any language that implements this way of checking the same variable for different boolean cases?

It looks to me like "Tiffany" is the real string to look for here, so I would convert to lowercase and check for "tiffany".. what if she wrotes "Tiffany Cupcake" ? tongue.png You'll miss that.
[/quote]Some languages allow this:
switch(name){
case "Tiffany":
case "tiffany":
case "Tiffany McClure":
...
break;
default:
...
}
Close enough?

Some languages allow this:
switch(name){
case "Tiffany":
case "tiffany":
case "Tiffany McClure":
...
break;
default:
...
}
Close enough?

C++ switch statements only work with some data types (mostly ints) - not with std::strings. Even so, that's pretty ugly syntax. happy.png
Am I the only one who noticed the typo ([font=courier new,courier,monospace]... "tiffany McClure" || "Tiffany Mcclure" || "tiffany mcclue"[/font])? One big reason checking each permutation sucks is because it's too easy to introduce human error.

I'd say convert to lower (or upper) case and check the string. I love regular expressions, but I probably wouldn't use them here.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]

[quote name='King Mir' timestamp='1342470870' post='4959728']
Some languages allow this:
switch(name){
case "Tiffany":
case "tiffany":
case "Tiffany McClure":
...
break;
default:
...
}
Close enough?

C++ switch statements only work with some data types (mostly ints) - not with std::strings. Even so, that's pretty ugly syntax. happy.png
[/quote]Which is why I said "some languages" and not "c++ and some languages". Why do you think switches are ugly?

[quote name='Servant of the Lord' timestamp='1342471896' post='4959736']
C++ switch statements only work with some data types (mostly ints) - not with std::strings. Even so, that's pretty ugly syntax. happy.png
Which is why I said "some languages" and not "c++ and some languages"[/quote]Ah, I thought your "some languages" was a not-so-subtle "hint hint", referring to C++ itself (What with the topic being about C++, and the "Close enough?" comment).

Why do you think switches are ugly?[/quote]
Not switches, per se, but using switches (which are typically meant to branch logic) to imitate the use of AND logic (which is what && is for).
In some situations it may be the best solution, but in general I prefer to use if() when I mean logical IF, and && when I mean logical AND. smile.png

This topic is closed to new replies.

Advertisement