I'm trying to get a distinct value from my enumeration using void pointers.
I've an enum declaration of a list of animals
enum Animal {Rat, Ox, Tiger, Rabbit, Dragon, Snake, Horse, Sheep, Monkey, Rooster, Dog, Pig};
So firstly, i have a function that returns me a value from my enumeration randomly
VoidPtr getAnAnimal()
{
VoidPtr anAnimal;
Animal *a = new Animal;
int k = rand() % 12;
*a = static_cast<Animal>(k);
anAnimal = a;
return anAnimal;
}
Then during my construction of array, I've a conditional statement that states if the array has the same value, it is suppose to randomly generate another enum value
void constructSet(VoidPtr* animalArray, int size)
{
for(int i = 0; i < size; i++)
{
animalArray[i] = getAnAnimal();
int k = 0;
while ((k < i) && (animalArray[i] == animalArray[k]))
{
animalArray[i] = getAnAnimal();
k++;
}
}
}
Unfortunately, it still returns me the same enum value despite calling for another value if the array are the same.
enum. Some casting will be needed. And useAnimal*instead ofVoidPtr.delete animalArray[i];before theanimalArray[i] = getAnAnimal()in your while loop. Ownership of the Animals in animalArray is effectively passing to the call ofconstructSet()apparently