Jump to content

  • Log In with Google      Sign In   
  • Create Account

Interested in a FREE copy of HTML5 game maker Construct 2?

We'll be giving away three Personal Edition licences in next Tuesday's GDNet Direct email newsletter!

Sign up from the right-hand sidebar on our homepage and read Tuesday's newsletter for details!


We're also offering banner ads on our site from just $5! 1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
35 replies to this topic

#1 bls61793   Members   -  Reputation: 102

Like
-1Likes
Like

Posted 15 July 2012 - 08:46 AM

I've worked with other languages in the past including basic and a lot of scripting languages, and I have worked a decent bit on C++ too. I haven't worked on C++ in quite a while now, and last night I was trying to refresh myself on the basics... so--and this is a little embarrassing--I wrote this small piece of code for a console program to send my girlfriend:

#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
//Vars
string username = "";

//Execute
cout << "Hello \n";
cin.get();
cout << "What is your name? ";
getline (cin, username);
cout << "Your name is: " << username;
cin.get();
if (username == "Tiffany" || "tiffany" || "Tiffany McClure" || "tiffany McClure" || "Tiffany Mcclure" || "tiffany mcclue")
{
  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();
}
return 0;
}

The problem is... it doesn't appear that the "if (username == "Tiffany" || "tiffany" || "Tiffany McClure" || "tiffany McClure" || "Tiffany Mcclure" || "tiffany mcclue")" statement evaluates correctly, as the program always displays the cout message in the "if" block, even if the if statement should be false.


I know these are beginner C++ concepts that have nothing to do with game development, but... this is like the only forum acct. I have for anything like this, and I hate to create another just to ask this somewhat stupid question.

I appreciate any help with this, I'm trying to pick up C++ again so that maybe I can do something useful with it.


EDIT: Oops. I haven't been on this site in a while. Forgot there was a "For Beginners" Section. This probably belongs there. Sorry about that.

Edited by bls61793, 15 July 2012 - 08:51 AM.


Sponsor:

#2 zacaj   Members   -  Reputation: 643

Like
8Likes
Like

Posted 15 July 2012 - 09:05 AM

You need to do
if (username == "Tiffany" || username =="tiffany"

#3 bls61793   Members   -  Reputation: 102

Like
1Likes
Like

Posted 15 July 2012 - 09:14 AM

Thanks... XD I knew I would feel dumb at the end of this one... always the little things.

Appreciate it. Sometimes it's just helpful to have someone else look at it.

#4 3DModelerMan   Members   -  Reputation: 1021

Like
1Likes
Like

Posted 15 July 2012 - 01:52 PM

The || && and ! operators operate on entire logical expressions. username == "Tiffany" is an entire expression, but "tiffany" is just a string literal.I made that mistake too when I first started out.

#5 mind in a box   Members   -  Reputation: 602

Like
0Likes
Like

Posted 15 July 2012 - 02:31 PM

I don't know how std::string handles this, but you should be able to do if(stricmp(username.c_str(), "Tiffany McClure")==0) to do a case insensitive comparison.
(stricmp measures some kind of "difference" between the strings, so you have to check for == 0)

#6 Servant of the Lord   Crossbones+   -  Reputation: 20287

Like
8Likes
Like

Posted 15 July 2012 - 02:51 PM

I usually convert my strings to all lowercase or uppercase, if I'm going to compare them to multiple possibilities.

std::string name = "Person McPerson";
std::transform(name.begin(), name.end(), name.begin(), ::tolower);

if(name == "person mcperson")
{
     //...
}

tolower(), std::transform
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#7 BinaryPhysics   Members   -  Reputation: 294

Like
0Likes
Like

Posted 15 July 2012 - 05:45 PM

[...] convert [...] strings to all lowercase...


I was going to suggest this. This is a great technique. Saves space and it means you catch strange possibilities like "TiffAnY MCClurE"; which would otherwise take up huge amounts of space if you tried to catch every single version.

Edited by BinaryPhysics, 15 July 2012 - 08:37 PM.


#8 Krohm   Crossbones+   -  Reputation: 3167

Like
0Likes
Like

Posted 16 July 2012 - 01:10 AM

Saves space

How?

#9 Aardvajk   Crossbones+   -  Reputation: 6056

Like
0Likes
Like

Posted 16 July 2012 - 06:47 AM


Saves space

How?



Stops the exe having to have all the different supported permutations of the string stored in its data section.

"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.

#10 Krohm   Crossbones+   -  Reputation: 3167

Like
0Likes
Like

Posted 16 July 2012 - 07:21 AM

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.

#11 mark ds   Members   -  Reputation: 1387

Like
0Likes
Like

Posted 16 July 2012 - 09:32 AM

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!

Edited by mark ds, 16 July 2012 - 09:34 AM.


#12 Servant of the Lord   Crossbones+   -  Reputation: 20287

Like
0Likes
Like

Posted 16 July 2012 - 11:14 AM

[Edit:] Double-posted for some reason. Can't seem to find the 'delete post' button.
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#13 Servant of the Lord   Crossbones+   -  Reputation: 20287

Like
0Likes
Like

Posted 16 July 2012 - 11:20 AM

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.
It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#14 mark ds   Members   -  Reputation: 1387

Like
0Likes
Like

Posted 16 July 2012 - 01:06 PM

2^14 permutations * 16 bytes (assuming ascii, 14 characters + a space + a null) = 256kb!

#15 kunos   Crossbones+   -  Reputation: 2207

Like
0Likes
Like

Posted 16 July 2012 - 01:21 PM

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
Lead Programmer
TWITTER: @KunosStefano
AssettoCorsa - netKar PRO - Kunos Simulazioni

#16 King Mir   Members   -  Reputation: 2031

Like
0Likes
Like

Posted 16 July 2012 - 02:34 PM


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" ? Posted Image You'll miss that.

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

#17 Servant of the Lord   Crossbones+   -  Reputation: 20287

Like
2Likes
Like

Posted 16 July 2012 - 02:51 PM

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. Posted Image

Edited by Servant of the Lord, 16 July 2012 - 02:52 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]


#18 Cornstalks   Crossbones+   -  Reputation: 6991

Like
1Likes
Like

Posted 16 July 2012 - 02:58 PM

Am I the only one who noticed the typo (... "tiffany McClure" || "Tiffany Mcclure" || "tiffany mcclue")? 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.
[ 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 ]

#19 King Mir   Members   -  Reputation: 2031

Like
0Likes
Like

Posted 16 July 2012 - 03:34 PM


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. Posted Image

Which is why I said "some languages" and not "c++ and some languages". Why do you think switches are ugly?

#20 Servant of the Lord   Crossbones+   -  Reputation: 20287

Like
0Likes
Like

Posted 16 July 2012 - 04:37 PM


C++ switch statements only work with some data types (mostly ints) - not with std::strings. Even so, that's pretty ugly syntax. Posted Image

Which is why I said "some languages" and not "c++ and some languages"

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?

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. Posted Image

Edited by Servant of the Lord, 16 July 2012 - 04:39 PM.

It's perfectly fine to abbreviate my username to 'Servant' rather than copy+pasting it all the time.
All glory be to the Man at the right hand... On David's throne the King will reign, and the Government will rest upon His shoulders. All the earth will see the salvation of God.
Of Stranger Flames - [indie turn-based rpg set in a para-historical French colony] | Indie RPG development journal

[Fly with me on Twitter] [Google+] [My broken website]

[Need web hosting? I personally like A Small Orange]





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS