1

Actually I'm making a budget calculating app, where there is a class for the items I buy.

class Item
{
    string name;
    string date;
    int amount;
    float singlePrice;
    float totalPrice;
    // constructor
    Public Item(string name, int amount, float price)
    {
        
    }
};

But I don't want to hard code every Item, I want to add items in the app and save it to file and calculate how much money I have left.

2
  • Choose a collection that can be modified and store the Boughts in there. Some are more suitable depending on how you want to access the data but you've got some options, (vector, unordered_map, ...). Other than the choice of data stucture there's another important choice you need to make: how do you persist the data? Database, file, ...? Commented Aug 22, 2021 at 10:56
  • 1
    Note that your constructor is private here, which probably is not what you want. Commented Aug 22, 2021 at 11:27

1 Answer 1

1

You need to put them in an appropriate data structure which will let you retrieve them by name later on.

Put them in a std::map<string, Bought> if there's going to be just one instance of each, otherwise, in a std::map<string, std::vector<Bought>> if needed; although, your data model really should have a notion of quantity too somewhere - i.e., separate your item catalog from a purchase itself.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.