0

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???

Edited by nullifyQQ

2
Contributors
1
Reply
2
Views
4 Years
Discussion Span
Last Post by L7Sqr
0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.