-4
#include <iostream>
#include <string>
#include <cstring>
using namespace std;

int main(){
    string a="asdasd";
    if(!strchr(a,'a')) cout<<"yes";
    return 0;
} 

I just began to learn C++ programming and I don't know why I got error in this line

if(!strchr(a,'a')) cout<<"yes";

But if I tried to code it like this, it would run very well.

if(!strchr("asdasd",'a')) cout<<"yes";

I know it is a stupid question but I really don't know why.. sorry..

user299560
  • 15
  • 5

2 Answers2

3

The library function strchr is for use with C-style strings, not the C++ string type.

Thomas Padron-McCarthy
  • 27,232
  • 8
  • 51
  • 75
2

When using std::string, the closest equivalent of strchr is find:

#include <iostream>
#include <string>

int main(){
    std::string a="asdasd";
    if(a.find('a') != std::string::npos) std::cout<<"yes";
} 
Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111