part of the code

string phone_book[11] = {"Becky Warren, 678-1223",
	                         "Joe Looney, 586-0097",
	                         "Geri Palmer, 223-8787",
	                         "Lynn Presnell, 887-1212",
	                         "Holly Gaddis, 223-8878",
	                         "Sam Wiggins, 486-0998",
	                         "Bob Kain, 586-8712",
	                         "Tim Haynes, 586-7676",
	                         "Warren Gaddis, 223-9037",
	                         "Jean James, 678-4939",
                            "Ron Palmer, 486-2783"};
   

   int x, index;
   char lookup[50], *strPtr = NULL;

/********************************** Input / Processing **********************************/



   cout << "Phone Book\n";
   cout << "enter a name: ";
   cin.getline(lookup, 50);
   for (index = 0; index < 11; index++)
   {
      strPtr = strstr(phone_book[index], lookup);
      if (strPtr != NULL)
         break;
   }
   if (strPtr == NULL)
   {
      cout << "no match";
   }
   else
   {
      cout << phone_book[index] << endl;
   }

gives me an error that 2 overlords cant convert all argument types. This happens only when I use array of string objects. When I modify it to 2 dimensional array f chars then the code above works no problem. Whats the problem and how to fix it?
I'm in the process of transition from c to c++ so go easy on me.

Recommended Answers

All 4 Replies

strstr() expects the first parameter to be a char* pointer, not a std::string object. use the c_str() method to get the pointer

strPtr = strstr(phone_book[index].c_str(), lookup);

But you don't need to use strstr() at all because std::string has a lookup function called find(). It returns an int to the beginning of the string or string::npos if not found

std::size_t pos = phone_book[index].find(lookup);
if( pos != string::npos)
{
   // found it
}

I wrote the above from memory so I hope its corect. If not the someone else will correct it.

strstr() does not work on string class, only char* s. Look at the string class methods to find your substitute function/method.

Thanks guys for the input. I think I have to do some reading about the find and other functions for string objects. Will post back and mark as solved if I figure it out.

I got it! Thanks for the help

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.