I am not able to understand the error,error: is cannot convert 'std::string {aka std::basic_string<char>}' to 'const char*' for argument '1' to 'int strcmp(const char*, const char*)'

void linear(string s[10],int n,string key)
{
for(int i=0;i<n;i++)
{ string s1=s[i];
cout<<s1<<" "<<key;

		if(strcmp(key,s[i])==0)
		{
		cout<<"element found at "<<i+1<<"th index"<<endl;
		return;
	}
}
cout<<"element not found"<<endl;
return;}

int main()
{ int n;
string key;
string s[10];
cout<<“enter the values of n:”;
cin>>n;
cin.ignore();//if we do not put this line then it would take \n as the imput of first string s[0]
for(int i=0;i<n;i++)
getline(cin,s[i]);
cout<<“enter the key:” ;
getline(cin,key);
linear(s,n,key);
}

1 Like

strcmp() takes input a character array since you are giving a string as an input it is throwing an error you can get around this by, either using character array i.e instead of string key, use char key[N], or if you are using string class you can simply compare them, so instead of if(strcmp(a, b)) we can use if(a == b).
Let me know if you still face some problem.