random data are added to my files

Hello guys,
I'm creating the program which whole purpose is to read data(index, name, height) from the file and then the user has 3 options:
1. write out the data from the file
2. add data at the end of the file
3. delete some data
Here is my program so far:
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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include <iostream>
#include <fstream>
#include <vector>
#include <string>
using namespace std;

struct people {
    int order=0;
    string name;
    int height;
};

//-----------------------------------------------------------------------------

void write_in_file(vector<people>& v)
{
    ofstream file("vysky.txt");
    for (int i=0; i<v.size(); ++i) {
        file << i+1 << "\t" << v[i].name << "\t" << v[i].height;
        if (i!=v.size()-1)
            file << endl;
    }
    file.close();
}

//-----------------------------------------------------------------------------

int main()
{
    people classmates;
    string a;
    int b;
    vector<people>v;
    int selection=1;
    while (selection==1 || selection==2 || selection==3 || selection==4) {
        cout << "1=write out, 2=add name, 3=delete name >";
        cin >> selection;
        switch(selection) {
        case 1: {
            ifstream file("vysky.txt");
            while(!file.eof()) {
                file >> classmates.order >> classmates.name >> classmates.height;
                cout << classmates.order << "\t" << classmates.name << "\t" << classmates.height << endl;
                v.push_back(classmates);
            }
            file.close();
            break;
        }
        case 2: {
            cout << "Enter names and heights, which you want to add: ";
            while (cin >> a >> b) {
                classmates.order=v.size()+1;
                classmates.name=a;
                classmates.height=b;
                v.push_back(classmates);
                write_in_file(v);
                cout << "Another name? (y/n)";
                char decision;
                cin >> decision;
                if (decision=='n')
                    break;
            }
            break;
        }
        case 3: {
            cout << "Enter index of the name which you want to delete: ";
            int elementindex;
            cin >> elementindex;
            v.erase(v.begin()+ elementindex-1);
            write_in_file(v);
            break;
        }
        default: {
            cout << "You ended the program. \n";
            break;
        }
        }
    }
    return 0;
}

My starting file is called vysky(txt file) and looks like this:
1 name1 185
2 name2 165
3 name3 140
4 name4 180
5 name5 180
6 name6 182
7 name7 181
8 name8 160
9 name9 152
When I decide to delete for example name8, it's working completely fine. The problem occurs when I try to delete another name, for example if after name8 I decide to delete name5, this happens:
1 name1 185
2 name2 165
3 name3 140
4 name4 180
5 name6 182
6 name7 181
7 name9 152
8 name1 185
9 name2 165
10 name3 140
11 name4 180
12 name5 180
13 name6 182
14 name7 181
15 name9 152

It's the same for adding the names. I have no idea where the problem is. Do you have any ideas what could be the problem?
PLEASE NOTE that to even start working with the names you first have to write out the names using option number 1, otherwise you won't have data to work with.
Thanks for your answers.
You're appending to the file. You need to truncate it when you open it.
 
std::ofstream file("vysky.txt", std::ios::trunc);

It didn't work for me. Still the same problem.
v.begin()+ elementindex-1
You can't do pointer arithmetic with iterators.

Also, you should be aware of this.
https://en.wikipedia.org/wiki/Erase%E2%80%93remove_idiom

Your erase isn't quite right.

$ ./david73.exe
1=write out, 2=add name, 3=delete name >2
Enter names and heights, which you want to add: throbber 12
Another name? (y/n)n
1=write out, 2=add name, 3=delete name >1
1       throbber        12
1=write out, 2=add name, 3=delete name >2
Enter names and heights, which you want to add: rabbit 8
Another name? (y/n)n
1=write out, 2=add name, 3=delete name >1
1       throbber        12
2       throbber        12
3       rabbit  8
1=write out, 2=add name, 3=delete name >2
Enter names and heights, which you want to add: noveltyitem
24
Another name? (y/n)n
1=write out, 2=add name, 3=delete name >1
1       throbber        12
2       throbber        12
3       rabbit  8
4       throbber        12
5       throbber        12
6       rabbit  8
7       noveltyitem     24
1=write out, 2=add name, 3=delete name >3
Enter index of the name which you want to delete: rabbit
Segmentation fault (core dumped)


It's also duplicating things when adding to the list, not just when deleting it.

Might want to add some sanity checking to the user input in case the user doesn't enter a number :-)

Also, display the list when the program first runs. If I run the program from a blank screen, I get this:


~/src/forums/david73$ ./david73.exe
1=write out, 2=add name, 3=delete name >3
Enter index of the name which you want to delete:


Last edited on
Topic archived. No new replies allowed.