Where did my crabs go?!

This is a discussion on Where did my crabs go?! within the C++ Programming forums, part of the General Programming Boards category; I have long problem with code, it is losing crabs for every bucket filled. How is this happening? Code: #include<iostream> ...

  1. #1
    Registered User
    Join Date
    Mar 2013
    Posts
    1

    Where did my crabs go?!

    I have long problem with code, it is losing crabs for every bucket filled. How is this happening?

    Code:
    #include<iostream>
    #include<string.h>
    using namespace std;
    
    class bucket{
        int capacity;
        
        public:
        int crabs;
        
        bucket()
        {
            crabs = 0;
            capacity = 10;
        }
        
        bool add_crab()
        {
            if(capacity > 0)
            {
                crabs++;
                capacity--;
                return true;
            }
            return false;
        }
        
        bool remove_crab()
        {
            if(crabs > 0)    
            {
                crabs--;
                capacity++;    
                return true;
            }
            return false;
        }
    };
    
    class collection{
        bucket *arr;    
        int count;
        public:
        
        collection()
        {
            
            arr = new bucket();
            count = 1;
        }
        
        void add_crab()
        {
            int i;
            for(i=0; i<count; i++)
            {
                if(arr[i].add_crab())
                {
                    break;
                }                            
            }
            
            if(i == count)
            {
                bucket *temp = new bucket[count+1];    
                
                int j;
                for(j = 0; j<count; j++)
                {
                    while(arr[j].remove_crab())    
                    {
                        temp[j].add_crab();            
                    }
                }
                
                delete arr;
                arr = temp;
                count++; 
            }
            
        }
        
        void print()
        {
            for(int i=0; i<count; i++)
            {
                cout<<"bucket "<<i<<" has "<<arr[i].crabs<<" crabs"<<endl;    
            }
        }
    };
    
    int main()
    {
        collection col;
        int n;
        char x;
        cin>>n;
        for(int i=0; i<n; i++)
        {
            col.add_crab();
        }    
        col.print();
        
        system("pause");
    }
    //Who ate the crabs?

  2. #2
    DRK
    DRK is offline
    Registered User
    Join Date
    Jun 2009
    Posts
    111
    1. When all buckets are full you're moving crabs to larger bucket array forgetting to add a new crab when done.

    2. You're misusing new new[] delete operators:
    Code:
    arr = new bucket();
    // ...
    bucket *temp = new bucket[count+1]; 
    // ...
    delete arr;
    arr = temp;
    3. To correctly call system() function an appropriate header file must be included.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21