// Find bug(s) of this program #include #include // Provides size_t #include class Bag { public: // TYPEDEF and MEMBER CONSTANTS enum { CAPACITY = 30 }; // Or static const size_t CAPACITY = 30; typedef int Item; Bag( ) { used = 0; } void insert(const Item& entry); void remove(const Item& target); void operator +=(const Bag& addend); size_t size( ) const { return used; } size_t occurrences(const Item& target) const; friend void operator << (ostream& outs, Bag& b); private: Item data[CAPACITY]; // The array to store items size_t used; // How much of array is used }; void Bag::insert(const Item& entry) { assert(size( ) < CAPACITY); data[used] = entry; used++; } void operator >> (istream& ins, Bag& b) { Bag::Item number; cout << "Enter numbers, a negative number when you are done: "; ins >> number; while (number >= 0) { if (b.size( ) < b.CAPACITY) b.insert(number); else cout << "I have run out of room and can't add that number." << endl; cin >> number; } } void operator << (ostream& outs, Bag& b) { outs << "( "; for(int i = 0; i < b.used; i++) outs << b.data[i] << " "; outs << ")" << endl; } void Bag::remove(const Item& target) { size_t index; // If target is not in the array, then index will be set equal to used. for (size_t index = 0; (index < used) && (data[index] != target); index++); if (index == used) // target isn't in the Bag, so no work to do return; // When execution reaches here, target is in the Bag at data[index]. // So, reduce used by 1 and copy the last item onto data[index]. used--; data[index] = data[used]; } size_t Bag::occurrences(const Item target) const { } void main( ) { Bag b1; int number; cout << "Enter numbers, a negative number when you are done: "; cin >> number; while (number >= 0) { if (b1.size( ) < b1.CAPACITY) b1.insert(number); else cout << "I have run out of room and can't add that number." << endl; cin >> number; } cout << b1; cout << "Enter a number to be removed from the bag:" << endl; while (b1.size( ) > 0) { cin >> number; if (b1.occurrences(number) == 0) cout << "No, that number does not occur!" << endl; else { cout << "Yes, I've got that number and will remove it." << endl; b1.remove(number); cout << b1; } } cout << "Now the bag is empty"; }