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)
-
dvdproject.zip (780.52K)
Number of downloads: 36

New Topic/Question
Reply




MultiQuote





|