--------------------------------------------------------------------------------

I am trying to do a program that will use a function to search a string for an occurence of another string. Can someone please tell me what is wrong with this?

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

void replaceSubstring(string, string, string);

int main(){
string string1, string2, string3;
cout <<"Substring Program!\n\n\n";
cout << "Please enter string one: \n";
getline (cin, string1);
cout << "Please enter string two: \n";
getline (cin, string2);
cout << "Please enter string three: \n";
getline (cin, string3);
replaceSubstring(string1, string2, string3);
system ("pause");
return 0;
}

void replaceSubstring(string string1, string string2, string string3){
char* a=NULL;
a=strstr(string1, string2);
cout << a;
}

I get this error:
"no matching function for call to `strstr(std::string&, std::string&)' "

You can tell because it is looking for a function that takes two arguments, both (std::string&)s.
However, the documentation for strstr() says that each argument must be a (char *).

Fix it by extracting the (char *)s from the (std::string)s: a=strstr(string1.c_str(), string2.c_str()); There is, BTW, a C++ way to do this: int index = string1.find( string2, 0 ); This way is better (and safer).

Also, don't use system("PAUSE") if you can avoid it. Look here.

Hope this helps.

You can tell because it is looking for a function that takes two arguments, both (std::string&)s.
However, the documentation for strstr() says that each argument must be a (char *).

Fix it by extracting the (char *)s from the (std::string)s: a=strstr(string1.c_str(), string2.c_str()); There is, BTW, a C++ way to do this: int index = string1.find( string2, 0 ); This way is better (and safer).

Also, don't use system("PAUSE") if you can avoid it. Look here.

Hope this helps.

got it! thanks a lot for your help.

This article has been dead for over six months. Start a new discussion instead.