Daniel B.
Daniel B.

Reputation: 1273

Why isn't array filling?

I'm trying to fill two arrays with an fstream. One is string and one is int. The string array (name) populates fine, but the char array only populates the first value.

void fillInventory(ifstream &fin, int costArray[],string itemArray[])
{
    string name = "junk";
    string cost;
    int i = 0;
    int max = 0;
    stringstream convert;

    while(name != "none")
    {
        getline(fin, name);
        getline(fin, cost);
        if(name != "none")
        {
            itemArray[i] = name;
            convert<<cost;
            convert >> costArray[i];
        }
        i++;
    }
}

Am I using stringstream wrong or is my logic off, or something else altogether?

Upvotes: 1

Views: 90

Answers (1)

Qaz
Qaz

Reputation: 61970

When you do this:

convert >> costArray[i];

You've reached the EOF on the stringstream, which sets the eofbit flag, causing future operations to fail. Reset the flags in order to continue:

convert >> costArray[i];
convert.clear();

Upvotes: 2

Related Questions