ok, couple of things.
replace is a standard string function, so you may have that problem I said where you named something after an existing item. Might rename it to Replace or something.
See if that is what is causing it.
loc does not appear to exist where you use it.
loc--; //does not exist.
cout << content[loc+1] << endl;
std::size_t loc; //now it does, this creates it. but compilers don't read backwards.
are you allowed to use the built in replace? Nevermind, I see that you did.
you are making this way, way too hard.
you can do this in about 10 lines in main.
loop to read a line
check the line for the replace string in a loop, and every time you find it, replace it with a call to replace
cout the modified string
until you can't read any more lines.
that is all you need to do. no extra function, no header file, etc.
I don't like to do this, but its a small, simple program, and a couple of the tricks are not clear to a beginner. I think it does what you need. You can test and debug it if not, its close ;)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
|
int main(int argc, char* argv[])
{
unsigned int i; //or size_t, if you like. Im a bit sloppy at times.
string s;
string av1 = argv[1]; //lets convert the args to strings just to be clean
string av2 = argv[2];
while( std::getline (std::cin,s)) //this WILL stop reading on the
//magic end of file marker on a redirected file to the input stream.
{
do
{
i = s.find(av1);
if(i != string::npos)
s.replace(i,av2.length()-1,av2); //you can use iterators here. I hate iterators.
} while (i != string::npos);
cout << s << endl;
}
return 0;
}
|