string to char*?

Hey everyone this is a portion of a program that i am working on. I used the find_state function earlier in my program to traverse the linked list. I was going to try and use it again to print out all the cities of a particular user entered state. Although, when i try to pass over pickState to find_state it can not be changed to a char*. Is there any way to pass over pickState to that function or should i just write a new function?

void listCityOfState(PtrType head)
{
string pickState;

cout << "Please enter a state: ";
cin >> pickState;

if(strcmp(pickState, find_state(pickState, head) == 0))
{
cout << head -> name;
}
listCityOfState(head);

}

PtrType find_state(char *name, PtrType head)
{

if(head == NULL)
{
return NULL;
}
if(head -> next == NULL)
{
return head;
}
else if(strcmp(name, head -> next -> state) <= 0) return head;
else return find_state(name, head -> next);
}
I don't see why you're using char* at all, it's clumsy and not necessary. For comparisons with a std::string use operator==(), and for conversion (when needed) use c_str():

if(pickState == find_state(pickState.c_str(), head)) // assuming PtrType is a typedef for char*
Passing a char array using string::data() where a C string is expected sounds crash bound. string::data() returns a char array without a null terminator. Functions that deal with C strings use the null terminator to figure out when to stop reading or writing.
Topic archived. No new replies allowed.