0

Possible Duplicate:
How do I properly compare strings in C?

#include <iostream>
using namespace std;

int main(){

    char name[100];
    cout<<"Enter: ";
    cin>>name;
    if(name == "hello"){
        cout<<"Yes it works!";
    }

    return 0;
}

Why when I entered hello in the prompt i didnt got "Yes it works!" message?

2
  • 2
    Because name is not a string, it's a character array.
    – Mr Lister
    Apr 29, 2012 at 14:41
  • 3
    Use std::string! That code is vulnerable to buffer overflows. If you really are using C++, then you sohuld start using STL classes.
    – mfontanini
    Apr 29, 2012 at 14:49

4 Answers 4

9

You need to use strcmp to test for equality.

name is an array, not a std::string, and hello is a string literal, i.e. a const char*. You're comparing pointers, not strings.

4
  • Keep in mind strcmp returns 0 if they're equal, so don't compare using if (strcmp (name, "hello"))
    – chris
    Apr 29, 2012 at 14:43
  • Yes. But now we should tell the OP that although in C++, name is not a string, it would be a string in C. And the comparison would still fail. How do we do that tactfully?
    – Mr Lister
    Apr 29, 2012 at 14:44
  • @chris all you need is en extra ;else.
    – Mr Lister
    Apr 29, 2012 at 14:45
  • @MrLister, I was referring to the tendency to just use if(), not if (... == 0). This is an easy source of logic errors.
    – chris
    Apr 29, 2012 at 14:46
4

Try this:

#include <string.h>
#include <iostream>
using namespace std;

int main(){

    char name[100];
    cout<<"Enter: ";
    cin>>name;

    if(strcmp(name, "hello") == 0) {
        cout << "Yes it works!"; 
    }

    return 0; 
} 
2
  • 1
    I wouldn't encourage people to use C-style methodology, especially in a situation with no error handling (what happens if someone types in a 100-letter input?) Also, I was going to say that if one is going to use legacy C headers it should be #include <cstring> and not #include <string.h>...but in trying to find a reference on that I found it's not as cut-and-dry as I've been led to believe: stackoverflow.com/questions/8380805/… Apr 29, 2012 at 15:14
  • You're right. After reading Bo Perssons answer, I'd rather recommend his solution.
    – Pedro
    Apr 29, 2012 at 15:19
3

If you use std::string instead of a char array, it will work:

#include <iostream>
#include <string>
using namespace std;

int main(){

    string name;
    cout<<"Enter: ";
    cin>>name;
    if(name == "hello"){
        cout<<"Yes it works!";
    }

    return 0;
}
1

There are low-level strings ("C strings") which do not have the high-level behaviors you have probably come to expect from other languages. When you type in a string literal (in "quotes") you are creating one of those types of strings:

http://en.wikipedia.org/wiki/C_string_handling

In C++, one of the first things people do is pass that low-level string to the constructor of std::string to create an instance of a class that has more of the conveniences in interface that you would be used to.

http://www.cplusplus.com/reference/string/string/

Because C++ is layered over a very C-like foundation, it's valuable to understand how C-style strings work. At the same time, a professional/idiomatic C++ program should not use functions like strcmp. For an interesting study into the differences between C style programming and C++ style programming, check this out:

Learning Standard C++ As A New Language (PDF) by Bjarne

Not the answer you're looking for? Browse other questions tagged or ask your own question.