please help me revise the function

#include <string>
#include <vector>
#include <iostream>
using namespace std;

int main()
{
string file=$Ilovestl.;
filter_text(file,string, filter);
system("pause");
return 0;


}
void filter_text(vector<string>*word,string filter)
{
string pos;
filter.insert(0,"\"+.()$1");
vector<string>::iterator iter= word->begin();
vector<string>::iterator iter= word->end();
while((pos=*iter.find_first_of(filter,pos))=string::npos)
{
*iter.erase(pos,1);
iter++;

}

}
//I learned it from (C++ primeer, pg 235-237)// but I don't know how to debug the function//this function is asked to erase "+.()$1 from $Ilovestl.//
There are an assortment of errors here. I fixed some of them, but also used some guesswork. No guarantee that this is correct.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void filter_text(vector<string>*word, string filter)
{
    int pos = 0;
    filter.insert(0,"\"+.()$1");
    vector<string>::iterator iter  = word->begin();
    vector<string>::iterator iter2 = word->end();

    while (iter != iter2)
    {
        while((pos=iter->find_first_of(filter,pos)) != string::npos)
        {
            iter->erase(pos,1);
        }
        iter++;
        pos = 0;
    }
}


Though I think this would make a bit more sense:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <string>
#include <iostream>
using namespace std;

void filter_text(string & word, string filter);

int main()
{
    string line = "$Ilovestl.";
    string filter = "\"+.()$1";

    filter_text(line, filter);

    cout << "line: " << line << endl;

    system("pause");
    return 0;
}

void filter_text(string & word, string filter)
{
    int pos = 0;
    while ((pos=word.find_first_of(filter,pos)) != string::npos)
    {
        word.erase(pos,1);
    }
}
Last edited on
Topic archived. No new replies allowed.