Function issues using ADT bag

according to decode, my insert functions in bag do not exist or do not match, I am also getting an error because the function only has one arguement supposedly? I checked against the infor my professor gave the class and it matches what he says it is supposed to be so I am really confused.

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
  #include <iostream>
using namespace std;

class Item
{
    private:
        string name;
        double cost;

    public:
        Item()
        {
            name = "";
            cost = 0.0;
        }

        Item (string newName, double newCost)
        {
            name = newName;
            cost = newCost;
        }

        void show () { cout << "- " << name << " $" << cost << endl; }

        string get_name() { return name; }
        double get_cost() { return cost; }

        void set_item(string newName, double newCost)
        {
            name = newName;
            cost = newCost;
        }

        void setItem(Item item)
        {
            name = item.get_name();
            cost = item.get_cost();
        }
};

class Bag
{
    private:
        Item *items;
        int number_items;
        int max;

    public:
        Bag(int Max)
        {
            items = new Item[Max];
            number_items = 0;
            max = Max;
        }

        void insert_item(string name, double cost)
        {
            items[number_items].set_item(name, cost);
            number_items++;
        }
        void insertItem(Item item)
        {
            items[number_items].set_item(item);
            number_items++;
        }

        int size() {return max;}

        void showItems()
        {
            for (int i = 0; i < number_items; i++)
                items[i].show();
        }

        void delete_first()
        {
            for (int i = 0; i < max - 1; i++)
                items[i] = items[i + 1];
            number_items--;
        }

        bool contains(string name_to_find)
        {
            for (int i = 0; i < number_items; i++)
                if (name_to_find == items[i].get_name())
                    return true;
            return false;
        }

        void sort_by_cost_ascending()
        {
            for (int i = 0; i < number_items - 1; i++)
                for (int j = 0; j < number_items - 1; j++)
                    if (items[j].get_cost() > items[j + 1].get_cost())
                        swap(j, j+1);
        }

        void swap(int index1,int index2)
        {
            Item temp = items[index1];
            items[index1] = items[index2];
            items[index2] = temp;
        }

        void most_expensive()
        {
            int max = 0;
            for (int i = 1; i < number_items; i++)
                if (items[i].get_cost() > items[max].get_cost() )
                    max = i;

             cout << "\nMost expensive is: "
                  << items[max].get_name() << " $"
                  << items[max].get_cost() << endl;
        }

        void show_reverse()
        {
            for (int i = number_items - 1 ; i >= 0; i--)
                items[i].show();
        }

        void get_frequency(Item item)
        {
            int count = 0;
            for (int i = 0; i < number_items; i++)
                if (items[i].get_name() == item.get_name() )
                    count++;

             cout << "\nThere are " << count << " "
                  << item.get_name() << "'s";
        }

        bool delete_item(Item item)
        {
            int index_delete;
            for (index_delete = 0; index_delete < number_items; index_delete++)
                if (items[index_delete].get_name() == item.get_name() )
                    break;

             if (index_delete == number_items)
                 return false;

             for (int i = index_delete; i < number_items - 1; i++)
                 items[i] = items[i + 1];

             number_items--;
             return true;
        }

        int get_index_of(Item item)
        {
            for (int i = 0; i < number_items; i++)
            {
                if (items[i].get_name() == item.get_name())
                    return i;
            }
        }

        double sum_of_all()
        {
            double sum = 0;
            for (int i = 0; i < number_items; i++)
                sum += items[i].get_cost();
            return sum;
        }
};

int main()
{
    cout << "Testing Items: ";
    Item apple("Fuji Apple", 4.99);
    apple.show();

    int max_items_in_bag = 4;
    Bag bag(max_items_in_bag);

    cout << endl << "Added Items to Bag: \n";
    bag.insert_item("Banana", 1.25);
    bag.insertItem(apple);

    Item oranges("Oranges", 5.50);
    bag.insert_item(oranges);
    bag.insert_item("Yogurt, .99");
    bag.insert_item("Milk", 3.99);
    bag.showItems();
    bag.most_expensive();
    bag.get_frequency(apple);

    if (bag.delete_item(apple))
        cout << "\n\n" << apple.get_name() << "deleted. \n";
    else
        cout << "Could not delete item. \n";

    cout << "\nAfter deleting Apple: ";
    bag.showItems();
    cout << "\nThe index of Milk is: " << bag.get_index_of(oranges) << endl;

    cout << "\nShow in reverse: ";
    bag.show_reverse();
}
set_item and setItem are spelled differently.
Same with insert_item and insertItem.
And here you've put both params in a string!
 
 bag.insert_item("Yogurt, .99");
Last edited on
set_item and setItem are spelled differently.
Same with insert_item and insertItem


Im not quite sure what you mean by this, there are supposed to be 2 different ways to insert/set in our program, he wants us to do it by making the itme first and adding it and also in one fell swoop.

Let me know if I am misunderstanding you
They should be spelled the same. That's allowed in C++. They are distinguished by having different arguments. The following compiles but still doesn't work properly. You're trying to insert 5 items but have only requested space for 4.

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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <iostream>
using namespace std;

class Item
{
    private:
        string name;
        double cost;

    public:
        Item()
        {
            cost = 0.0;
        }

        Item (string newName, double newCost)
        {
            name = newName;
            cost = newCost;
        }

        void show () { cout << "- " << name << " $" << cost << endl; }

        string get_name() { return name; }
        double get_cost() { return cost; }

        void set_item(string newName, double newCost)
        {
            name = newName;
            cost = newCost;
        }

        void set_item(Item item)
        {
            name = item.get_name();
            cost = item.get_cost();
        }
};

class Bag
{
    private:
        Item *items;
        int number_items;
        int max;

    public:
        Bag(int Max)
        {
            items = new Item[Max];
            number_items = 0;
            max = Max;
        }

        void insert_item(string name, double cost)
        {
            items[number_items].set_item(name, cost);
            number_items++;
        }
        void insert_item(Item item)
        {
            items[number_items].set_item(item);
            number_items++;
        }

        int size() {return max;}

        void show_items()
        {
            for (int i = 0; i < number_items; i++)
                items[i].show();
        }

        void delete_first()
        {
            for (int i = 0; i < max - 1; i++)
                items[i] = items[i + 1];
            number_items--;
        }

        bool contains(string name_to_find)
        {
            for (int i = 0; i < number_items; i++)
                if (name_to_find == items[i].get_name())
                    return true;
            return false;
        }

        void sort_by_cost_ascending()
        {
            for (int i = 0; i < number_items - 1; i++)
                for (int j = 0; j < number_items - 1; j++)
                    if (items[j].get_cost() > items[j + 1].get_cost())
                        swap(j, j+1);
        }

        void swap(int index1,int index2)
        {
            Item temp = items[index1];
            items[index1] = items[index2];
            items[index2] = temp;
        }

        void most_expensive()
        {
            int max = 0;
            for (int i = 1; i < number_items; i++)
                if (items[i].get_cost() > items[max].get_cost() )
                    max = i;

             cout << "\nMost expensive is: "
                  << items[max].get_name() << " $"
                  << items[max].get_cost() << endl;
        }

        void show_reverse()
        {
            for (int i = number_items - 1 ; i >= 0; i--)
                items[i].show();
        }

        void get_frequency(Item item)
        {
            int count = 0;
            for (int i = 0; i < number_items; i++)
                if (items[i].get_name() == item.get_name() )
                    count++;

             cout << "\nThere are " << count << " "
                  << item.get_name() << "'s";
        }

        bool delete_item(Item item)
        {
            int index_delete;
            for (index_delete = 0; index_delete < number_items; index_delete++)
                if (items[index_delete].get_name() == item.get_name() )
                    break;

             if (index_delete == number_items)
                 return false;

             for (int i = index_delete; i < number_items - 1; i++)
                 items[i] = items[i + 1];

             number_items--;
             return true;
        }

        int get_index_of(Item item)
        {
            for (int i = 0; i < number_items; i++)
            {
                if (items[i].get_name() == item.get_name())
                    return i;
            }
            return -1;  // not found
        }

        double sum_of_all()
        {
            double sum = 0;
            for (int i = 0; i < number_items; i++)
                sum += items[i].get_cost();
            return sum;
        }
};

int main()
{
    cout << "Testing Items: ";
    Item apple("Fuji Apple", 4.99);
    apple.show();

    int max_items_in_bag = 4;
    Bag bag(max_items_in_bag);

    cout << endl << "Added Items to Bag: \n";
    bag.insert_item("Banana", 1.25);
    bag.insert_item(apple);

    Item oranges("Oranges", 5.50);
    bag.insert_item(oranges);
    bag.insert_item("Yogurt", .99);
    bag.insert_item("Milk", 3.99);
    bag.show_items();
    bag.most_expensive();
    bag.get_frequency(apple);

    if (bag.delete_item(apple))
        cout << "\n\n" << apple.get_name() << "deleted. \n";
    else
        cout << "Could not delete item. \n";

    cout << "\nAfter deleting Apple: ";
    bag.show_items();
    cout << "\nThe index of Milk is: " << bag.get_index_of(oranges) << endl;

    cout << "\nShow in reverse: ";
    bag.show_reverse();
}

Last edited on
but wouldnt requesting to index 4 give 5 items since 0 is where the index starts?
Nope.
Well, that will be fun telling the professor that his code to copy doesnt function. fixed those issues, on to the next one.

So there is also a boolean function called contains at line 81.

I am getting these back from Clion debug about that one line of code:

note: no known conversion for argument 1 from 'Item' to 'std::__cxx11::string' {aka 'std::__cxx11::basic_string<char>'}
C:-----------.cpp: In member function 'int Bag::get_index_of(Item)':

note: candidate: 'bool Bag::contains(std::__cxx11::string)'
bool contains(string name_to_find)

note: no known conversion for argument 1 from 'Item' to 'std::__cxx11::string' {aka 'std::__cxx11::basic_string<char>'}

note: no known conversion for argument 1 from 'Item' to 'std::__cxx11::string' {aka 'std::__cxx11::basic_string<char>'}
C:\Users\jakel\CLionProjects\p4.cpp\p4.cpp: In member function 'int Bag::get_index_of(Item)':
Thanks for your help by the way, I wasnt getting any feedback from the professor right now and wont see any classmates until after this is due. Means a lot.
I think you must have changed something. But it looks like in order to call contains you need to pass a string, something like:
 
    contains(item.get_name())

Topic archived. No new replies allowed.