I have this weird problem.

If my constructor is
[header]

class Book
{
private:
vector<string>Entry;
public:
Book();

[cpp]

Book::Book(){
Entry.push_back("0");
Entry.push_back("1");
Entry.push_back("2");

The weird thing is that it does not update when I do more .push_back.
I use:

void Book::addItem(vector<string>newItem)
{
for(int f=0; f<Entry.size(); f++)
{
Entry.push_back(newItem[f]);
cout<<Entry[f];
}
}

when i display all the items in Entry, only the ones in the constructor is displayed, and not all the additional ones I add through addItem().

I don't know what's wrong...!!!

How to fix???

cout<<Entry[f];

That will only ever print the elements indicated by f. If Entry.size() is optimized away that will only ever be 3 (the original 3 items).

You should be aware that you are modifying the size of teh vector while simultaneously using it's size as a stopping condition. This is probably a bad thing. Perhaps something like:

size_t sz = Entry.size ();
for (size_t i = 0; i < sz; ++i) {
   // modify Entry here
}

would be better.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.