VECTOR.PUSH_BACK(N) Is deleting previous data

when vector.push_back(N) is used vector[n-1] is deleted.

Page 1 of 1

4 Replies - 539 Views - Last Post: 18 December 2009 - 01:21 PM Rate Topic: -----

#1 SCAIRBORN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-December 09

VECTOR.PUSH_BACK(N) Is deleting previous data

Posted 17 December 2009 - 05:00 AM

Hi, I'm writing a program for a DVD collection to save to a file and be read later.
I have 2 classes, DVD and Collection.
I'm having collection read the DVD data from a file one element at a time separated by white space.
When the designated data is read it is initialized into a dvd and then put into a vector of dvd's for use while the program is open. When the user is done the program will write the vector to the file.

My problem is that all the data goes into the DVD and then a pushback is performed and every thing is fine (using debugger the data is put into the new allocation at the end of the vector). But all previous data in previous allocation is dropped.

here is the simple code for this function:
void Collection::set(vector<dvd>& cVector)
{
	vector<dvd> dummy_vector;
	dummy_vector.reserve(1);
	int j = 0;
		string a,b,c;
		int d,e;
		double f;

  ifstream collectionFile ("stefandvd.txt");
  if (collectionFile.is_open())
  {
	while (! collectionFile.eof() )
	{

		
			collectionFile >> a;
			collectionFile >> b;
			collectionFile >> c;
			collectionFile >> d;
			collectionFile >> e;
			collectionFile >> f;
	
		dvd n(a,b,c,d,e,f);

		/* PROBLEM HERE */
		dummy_vector.push_back(n);	//PROBLEM HERE!!!
		dummy_vector[j]=n;
		cout<<dummy_vector.at(j)<<endl;
		j++;
		/* END PROBLEM */
	}
	cVector = dummy_vector;
	//cout << "The contents of cStefan are:";
	//for (unsigned int i=0; i < cVector.size(); i++)
	//cout << " " << cVector[i];
	//cout << endl;

	collectionFile.close();
  }

  else cout << "Unable to open file";
}



I can post the entire project in here if you would like to run it for yourself to see whats going on.
Thanks for any help/advice... (this has been driving me up the wall for the last month!)

-Stefan

Attached File(s)



Is This A Good Question/Topic? 0
  • +

Replies To: VECTOR.PUSH_BACK(N) Is deleting previous data

#2 EdwinNameless  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 120
  • View blog
  • Posts: 710
  • Joined: 15-October 09

Re: VECTOR.PUSH_BACK(N) Is deleting previous data

Posted 17 December 2009 - 06:00 AM

View PostSCAIRBORN, on 17 Dec, 2009 - 11:00 AM, said:

		dummy_vector.push_back(n)
		dummy_vector[j]=n;



You have to choose, it's either or.

This post has been edited by EdwinNameless: 17 December 2009 - 06:01 AM

Was This Post Helpful? 0
  • +
  • -

#3 SCAIRBORN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-December 09

Re: VECTOR.PUSH_BACK(N) Is deleting previous data

Posted 17 December 2009 - 06:21 AM

View PostEdwinNameless, on 17 Dec, 2009 - 05:00 AM, said:

View PostSCAIRBORN, on 17 Dec, 2009 - 11:00 AM, said:

		dummy_vector.push_back(n)
		dummy_vector[j]=n;



You have to choose, it's either or.


Oddly enough if I take out the dummy_vector[j]=n; then I end up with a completely blank vector. The push_back(n) is only creating new containers but not adding any information to them.

If I take out dummy_vector.push_back(n) then I receive an out of bounds error.

I understand its improper programming, i'm just throwing everything i've got in the vector stl at this thing trying to make it work.
Was This Post Helpful? 0
  • +
  • -

#4 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 855
  • View blog
  • Posts: 2,338
  • Joined: 20-August 07

Re: VECTOR.PUSH_BACK(N) Is deleting previous data

Posted 17 December 2009 - 06:33 AM

Why are you using dummy_vector? Seems as if it would be easier to fill your cVector directly

Also - never use "EOF" in a while loop (read here for the reason why: http://www.dreaminco...topic145699.htm )
    ifstream collectionFile ("stefandvd.txt");
    while ( collectionFile >> a >> b >> c >> d >> e >> f )
        cVector.push_back( dvd(a,b,c,d,e,f) ); 



If cVector is empty after that, then its likely to be a problem with your "stefandvd.txt". Since you haven't shown us what the file contains, we can only assume you have already made sure that >> is OK to read your file (If its not OK, then your vector will remain empty).

This post has been edited by Bench: 17 December 2009 - 07:07 AM

Was This Post Helpful? 1
  • +
  • -

#5 SCAIRBORN  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-December 09

Re: VECTOR.PUSH_BACK(N) Is deleting previous data

Posted 18 December 2009 - 01:21 PM

Turns out the problem was with my dvd class copy constructor.
I know things were redundant, I was just throwing everything at this problem trying to force it to work.

Bench, THANKS for the the EOF help that was another problem I was having!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1