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?