I am trying to learn C++ and am trying to figure out how to use strings and if statements together. Here's the code I'm trying to play around with:

#include <iostream.h>

int main()
char * a;
cout << "Do you want to enter?\n"<<endl;
cout << "To enter, type yes. Otherwise, type no. \n"

if(a = "yes")
cout<<"\nYou open the door";

else if (a = "no")
cout<<"\nYou walk away from the door";

Every time I type in no, the statement "You open the door" pops up. Anybody know what I'm doing wrong?


Edited by lightningfire: n/a

6 Years
Discussion Span
Last Post by sftranna

First of all, for any type (string or other), the statement "if ( a = b )" does not check whether a is equal to b, it assigns the value of b to a, and returns the final value a (which is also the value of b). The single = sign is an assignment operator, not a comparison operator. The comparison for equality is ==, i.e. double equal sign.

Second, the strings that you are using are so-called C-strings (kept for legacy support of C code in C++). The proper string to use is the class "std::string" (in the "#include <string>" header. Using these, your code will work (with the double equal sign instead of single equal sign for comparisons).

Third, if you have to use "char *", i.e. C-strings, then there is no equality operator for it, so "a == "yes"" will not work. The proper function to compare two C-strings is strcmp(), which will return 0 if they are equal. Thus:

if( strcmp(a,"yes") == 0 )

Finally, and most importantly, having "char* a;" does not initialize "a". "a" is a pointer to an array of characters (char). By not initializing it, you have a pointer that points nowhere (well it points somewhere, but not somewhere that it should point to, because the memory at that address will be used for something else and using "a" uninitialized will corrupt your program). So, you need to initialize it by giving some space for it. Since you are beginning to learn, I'm not sure how much I should or could explain, so I will just say that you should replace line 5 by this:

char a[256]; //this statically allocates 256 bytes which will be big enough to store the user input, presumably.

But, frankly, using std::string is highly recommended here.


Well, a couple of things. First off, you're never allocating any room for the string a. You're just declaring a pointer for it; when the user enters data, it has nowhere to go. You probably want to do something like

char a[32];

Or something.

Second, you are using '=' instead of '=='. '=' does assignment and returns whatever it assigned; c++ will interpret any non-null thing as "true" in an if-statement, and therefore since "yes" is not evaluating to null, '=' is returning a non-null thing and if sees that as true.

However, when comparing strings, you want to use the strcmp() function. I recommend reading up on that; it's easy to use. There may be other ways to get the same effect in c++; look into the string.h library.

I hope you found this helpful. Please don't hesitate to ask me or others any more well-worded, intelligent questions. Thanks for posting your code, by the way.


Agree with the above, just thought I'd add since no one else has mentioned that iostream.h is deprecated and you should ideally use <iostream> without the .h extension.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.