• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
BrandonSnider

(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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


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

[i]transform(username.begin(), username.end(), username.begin(), ::tolower);
if (username.find("tiffany") != username.npos)[/i]
{
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 this post


Link to post
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0