Why doesn't the erase element function work?

I'm trying to write a program to manipulate a vector and save it in a text file. All the functions work except the erase element function, lines 371-377. Why isn't people.erase(position); working?

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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<fstream>
#include<iomanip>
#include<stdlib.h>




using namespace std;




int fillVector();
int sortVector();
int printVector();
int saveVector();
int printBackup();
int loadVector();
int addSingleObject();
int addToVector();
int fillVector();
int eraseElement();



struct addressCard
{
string name;
string street;
string town;
string county;
string postCode;
string country;
int telephoneNumber;
};




// write to an output stream, object into a stream
ostream& operator<< ( ostream& stm, const addressCard& a )
    // write one string per line, end with a blank line
{
    return stm << a.name << '\n' << a.street << '\n' << a.town << '\n' 
<< a.county << '\n' << a.postCode << '\n' << a.country << "\n\n" ;
}

// read from an input stream
istream& operator>> ( istream& stm, addressCard& a )
{
    // read one line per string, ignore blank lines at the beginning

    // read the first line, after skipping over blank lines
    while( getline( stm, a.name ) && a.name.empty() ) ;

    // read the remaining lines
    if( getline( stm, a.street ) && getline( stm, a.town ) 
&& getline( stm, a.county ) && getline( stm, a.postCode ) 
&& getline( stm, a.country ) ) ; // do nothing more
    else a = {} ; // clear all fields in a

    return stm ;
}




bool genericSort(const addressCard &obj1, const addressCard &obj2) {
	if (obj1.name == obj2.name)
		if (obj1.street == obj2.street)
			if (obj1.town == obj2.town)
				if (obj1.county == obj2.county)
					if (obj1.postCode == obj2.postCode)
						return obj1.country < obj2.country;
					else
						return obj1.postCode < obj2.postCode; //same as the 
//correspondent if condition, replace == with <
				else
					return obj1.county < obj2.county;
			else
				return obj1.town < obj2.town;
		else
			return obj1.street < obj2.street;
	else
		return obj1.name < obj2.name;

}


enum BOOL { FALSE, TRUE };
int numberOfExtraPeople;
const unsigned numberOfPeople = 3;
string file_name = "data.txt";
vector<addressCard> people(numberOfPeople);
vector<addressCard> spareVector(numberOfExtraPeople); 




int main()
{
char choice;

BOOL exit = FALSE;
for (;;)
{
cout <<"(1) Fill vector (2) Sort vector (3) Print vector (4) Save file\n";
cout <<"(5) Print file (6) Load file (7) Add single object\n";
cout <<"(8) Add multiple objects (9) Erase element (0) Exit\n";
cin >> choice;


switch(choice)
{
case '0' :
exit=TRUE;
break;
case '1' : 
fillVector();
break;
case '2' :
sortVector();
break;
case '3' :
printVector();
break;
case '4' :
saveVector();
break;
case '5' :
printBackup();
break;
case '6' :
loadVector();
break;
case '7' :
addSingleObject();
break;
case '8' :
addToVector();
break;
case '9' :
eraseElement();
break;
default :
cout <<"Please select again.\n";
break;
} // end switch

if(exit)
break;
}

}




int fillVector()
{

cin.ignore();

for (vector<addressCard>::size_type i = 0; i!=numberOfPeople; ++i)
{
cout <<"Person# " <<i+1<< " name: " << endl;
getline(cin,people[i].name);

cout <<"Person# " <<i+1<< " street: " << endl;
getline(cin,people[i].street);

cout <<"Person# " <<i+1<< " town: " << endl;
getline(cin,people[i].town);

cout <<"Person# " <<i+1<< " county: " << endl;
getline(cin,people[i].county);

cout <<"Person# " <<i+1<< " postCode: " << endl;
getline(cin,people[i].postCode);

cout <<"Person# " <<i+1<< " country: " << endl;
getline(cin,people[i].country);

}

cout <<"\n\n";
}





int sortVector()
{

cout <<"Sorting....\n";

sort(people.begin(), people.end(), genericSort);
}





int printVector()
{
cout <<"Printing results....\n";

for (addressCard &n : people)
{
cout << n.name << endl;
cout << n.street << endl;
cout << n.town << endl;
cout << n.county << endl;
cout << n.postCode << endl;
cout << n.country << endl;

cout <<"\n\n";
}

cout <<"Printing raw vector....\n";

for (vector<addressCard>::size_type i = 0; i!=numberOfPeople; ++i)
{
cout <<people[i].name << endl;

cout <<people[i].street << endl;

cout << people[i].town << endl;

cout <<people[i].county << endl;

cout << people[i].postCode << endl;

cout <<people[i].country << endl;

cout <<"\n\n";

}

}





int saveVector()
{


cout <<"Writing to file....\n";

const string file_name = "data.txt" ;

        ofstream ofile(file_name) ;
        for( const addressCard& a : people ) ofile << a ; //vector 
//favourites of class type addressCard, object a iterates vector
// and writes it to text file
ofile.close();

}




int printBackup()
{

cout<<"Writing text file to screen...\n\n";

    string x;
    ifstream inFile;

    inFile.open("data.txt");
    if (!inFile) {
        cout << "Unable to open file";
        return(1); // terminate with error
    }

    while (getline(inFile,x)) {
cout << x << endl ;
}

    inFile.close();
}




int loadVector()
{
cin.ignore();

cout <<"Loading text file into existing vector....\n";

        ifstream ifile(file_name) ; //ifstream reads text file
        addressCard a ; //new object
        people.clear(); //clears existing vector
        cout << people.size() << " size of array after clear\n\n" ;        
        while( ifile >> a ) people.push_back(a) ; //object a reads from text
// file, fills existing vector people
        cout << people.size() << " items were read\n\n" ;

        for( const auto& a : people ) cout << a ;
}




int addSingleObject()
{

cout <<"Adding an element to the vector....\n";

addressCard addedObj;
addedObj = {"Jonathan Zebes","26 Grovsenor St","Chester","Cheshire",
"CH8 9PO","UK"};
people.push_back(addedObj);

}




int addToVector()
{

cout << "Adding several elements to the existing vector.\n";
cout << "How many elements would you like to add?\n";

cin>>numberOfExtraPeople;

cin.ignore();



for (vector<addressCard>::size_type i = 0; i!=numberOfExtraPeople; ++i)
{
cout <<"Person# " <<i+1<< " name: " << endl;
getline(cin,spareVector[i].name);

cout <<"Person# " <<i+1<< " street: " << endl;
getline(cin,spareVector[i].street);

cout <<"Person# " <<i+1<< " town: " << endl;
getline(cin,spareVector[i].town);

cout <<"Person# " <<i+1<< " county: " << endl;
getline(cin,spareVector[i].county);

cout <<"Person# " <<i+1<< " postCode: " << endl;
getline(cin,spareVector[i].postCode);

cout <<"Person# " <<i+1<< " country: " << endl;
getline(cin,spareVector[i].country);

}

cout <<"Copying spare vector to existing vector...\n";  
    for (int i=0; i<spareVector.size(); i++) 
        people.push_back(spareVector[i]); 
}




int eraseElement()
{
int position;
cout <<"What position would you like to erase?\n";
cin>>position;
cout <<"Erasing position" <<position<< "\n"; 	
people.erase(position);
}
Because erase expects an iterator, not an index.

 
people.erase(people.begin() + position);
The vector class erase function doesn't take an int as parameter.

http://www.cplusplus.com/reference/vector/vector/erase/

It takes an iterator.

closed account (E0p9LyTq)
Why isn't people.erase(position); working?

Because you are supplying the wrong data type for the method to work.

erase requires a vector iterator, or a range defined by iterators, to work.

https://en.cppreference.com/w/cpp/container/vector/erase
Topic archived. No new replies allowed.