I have a program that calls a function to generate an array of structures( data_set ). The structures consist of a char[20] and 2 int data types. Now what I'm trying to do is randomly pick from an enumerated data type and here is the code I have with a comment where my problem is:

// enumerated data type
enum datalist
{
   complete,
   considering,
   active,
   // and 27 others but you get the point
};

// structures
struct data_set
{
   char status[20];
   int priority;
   int complexity;
};

// declarations
enum datalist status_type,*ptrlist;  //  I imagine I'll need a pointer
struct data_set set1[30];

// implementation
srand(time(NULL));  // for random picking from data list
for(int i=0;i<30;i++)
{
   random_number=rand()%30;  // to get a number from 0 to 29
   // this is where I am having trouble, I know the enum datalist counts
   // ...complete as 0, considering as 1, and so on.  However, I've tried
   // ...many ways to take my random_number and make it point to one
   // ...of the enumerated data types, but I usually wind up with a
   // ...conversion error, non lValue, forward declaration, and others.
   set1[i].status=s;  // put randomly picked status_type in struct set1
}

It's probably something simple that I'm not seeing. Hope this code isn't too hard to read, I'm still learning. Oh, and if you see any bad habits or form in this, please let me know.

Thanks,
Auto aka Servo

Recommended Answers

All 4 Replies

Already figured this one out and sent a PM to see if I can have this thread locked or removed.

Already figured this one out and sent a PM to see if I can have this thread locked or removed.

How did you fix it? It would be really nice to have solution here, for posterity's sake. Perhaps you could submit a code snippet?

Sure, I'll post my change when I get home. However, it's more of a workaround, I changed the enum datalist to a string array instead.

I'm still learning so this may not be the optimal way of doing this, but here is my "fix".

// data list of possible states
[B]char[/B] datalist[30][20]=
{
   "complete",
   "considering",
   "active"
   // and 27 others but you get the point
};

// structure of data sets
[B]struct[/B] data_set
{
   [B]char[/B] status[20];
   [B]int[/B] priority;
   [B]int[/B] complexity;
};

[B]int[/B] main()
{
   // declarations
   [B]struct[/B] data_set set1[30];
   [B]int[/B] random_number;
   // implementation
   srand(time(NULL));  // for random picking from data list
   [B]for[/B]([B]int[/B] i=0;i<30;i++)
      {
      random_number=rand()%30;  
      strcpy(set1[i].status,&datalist[random_number][20]);
   
      printf("%s\n",set1[i].status);  // temporary printf() to check if it's working okay
      }
   getch(); // temporary halt in program to allow me to look over print out
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.