cannot convert string to char error

iam getting this error for argument 1. error: cannot convert ‘std::string’ to ‘const char*’ for argument ‘1’ to ‘int strcmp(const char*, const char*)
this is where im getting the error

int search_contacts(contact contacts[], char search)

{

cout << "Search contact by LastName:____ ";

cin >> search;

bool found = false;int size=2;

for(int index=0;index<size;index++)

{

if(strcmp(contacts[20].getlname(),search)==0)

{

found = true;

cout<<"Result found "<<endl;

break;

}

}

if(!found)

cout<<"no record found"<<endl;

}

below is most of the code

#include<iostream>

#include<string>

#include<cstdlib>

#include <stdio.h>

#include <string.h>



using namespace std;
class contact{

private:

char fn[20];

char ln[20];

Address address;

string email;

string number;

public:

void input();

void output();

void setfname(char f_n[]);

void setlname(char l_n[]);

void setaddress(Address caddress);

void setemail(string emaila);

void setnumber(string num);

string getfname();

string getlname();

Address getAddress();

string getemail();

string getnumber();

contact();
contact(char f_n[], char l_n[], string emaila,string num);
};
void menu(string opt);

int search_contacts( contact contacts[], char search);
int main(){

//char search;

const int MAX=2;

contact contacts[MAX];
cout<<contacts[20].getlname();

cout << "Enter up to " << MAX << " contacts.\n";

for (int i = 1; i <= MAX; i++)

{

contacts[0].input();

}

contacts[0].output();





return 0;

}
int search_contacts(contact contacts[], char search)

{

cout << "Search contact by LastName:____ ";

cin >> search;

bool found = false;int size=2;

for(int index=0;index<size;index++)

{

if(strcmp(contacts[20].getlname(),search)==0)

{

found = true;

cout<<"Result found "<<endl;
//cout<<"Contact Name: "<<fn<<" "<<ln<<endl;



break;

}

}



if(!found)

cout<<"no record found"<<endl;



}



void menu(string opt){

char search;

contact contacts[0];

const int MAX = 2;

cout<<"Choose(type) a Menu: search | display all(show) | exit'"<<endl;

cin>>opt;

// do this if the option selected is search

if(opt=="search")

{

search_contacts(contacts, search);

}

else if(opt=="show")

{cout<<"asfasfasfafs";

//show_all(contacts, index);

}

else if(opt=="exit")exit(0);
else if((opt!="exit")||(opt!="show")||(opt!="search"))
{
cout<<"Choose an option from the menu";
cin>>opt;
}

}



contact::contact()

{

//fn[20]=""; ln[20]=""; email=""; number="";

}

contact::contact(char f_n[], char l_n[], string emaila,string num)

{

fn[20]= f_n[20]; ln[20]= l_n[20]; email= emaila;number= num;

}

void contact::input()

{
cout<<"fn and ln separate by a space: ";

cin>>fn>>ln;

cout<<"address: ";

address.input();

cout<<"email: ";

cin>>email;

cout<<"phone number: ";

cin>>number;

}

void contact::output()

{
int opt;
char search;

contact contacts[0];

const int MAX = 2;
menu:
cout<<"Choose one of the following Menu to perform a funtion:\n Enter 1 to perform a search\n Enter 2 to show all contacts\n Enter 3 to cancel and exit program \n"<<endl;

cin>>opt;

switch (opt){
case 1: search_contacts(contacts, search);
break;
case 2:cout<<"show all function";
break;
case 3:cout<<"exit";
break;
default: cout<<"Invalid entry, terminating...";

}
}

void contact::setfname(char f_n[])

{

fn[20]= f_n[20];

}

void contact::setlname(char l_n[])

{

ln[20]= l_n[20];

}

//

void contact::setemail(string emaila)

{

email= emaila;

}

void contact::setnumber(string num)

{

number= num;

}

Address contact::getAddress()

{

return address;

}

string contact::getfname()

{

return fn;

}

string contact::getlname()

{

return ln;

}

//

string contact::getemail()

{

return email;

}

string contact::getnumber()

{

return number;

}

c_str() ?? usage : string.c_str() will convert a string
Also, strcmp doesn't take a char.

Did you mean to use a char* for second parameter of search_contacts? That is, to pass a C-style, null-terminated string to strcmp? If so, you can use c_str(), as strongdrink suggested. Or just use string's operator==

Where str is a string

strcmp(str.c_str(), "hello") == 0

and

str == "hello"

do the same thing, as does

str.compare("hello") == 0
Last edited on
Topic archived. No new replies allowed.