0

Why is push_back not working as intended? Pretty confused as to why it doesn't work with my current code below

using namespace std;
void addItem(vector<string>& STRING, string item)
{
    STRING.push_back(item);
}

int main()
{
    string item;
    vector<string> STRING(100);
    ifstream myFile;
    myFile.open("food.txt");

    if (myFile.is_open()) //code where I store my data into the array
    {
        int i = 0;
        while (!myFile.eof())
        {
            getline(myFile, STRING[i]);
            i++;
        }
    }
    myFile.close();

    cin >> item;
    addItem(STRING, item);
    int x = 0;
    while(!STRING[x].empty()) //code to print the current array
    {
        cout << STRING[x];
        printf("\n");
        x++;
        return 0;
    }
}

Is there something wrong with how I initialized my array? Because when I used CodeBlocks, there were 0 errors and 0 warnings so I assumed it was fine until I ran it.

ThomasMcLeod
  • 7,026
  • 4
  • 37
  • 71
Lee M
  • 350
  • 4
  • 18
  • 7
    Please provide a [MCVE] that we can reproduce the problem.. – πάντα ῥεῖ Mar 03 '17 at 15:22
  • 1
    if your `printList` is the `while` loop you showed in the minimal code, then that is just plain wrong. Also you never actually check if the length of the vector has changed, so how did you determine it didn't? – UnholySheep Mar 03 '17 at 15:42
  • Refactor your code such that the number 100 is not hard-coded anywhere. Initialise `STRING` as empty and use `push_back` to add to it from the beginning. Rename the variable to `strings`, because `STRING` looks like a macro. Don't use `eof` in the loop condition. And finally, avoid `using namespace std;`. Oh, and consider `std::cout` instead of `printf`. – Christian Hackl Mar 03 '17 at 16:08
  • Read [Why is `iostream::eof` inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong/13536879) – molbdnilo Mar 03 '17 at 17:09

1 Answers1

3

Your code does work. However you specified initial size of vector while creating it. Your vector starts with initial size of 100 elements. With that being said, you are indeed adding new element to the array, however push_back() is putting it right after already existing array - at 100th position.

You can avoid it by using defaul constructor

vector<string> STRING;

Also, I'll paste here my printList function, that will show you what the issue is with:

void printList(vector<string> &STRING)
{
    cout << "size: " << STRING.size() << '\n';
    for (int i = 0; i < STRING.size(); i++)
        cout << i << ":" << STRING[i] << ", ";
    cout << '\n';
}

@Edit: Fixed syntax error (Vector instead of vector). Thank you for pointing it out Christian Hackl!

Kosmo
  • 68
  • 5
  • whoo thanks dude your printList function works and now I see the problem – Lee M Mar 03 '17 at 16:07
  • 1
    There's a typo in your answer where it says `Vector` instead of `vector`. And you should use `'\n'` instead of `std::endl`. – Christian Hackl Mar 03 '17 at 16:11
  • @ChristianHackl Fixed the typo, thank you for pointing it out! Could you elaborate on the '\n' instead of std::endl part a little more? All the difference I know is about std::endl flushing output with every usage (ergo, performance drops if overused). – Kosmo Mar 03 '17 at 16:24
  • 1
    @Kosmo: That's pretty much the reason. It's not necessary to flush all the time after a linebreak. – Christian Hackl Mar 03 '17 at 16:37