12

Hi can any one tell what wrong with this code ?.

 string s=getString(); //return string

    if(!strcmp(s,"STRING")){
         //Do something
    }

while compiling I am getting the error like

error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int strcmp(const char*, const char*)’|
Jeyaram
  • 9,158
  • 7
  • 41
  • 63
Haris
  • 13,645
  • 12
  • 90
  • 121
  • 2
    You want `if ( s == "STRING" )`. `strcmp` is the `const char*` version. (You could use `if (!strcmp(s.c_str(), "STRING")`, but don't). – BoBTFish May 29 '13 at 09:42
  • 1
    Did you look at the documentation for `strcmp` to see what arguments it accepts? Why do you even use `strcmp` like that when you already have an `std::string` that defines `operator==`? – Jon May 29 '13 at 09:42
  • 5
    @BoBTFish `(s == "STRING")` in this case. – ForEveR May 29 '13 at 09:43
  • @BoBTFish Yes I want to compare s with "STRING" – Haris May 29 '13 at 09:44
  • possible duplicate of [Convert std::string to const char\* or char\*](http://stackoverflow.com/questions/347949/convert-stdstring-to-const-char-or-char) – Tony Delroy May 29 '13 at 09:46
  • 5
    @Rakkun: that's not good practice... the `std::string` type overloads comparison to a `const char*` to avoid creating a `std::string` temporary as your suggested code does.... – Tony Delroy May 29 '13 at 09:48
  • 4
    The beauty of `C++`: Where `string == "foo"` means exactly what it says. – stefan May 29 '13 at 09:51
  • 1
    The beauty of C: Where string == "foo" means exactly what it means :-) – paxdiablo May 29 '13 at 11:31

5 Answers5

31

strcmp accepts const char* as argument. You can use c_str method:

if(!strcmp(s.c_str(),"STRING"))

Or just use overloaded operator== for std::string:

if(s == "STRING")
awesoon
  • 32,469
  • 11
  • 74
  • 99
  • I am going to use if (s == "STRING") , but instead of this can I use if(getString() =="STRING") – Haris May 29 '13 at 10:05
  • My code is just like this http://pastebin.com/raw.php?i=rDcPApix And if do like this if (s == "STRING") I am getting the result. – Haris May 29 '13 at 10:27
  • Your code works for me. [Example](http://coliru.stacked-crooked.com/view?id=3b624c795e449d52abf0b16ac5711e7f-7f4bc2c90970fdf19c48ebf8cc2bae3f) – awesoon May 29 '13 at 10:35
  • Hi thanks for your response. That was my coding problem, any way I solved it. – Haris May 29 '13 at 10:43
12

You need to use s.c_str() to get the C string version of a std::string, along the lines of:

if (!strcmp (s.c_str(), "STRING")) ...

but I'm not sure why you wouldn't just use:

if (s == "STRING") ...

which is a lot more readable.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
5

You can use the c_str() method on std::string as in the other answers.

You can also just do this:

if (s == "STRING") { ... }

Which is clearer and doesn't pretend that you're writing C.

janm
  • 17,976
  • 1
  • 43
  • 61
1

You must use the c_str() member function of std::string that gives you the underlying char array, if you want to keep the C way of comparing strings.

Otherwise, you should use the operator== which can test equality between strings and const char*.

JBL
  • 12,588
  • 4
  • 53
  • 84
1

You must use c_str() and it should solve your problem.

Thibel
  • 277
  • 1
  • 5
  • 13