Stack Exchange
Vector of Vector with custom class
I'm trying to teach myself C++ and found an example problem online oriented around a soda inventory.  I have two classes 1) inventory and 2) soda.  Soda contains its initial amount (int), name (string), id (string), and quantity (int).  Inventory contains a vector of vectors (of type soda).
Class declaration for inventory:
private:
std::vector< std::vector<Soda> > softDrinks;
public:
Inventory();
void buildInventoryGood();
void processTransactionsGood();
std::string displayReport();

Class declaration for soda:
private:
std::string name;
std::string id;
int quantity;
int startingAmount;
int numberOfTransactions;

public:
Soda();
Soda(std::string sodaName, std::string sodaID, int initialAmount);
int addSoda(std::string id, int amount);
int withdrawSoda(std::string id, int toWithdraw);
std::string Report();
std::string getName();
std::string getID();

I can run buildInventoryGood() and it builds everything fine.  (below is the result if I were to cout a report)
Name    ID      InitialAmount   FinalAmount
coke    123     100             300
pepsi   321     200             200
Shasta  987     300             300

My problem is with processTransactionGood().  I've left some cout debug statements in there to help me figure out what is going on.  
void Inventory::processTransactionsGood()
{
vector<Soda> vecSoda;
string textline;
string name;
string id;
int quantity;
ifstream infile("data6trans.txt");

while (getline(infile, textline))
{
    string space_string;
    std::istringstream text_stream(textline);
    text_stream >> id;
    getline(text_stream, space_string, ' '); // Read characters after number until space.
    text_stream >> quantity;

    for (auto drink : softDrinks) {
        auto it = find_if(drink.begin(), drink.end(), [id](Soda obj) {return obj.getID() == id; });
        if (it == drink.end()) {}
        else {
            auto index = distance(drink.begin(), it);
            cout << id << "  " << quantity << " " << index << "a" << endl;
            drink[index].withdrawSoda(id, quantity);
            cout << drink[index].Report() << endl;
        }
    }
}
}

That last cout statement shows me that a given soda was processed (in this instance decremented by a value of one)
Name    Id      starting        final
Shasta  987     300             299

The final result for Shasta should be 299.
But when I run displayReport() I get a result I wasn't expecting:
string Inventory::displayReport() 
{
string report = "Name\tID\tInitialAmount\tFinalAmount\n";
for (auto item : softDrinks) {
    for (auto drink : item) {
        report += drink.Report();
    }
}
return report;
}

I get:
Name    ID      InitialAmount   FinalAmount
coke    123     100             300
pepsi   321     200             200
Shasta  987     300             300

All that to say: I'm not sure that I'm grasping the practice of a vector of vectors and I'm not setting something the way it should be
Edit: ^ and I don't know what the right way is and am looking for a pointer or reference.

The worst part of all this code is that you would dare leave Shasta capitalized, but not Pepsi nor Coke.

one must first be able to master the ability to add and subtract correctly before they start to do user input validation and string manipulation.

for (auto drink : softDrinks)

makes drink to be a copy of softDrink's element. That's why the modified element isn't stored to softDrinks.
Please use
for (auto &drink : softDrinks)

instead of that to have a reference in hand.

AndreyS thanks.  That fixed it perfectly.  I guess I need to re-read about & and *.

You're welcome :) It's a really confusing point in C++11 as we believe 'auto' makes all things right for us - and this is on top of the 'traditional' confusion about copy vs. reference.

@lunchbox, you may find this article I wrote useful. It summarizes auto type deduction rules.
