c++ database cant delete data...

anyone help me with the problem??
I think the vector is OK

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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59

struct bio{
    string name;
    string job;
    int age;
    string address;
    char gender;
    string hobby;
};



void delbio()
{
    ofstream fout;
    ifstream fin;
    string kot;
    char a,b;
    fin.open("1.txt");
    fout.open ("1.txt", ofstream::out | std::ofstream::app);
    bio arrays;
    arrays.name="hack";
/*part that gets record from another file*/
     while (arrays.name.length()>0)
        {
            getline (fin,arrays.name);
            if (arrays.name.length()<=0)
                {
                    break;
                }
            getline (fin,arrays.job);
            fin >> arrays.age;
            fin >> a;
            getline (fin,arrays.address);
            arrays.address=a+arrays.address;
            fin >> arrays.gender;
            fin >> b;
            getline (fin,arrays.hobby);
            arrays.hobby=b+arrays.hobby;
            getline(fin,kot);
            newones.push_back(arrays);


}
 //the part that deletes record(vector)
            string str1;
            cout << "Enter name and surname (Both with first letter in capital)" << endl;
            cin >> str1;
            int x=0;
        while (x<newones.size())
        {
            if(newones[x].name==str1)
                {
                    newones.erase(x,1); /*here my compiler says it is an error "no matching function for call to std::vector<bio>.erase(int&,int)"*/
                }
            cin.get();
            x++;
        }
}
Last edited on
The erase function expects an iterator to the element that you want to delete. You can use the begin function to get an iterator to the first element and then add x to that iterator to get an iterator to the element at position x.
 
newones.erase(newones.begin() + x);
I want to delete only one record not from begining
peter gave code that only deletes one record
Thanks... But why do I have to use
newones.erase(newones.begin() + x);
and not
newones.erase( x);


@peter
I compiled as you said and It worked but I have that question...
Thanks... But why do I have to use


Because that's the way the function works.

Again: http://www.cplusplus.com/reference/vector/vector/erase/?kw=vector%3A%3Aerase

Erase works on iterators. An index is not an iterator.
Last edited on
Look at the link cire posted and look at this link too
http://www.cplusplus.com/reference/vector/vector/

You should notice the different methods that can be called on a vector. The erase method only has one way it can be called.
Thanks all... I got the idea
Topic archived. No new replies allowed.