### Why doesn't the spare vector copy into the existing vector?

Why doesn't the spare vector copy into the existing vector on line 234?
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253`` ``````#include #include #include #include #include #include #include using namespace std; 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; } const unsigned numberOfPeople = 3; int main() { vector people(numberOfPeople); for (vector::size_type i = 0; i!=numberOfPeople; ++i) { cout <<"Person# " <::size_type i = 0; i!=numberOfPeople; ++i) { cout <> 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 ; 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); cout << "Printing out vector....\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 << "Adding several elements to the existing vector.\n"; cout << "How many elements would you like to add?\n"; int numberOfExtraPeople; cin>>numberOfExtraPeople; cin.ignore(); vector spareVector(numberOfExtraPeople); for (vector::size_type i = 0; i!=numberOfExtraPeople; ++i) { cout <<"Person# " <
> cout <<"Copying spare vector to existing vector...\n";
Because you're pushing INTO spareVector, not into people.

Oh, and 250 lines of badly indented code is no fun to read.

No to mention, a main() well over 100 lines long.

Create some functions to separate the work-flow into easily recognisable and manageable steps.
 ``123`` ``````cout <<"Copying spare vector to existing vector...\n"; for (int i=0; i
that reads: traverse 'people' and put its elements onto `spareVector'
you've got it backwards
`people.insert(people.end(), spareVector.begin(), spareVector.end());`
Why isn't this having any effect even though it compiles

int eraseElement()
{
int position;
cout <<"What position would you like to erase?\n";
cin>>position;
cout <<"Erasing position" <<position<< "\n";
people.erase (people.begin()+(position+1));
}

It doesn't work 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. position is an int.

https://en.cppreference.com/w/cpp/container/vector/erase

Stop using the wrong data type and erase will work for you.

Last edited on
This is valid:
`people.erase( people.begin() + position + 1 );`
because the `people.begin() + position` is an `iterator + integer` that returns an `iterator`.

If that were "supplying the wrong data type for the method", then it would be a syntax error at compile-time.

However,
 ``123456`` ``````int eraseElement() { int position; cin>>position; people.erase( people.begin()+(position+1) ); }``````

Should get a warning, because it does not return any integer despite promising to do so.

Furthermore:
* The input could fail. (What integer value is "Dolly"?)
* There is no safety check that `-2 < position < people.size()-1`
* The logic of showing "Erasing position 41" when you actually erase position 42 is atrocious.

We don't know where you do use that function nor how you test it.
I misread the parameter, I screwed up. I focused on the position+1.

Which as you point out is still wrong. Thanks for giving me a Gibbs slap, it was needed.