Printing distinct enum values using voidPtr

I'm trying to figure out how do i print a distinct enum value by using a conditional statement in my construction of array.

I have a enum declaration

 
    enum Animal {Rat, Ox, Tiger, Rabbit, Dragon, Snake, Horse, Sheep, Monkey, Rooster, Dog, Pig};


And i construct my array using a voidptr

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
    void constructSet(VoidPtr* animalArray, int size)
    {
	 int k = 0;  

	   for(int i = 0; i < size; i++)
	   {
		 animalArray[i] = getAnAnimal();

		 while ((k < i) && (animalArray[i] == animalArray[k]))
		 {	 
			
				animalArray[i] = getAnAnimal();   
			
			k++;
		}        
	}

    }

Then i have to use some static cast to revive the value of my enum

1
2
3
4
5
6
7
8
9
10
11
12
13
14
    VoidPtr getAnAnimal()
     {
	VoidPtr anAnimal;
	
	Animal *a = new Animal;
	
	int k = rand() % 12;
	
	*a = static_cast<Animal>(k);
	
	anAnimal = a;
	
	return anAnimal;
    }

Unfortunately, they still return me the same values.
Last edited on
> And i construct my array using a voidptr

Why voidptr?
Use an array of Animal and pass a pointer to Animal. Or use a vector if the size is not known at compile-time.

One way to construct a random set of distinct animals is:
a. shuffle (like shuffling a deck of cards) a sequence containing all animals
b. pick the first size animals from the shuffled sequence.

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
#include <iostream>
#include <vector>
#include <random>
#include <algorithm>

enum Animal { Rat, Ox, Tiger, Rabbit, Dragon, Snake, Horse, Sheep, Monkey, Rooster, Dog, Pig };
constexpr std::size_t NUM_ANIMALS = Pig+1 ;

std::ostream& operator<< ( std::ostream& stm, Animal a )
{
    static const std::string names[NUM_ANIMALS] = { "Rat", "Ox", "Tiger", "Rabbit",
             "Dragon", "Snake", "Horse", "Sheep", "Monkey", "Rooster", "Dog", "Pig" };
    return stm << names[a] ;

}

std::vector<Animal> construct_set( std::size_t sz )
{
    static Animal all_animals[] = { Rat, Ox, Tiger, Rabbit, Dragon, Snake, Horse, Sheep, Monkey, Rooster, Dog, Pig };
    static std::mt19937 rng( std::random_device{}() ) ; // random number generator

    if( sz > NUM_ANIMALS ) sz = NUM_ANIMALS ;
    std::shuffle( all_animals, all_animals+NUM_ANIMALS, rng ) ;
    return { all_animals, all_animals+sz } ;
}

void construct_set( Animal* array, std::size_t sz )
{
    const auto set = construct_set(sz) ;
    std::copy( set.begin(), set.end(), array ) ;
}

int main()
{
    for( int i = 0 ; i < 5 ; ++i )
    {
        std::cout << '#' << i << ". [" ;
        for( Animal a : construct_set(8) ) std::cout << ' ' << a ;
        std::cout << " ]\n" ;
    }

    Animal array[5] {} ;
    construct_set( array, sizeof(array) / sizeof(Animal) ) ;
    std::cout << "\nthe array contains: [ " ;
    for( Animal a : array ) std::cout << a << ' ' ;
    std::cout << "]\n" ;
}

http://coliru.stacked-crooked.com/a/637a51e37d18c43c
Topic archived. No new replies allowed.