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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
//Bag.h
#include<iostream>
#include<vector>
#include<string>
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<Bag> 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<Bag> 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
myVector[z].get_item == "bread"

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.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//Bag.h
#include<iostream>
#include<vector>
#include<string>
using namespace std;

class Bag
{   vector<string>     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.