user40120
user40120

Reputation: 648

Adding element to vector

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?

Upvotes: 0

Views: 1528

Answers (4)

aJ.
aJ.

Reputation: 35450

  1. Make the changes to loadFile() function to take vector object by reference and update the same in loadfile().

Something like this:

bool loadFile(char *myTextFile, vector<sStruct>& vectorData_out)
{
      //Read the file
      //push_back the element into vectorData_out
      //vectorData_out.push_back() ...code to push
}

2.Then change the addElement to accept vectorData_out by reference:

bool addElement(vector<sStruct>& vAddElement_out)
{
      //initilization code for addElement 
     vAddElement_out.push_back( addElement );
     cout << vAddElement->size() << endl;
}

Now your calling code looks line this:

std::vector<sStruct> aVectorData;
loadFile("filename.txt", aVectorData);
addElement(aVectorData);

EDIT: Also, try avoiding allocating on heap unless it is absolutely necessary

Upvotes: 1

dirkgently
dirkgently

Reputation: 111120

Are the user specified fields in a single line or spread across multiple lines?

     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;

The code snippet above reads in four lines. Can you paste the first few lines of your user input file?

Upvotes: 0

Naveen
Naveen

Reputation: 73433

This is because you are creating a new instance of the vector each time you enter loadFile() method. If you want to preserve the contents of the vector, then don't create the vector object inside loadFile. Create it outside this function ( probably from the one which calls loadFile()) and pass it to this function.

Upvotes: 1

DevSolar
DevSolar

Reputation: 70243

Your function addElement() gets a parameter vAddElement, but you are pushing into vectorData...?!?

Upvotes: 1

Related Questions