### Data Structures Grocery Bag Exercise

Exercise: Imagine that you have just left a store with a bag of groceries. You are concerned that the fragile items will not survive the trip home, so when you reach your car, you place those items into their own bag. If Bag is a class of bags, write C++ statements that remove all the items from store Bag and place them into one of two new bags, as follows: Place all occurrences of bread and eggs into fragile Bag, and all other items into grocery Bag. When you are done, store Bag should be empty. Assume that grocery items are represented by strings.

My Code:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596`` ``````//Bag.h #include #include #include using namespace std; #ifndef BAG_H #define BAG_H class Bag { public: Bag(); Bag(string some); void add_item(string name); string get_item(); private: string item_name; }; #endif // !BAG_H //Bag.cpp #include"Bag.h" Bag::Bag() { } void Bag::add_item(string name) { item_name = name; } string Bag::get_item() { return item_name; } Bag::Bag(string some) { item_name = some; } //Main.cpp #include"Bag.h" int main() { vector myVector; string item; do { cout << "Enter the name of the item or x to stop entering"; cin >> item; if (item != "x") myVector.push_back(Bag(item)); } while (item != "x"); for (int x = 0; x < myVector.size(); ++x) { if (x == 0) { cout << "Stor bag has " << myVector.size() << "items. These are: \n"; } cout << myVector[x].get_item() << endl; } vector fragileBag, groceryBag; for (int z = myVector.size() - 1; z >= 0; --z) { if (myVector[z].get_item() == "eggs" || myVector[z].get_item == "bread") //ERROR APPARENTLY HERE!! { fragileBag.push_back(myVector[z].get_item()); myVector.pop_back(); } else { groceryBag.push_back(myVector[z].get_item()); myVector.pop_back(); } } for (int x = 0; x < fragileBag.size(); ++x) { if (x == 0) { cout << "Fragile bag has " << fragileBag.size() << "items, these are: \n"; } cout << fragileBag[x].get_item() << endl; } for (int a = 0; a < groceryBag.size(); ++a) { if (a == 0) { cout << "Groceries bag has " << groceryBag.size() << "items, these are: \n"; } cout << groceryBag[a].get_item() << endl; } system("pause"); } ``````

Any help is welcome. Thanks in advance
Last edited on
> if (myVector[z].get_item() == "eggs" || myVector[z].get_item == "bread") //ERROR APPARENTLY HERE!!
Compare

myVector[z].get_item() == "eggs"

with

See anything suspicious?

I hate when that happens hahaha, thank you my friend
Last edited on
I think you're misreading the instructions.
 write C++ statements that remove all the items from store Bag and place them into one of two new bags,

I read that as meaning that a Bag is a collection of items. You've created vectors of bags, not collections of items.
 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192`` ``````//Bag.h #include #include #include using namespace std; class Bag { vector items; public: Bag(); void add_item(const string & item); size_t num_items() const; const string & get_item(size_t n); void RemoveLast(); void Print (const string & bagname) const; }; //Bag.cpp // #include "Bag.h" Bag::Bag() {} void Bag::add_item(const string & item) { items.push_back(item); } const string & Bag::get_item(size_t n) { return items[n]; } size_t Bag::num_items() const { return items.size(); } void Bag::RemoveLast() { items.pop_back(); } void Bag::Print(const string & bagname) const { if (items.size() == 0) { cout << bagname << " is empty." << endl; return; } cout << bagname << " has " << items.size() << "items. These are: " << endl; for (size_t x = 0; x < items.size(); ++x) { cout << items[x] << endl; } } //Main.cpp // #include"Bag.h" int main() { Bag store_bag; string item; do { cout << "Enter the name of the item or x to stop entering"; cin >> item; if (item != "x") store_bag.add_item(item); } while (item != "x"); store_bag.Print ("Store bag"); Bag fragileBag; Bag groceryBag; for (size_t z = store_bag.num_items() - 1; z >= 0; --z) { if (store_bag.get_item(z) == "eggs" || store_bag.get_item(z) == "bread") { fragileBag.add_item(store_bag.get_item(z)); store_bag.RemoveLast(); } else { groceryBag.add_item(store_bag.get_item(z)); store_bag.RemoveLast(); } } store_bag.Print ("Store bag"); fragileBag.Print ("Fragile bag"); groceryBag.Print ("Grocery bag"); system("pause"); return 0; }``````
Last edited on
Topic archived. No new replies allowed.