add to vector

I have read from a file and store the elements to a vector. The problem i am having is adding new element to the vectors. Here is what i have:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

    vector<bank>obj; 

    while(!infile.eof())
     {
                         
        
     }

     cout<<"enter last name"; 
     cin>>l; 
     cout<<"enter  first name"; 
     cin>> f; 
     obj[total].assign(l,f);
     total=toatl++; //I have tried obj.push_back(total) but didn't work. 


if anyone could help me out I will greatly appreciated it.

Last edited on
You have a few errors.
This line is wrong
infile>>l>>f>>;
it should be
infile>>l>>f;
You probably also want to put it inside the loop condition instead like this
while(infile>>l>>f)
Read here for why http://www.parashift.com/c++-faq-lite/input-output.html#faq-15.5

If total is not a bank object you can't add it to the vector.

Is this a mistake? total=toatl++;

vector has the size() function that gives the number of elements in the vector so no need to keep a count outside.
I think you are putting the cart ahead of the horse a bit. Instead of putting a default temp object in the vector and then using your assign() function, try doing it the other way around:

1
2
3
4
5
6
7
while(infile.good())
{
  infile >> l >> f;
  bank temp;
  temp.assign(l,f);
  obj.push_back(temp);
}
how would i do the assign member. I trying doing obj.assign(l,f); but it gave me an error. Also how would i add an element to the vector. I tried doing obj.push_back(temp); but didn't work.
Last edited on
Does bank have an assign function?
I think we need more information if we are going to help you properly. How is the Bank struct/class defined?
here is the class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class bank: 
{
    public:
   //other members; 

    void assign(string, string); 
    
    private: 
               string first, last; 
}

 void Bank::assign(string Last, string First)
{
    last=Last; 
    first=First; 
}

Last edited on
You forgot to specify the type of assign inside the class definition.
oh sorry i have it correctly in code. so how would i exactly access the assign using the vector obj.
Doesn't this work?
1
2
3
4
5
6
while(infile>>l>>f)
{
	bank temp; 
	temp.assign(l,f); 
	obj.push_back(temp);
}
I got it to work thanks for the help.
Topic archived. No new replies allowed.