Jump to content
  • Advertisement
Sign in to follow this  
BrandonSnider

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

This topic is 2159 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I don't know how std::string handles this, but you should be able to do [font=courier new,courier,monospace]if(stricmp(username.c_str(), "Tiffany McClure")==0) [/font]to do a case insensitive comparison.
(stricmp measures some kind of "difference" between the strings, so you have to check for == 0)

Share this post


Link to post
Share on other sites
[...] 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

Share this post


Link to post
Share on other sites

[quote name='BinaryPhysics' timestamp='1342395942' post='4959383']
Saves space
How?
[/quote]


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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!