Followers 0

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

## 35 posts in this topic

[quote name='Servant of the Lord' timestamp='1342459239' post='4959658']
I'm confused about what you are disagreeing with.

Are you saying that this...
[code]
if(name == "person mcperson" || name == "Person Mcperson" || name == "Person McPerson"
|| name == "PERSON MCPERSON" || name == "person MCPERSON" || name == "PERSON mcperson" || ...etc... )
{
//...
}[/code]

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

Or are you just saying to cache the lowercase name when possible, so you don't have to convert it every function call?[/quote]Neither, but it appears you're likely got things in reverse.
What I'm saying is that the whole notion of string (upprer/lower case) permutations is flat out [b]insane[/b].The difference is conceptual, even before we start thinking about comparing strings, so many things have gone wrong I can hardly believe.[list=1]
[*]there should be input sanitization forcing a coherent format... but let this pass.
[*]everyone thinking about storing or even computing those permutations is on the wrong track: this is clearly not the way to do things (very special bonus for elaborations about working on strings loaded from a file you know nothing about!). But what the heck, let this pass as well.
[*]everyone saying that toupper is to be used "to save space" is not saying it as (s)he should. What (s)he should say is "you have to use the proper function becouse everything else is flat out broken and if you have come so far you're really better take a step back and rethink everything you have done so far"!
[/list]
Original OP question is: why my string comparisons do fail? Because of operator precedence and syntax rules.
But the problem he is having, behind the scenes is he does not understand the basic of string comparisons in this specific context (as much as having two variables [font=courier new,courier,monospace]varx[/font] and [font=courier new,courier,monospace]VARX [/font]might make sense, I hardly believe the difference between [i]Person McSurname[/i] and [i]Person MCSurname[/i] is meaningful in most cases).
All posts regarding permutations are [b]madness[/b]. And should be eradicated. Some people reading this might actually believe working with the permutation is somewhat ... maybe encouraged? Think of the children!

If a string is so important permutations have to be checked... it probably is not a string!

I don't know if I'm being able to explain myself.
2

##### Share on other sites
I think I should point out that neither of King Mir's snippets is valid: You can't use switch' on strings, and you can't have non-constant values in the case clauses.

You can use a hash map (unordered_map') to convert a string into an enum and then switch on the enum. In one case I used a [url="http://en.wikipedia.org/wiki/Trie"]trie[/url] instead of a hash map because speed was critical, but you generally won't need that. Edited by alvaro
0

##### Share on other sites
[quote name='Krohm' timestamp='1342518586' post='4959942']...
...

I don't know if I'm being able to explain myself.
[/quote]

I think I get what you're saying, and those are some good points. Let me try to summarize what I [i]think [/i]you are saying:
[b]1)[/b] For comparisons, strings are not what should be used - if you are using a string to frequently compare to other values, it shouldn't be a string.
[b]2)[/b] If you [i]have[/i] to compare strings (because they come from user input - like parsing files) they should be sanitized when received, not when compared.

Am I missing anything? Edited by Servant of the Lord
1

##### Share on other sites
[quote name='alvaro' timestamp='1342527022' post='4959977']
I think I should point out that neither of King Mir's snippets is valid: You can't use switch' on strings, and you can't have non-constant values in the case clauses.
[/quote]
I don't know if you saw this. If you did, sorry for being redundant. That's what Servant of the Lord pointed out, but King Mir was responding to kunos's question if any languages allowed the syntax he quoted. Since then, the thread kind of derailed into non-C++ land. Yes, it's worth pointing out that this can't be done in C++, but it's also worth pointing out that this can be done in some other languages.

@Servant of the Lord: I wasn't saying you implied all switches were bad. The primary point of my post was the operator mix up The other points were just small addendum; I didn't think you'd write such an epic reply ha. I think I'm more used to seeing switch statements used in ways similar to Duff's Device, though. That's probably due to my work in C with FFmpeg, where things like that are common. Whether it's clear or not to someone I think depends on both the person and the situation. And whether or not code like that is a "good idea" is something else too
2

##### Share on other sites
@Cornstalks: I lack succinctness. [img]http://public.gamedev.net//public/style_emoticons/default/happy.png[/img]
My posts are usually too repetitive, reiterating previous points multiple times. [img]http://public.gamedev.net//public/style_emoticons/default/dry.png[/img]
0

##### Share on other sites
[quote name='alvaro' timestamp='1342527022' post='4959977']
I think I should point out that neither of King Mir's snippets is valid: You can't use switch' on strings, and you can't have non-constant values in the case clauses.

You can use a hash map (`unordered_map') to convert a string into an enum and then switch on the enum. In one case I used a [url="http://en.wikipedia.org/wiki/Trie"]trie[/url] instead of a hash map because speed was critical, but you generally won't need that.
[/quote]The examples were not C++. It was a divergent discussion on the elegance of using switch statements to compare a value to series of literals when there are only two possible branch paths. In fact the same question arises when using int, so the discussion is relevant to C++, though the examples aren't.
0

##### Share on other sites
[quote name='Servant of the Lord' timestamp='1342548582' post='4960092']
I think I get what you're saying, and those are some good points. Let me try to summarize what I [i]think [/i]you are saying:
[b]1)[/b] For comparisons, strings are not what should be used - if you are using a string to frequently compare to other values, it shouldn't be a string.
[b]2)[/b] If you [i]have[/i] to compare strings (because they come from user input - like parsing files) they should be sanitized when received, not when compared.

Am I missing anything?[/quote]Almost there.
[b]3)[/b] If you do not sanitize and need to work on strings, at least make sure you're understanding the real meaning of the data you're looking at. Compare them case-insensitive [u]for this specific case[/u] using [font=courier new,courier,monospace]toupper[/font].
0

##### Share on other sites
[quote name='Krohm' timestamp='1342594038' post='4960351']
[quote name='Servant of the Lord' timestamp='1342548582' post='4960092']
I think I get what you're saying, and those are some good points. Let me try to summarize what I [i]think [/i]you are saying:
[b]1)[/b] For comparisons, strings are not what should be used - if you are using a string to frequently compare to other values, it shouldn't be a string.
[b]2)[/b] If you [i]have[/i] to compare strings (because they come from user input - like parsing files) they should be sanitized when received, not when compared.

Am I missing anything?[/quote]Almost there.
[b]3)[/b] If you do not sanitize and need to work on strings, at least make sure you're understanding the real meaning of the data you're looking at. Compare them case-insensitive [u]for this specific case[/u] using [font=courier new,courier,monospace]toupper[/font].
[/quote]

About point one. So if I can't use username in the database and username given by the user as a input to compare if the user gave the correct input then what the fuck I should compare? Better example yet is searching for a username in the database without knowing the users id. String comparisons happen every day everywhere, so why they should not be used? Even when you log in to gamedev.net. We would not have compilers if we would not compare strings... Edited by TMKCodes
0

##### Share on other sites
[quote name='Aardvajk' timestamp='1342442875' post='4959574']"Saves space" is pushing it though, overhead of calling method is likely to outweight space saving but of course this is irrelevant and space saving is hardly the reason to use this approach.[/quote]

I did have this in the back of my mind by I was, for some bizarre reason I can't currently recall, thinking more in terms of space in the actual source file. Having 30 lines to catch a single string is pretty ugly...

It's also more human-friendly. It allows the program to recognise a string however someone wants to write it.
0

##### Share on other sites
you can use the string::find() member function as I do below to check if the string contains "tiffany" which would remove all of the other superfluous checks. You could search for the minimal accepted permutation of the name, even just "tif" (just in case she can't spell her name correctly).

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
int main(void)
{
cout << "Hello \n";
cin.get();
cout << "What is your name? ";
cin.get();

{
cout << "Your name is Tiffany... \n The Creator has a message for you: \n I love you Cupcake";
cin.get();
}
else
{
cout << "Your name is not Tiffany.";
cin.get();
}
}
1

##### Share on other sites
[quote name='kunos' timestamp='1342466480' post='4959691']
[quote name='bls61793' timestamp='1342363560' post='4959267']
[CODE]
if (username == "Tiffany" || "tiffany" || "Tiffany McClure" || "tiffany McClure" || "Tiffany Mcclure" || "tiffany mcclue")

[/CODE]

[/quote]

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?

[/quote]
Regular expressions can match a specified pattern, every language will have at least one implementation of a regex language.

EDIT: Although that's not exactly what you're asking, the effect is to want to test your variable against a range, which for strings is equivalent to a pattern. Edited by Sarmon
0

## Create an account

Register a new account