(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

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 trie instead of a hash map because speed was critical, but you generally won't need that.
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.
Advertisement

I think I get what you're saying, and those are some good points. Let me try to summarize what I think you are saying:
1) 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.
2) If you have 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?
Almost there.
3) 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 for this specific case using [font=courier new,courier,monospace]toupper[/font].

Previously "Krohm"


[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 think you are saying:
1) 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.
2) If you have 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?
Almost there.
3) 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 for this specific case 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...
"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.


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.
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)
{
string username;
cout << "Hello \n";
cin.get();
cout << "What is your name? ";
getline (cin, username);
cout << "Your name is: " << username;
cin.get();

transform(username.begin(), username.end(), username.begin(), ::tolower);
if (username.find("tiffany") != username.npos)

{
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();
}
}

[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?

[/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.

This topic is closed to new replies.

Advertisement