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?

Community
  • 1
  • 1
user1341970
  • 449
  • 2
  • 7
  • 15

4 Answers4

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.

Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • Keep in mind `strcmp` returns 0 if they're equal, so don't compare using `if (strcmp (name, "hello"))` – chris Apr 29 '12 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 '12 at 14:44
  • @chris all you need is en extra `;else`. – Mr Lister Apr 29 '12 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 '12 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; 
} 
Pedro
  • 4,100
  • 10
  • 58
  • 96
  • 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 ` and not `#include `...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: http://stackoverflow.com/questions/8380805/difference-between-string-h-and-cstring – HostileFork says dont trust SE Apr 29 '12 at 15:14
  • You're right. After reading Bo Perssons answer, I'd rather recommend his solution. – Pedro Apr 29 '12 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;
}
Bo Persson
  • 90,663
  • 31
  • 146
  • 203
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