Hi,
I was wondering why im losing the information.
I have three functions thus far.
An option processing function, and two vector functions one for reading a text file and the other for adding a user specifcation to the same vector.
Whats happening is that i read the file and save its contents into a vector and then choose the next option to add a specification. To do that i use push_back. I output ( or debug ) to see if i was succesful. yes. So i choose the option that reads the file again, and im back to where i started. The users spec was lost. And i think its because im allocating it every time i enter that option.
Im a begginner so my code is not up to most programming standards.
Heres my first function which isolates feilds delimted by commas, saved into structure member varaibles, then saved into a vector as elements for each line in the file.
vector<sStruct> * loadFile(char *myTextFile)
{
myStruct
sStruct;
vector<myStruct>
vectorAddress,
*vectorData = new vector<myStruct>
string
feild1, feild2, feild3, feild4;
ifstream
*inFile = new ifstream;
inFile->open( myTextFile, ios::in );
if ( !inFile->good() )
{
cout << "? File Doesnt Exist! " << endl;
}
while ( !inFile->eof() )
{
getline( *inFile, feild1, ',' );
sStruct.m_1 = field1;
getline( *inFile, feild2, ',' );
sStruct.m_2 = field2;
getline( *inFile, field3, ',' );
sStruct.m_3; = feild3
getline( *inFile, feild4 );
sStruct.m_4 = feield4;
vectorData->push_back( sStruct );
}
inFile->clear();
inFile->close();
cout << vectorData->size();
delete inFile; // allocated obj delete to fast why bother?
return vectorData;
}
This function is successful in adding another element into the vector.
vector<sStruct> * addElement(vector<sStruct> *vAddElement)
{
sStruct addElement; // referring to the same struct.
cout << "Enter a String: ";
cin >> addElement.feild1
vAddElement->push_back( addElement );
cout << vAddElement->size() << endl;
return vAddElement;
}
When im in the first function, i debug my vector object, and the data from the file is saved. ok. So i go to the next function and add a string to the struct member that has the first feild. Hopefully not overwritting anything. I debug to make sure and nope, its all good, push_back works nice. But, when i go to my first function. Everythingn is back as it was when is started.
I know its because im reading the file there, and allocating each time i enter that function. Is there a way to prevent this?