using rand with char

I'm trying to use an array of char to randomly generate but running into errors

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

#define TRUE 1
#define FALSE 0

typedef struct{
	int weight;
	char name[40];
	int neutered;
}CAT;

int catName = 0;
char *name[6] = { "Fluffy", "Tigger", "Max", "Betty", "Cat-27", "Jake" };
int *neutered[2] = { TRUE, FALSE };
CAT catList[10];

catName = rand() % *name[6];
	

	catList[0].weight = rand() % 30 + 1;
	strcpy_s(catList[0].name, length, catName);
	catList[0].neutered = rand() % TRUE + FALSE;
Last edited on
catName = name[rand() % 6];
Your code looks like C more than C++. And this is C++ board.
Btw, I'll answer in C.

int catName = 0; >> char catName[SIZE]; //define SIZE 20

int *neutered[2] = { TRUE, FALSE }; >> int neutered[2] = { TRUE, FALSE};

catName = rand() % *name[6]; >> strcpy(catName,name[rand()%6]);

catList[0].neutered = rand() % TRUE + FALSE; >> catList[0].neutered = rand()%2; or catList[0].neutered = neutered[rand()%2];
Last edited on
thank you for the help lsk but it does not all seem to work correctly

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#define TRUE 1
#define FALSE 0
#define SIZE 20

typedef struct{
	int weight;
	char name[40];
	int neutered;
}CAT;

CAT* pMax;
	char *name[6] = { "Fluffy", "Tigger", "Max", "Betty", "Cat-27", "Jake" };
	int neutered[2] = { TRUE, FALSE };
	CAT catList[10];
	char catName[SIZE];

catName[SIZE] = name[rand() % 6];

	catList[0].weight = rand() % 30 + 1;
	strcpy_s(catList[0].name, length, catName);
	catList[0].neutered = neutered[rand() % 2];
Last edited on
catName[SIZE] = name[rand() % 6]; >> strcpy(catName,name[rand() % 6]); !!

So, are your
1
2
3
4
5
catName[SIZE] = name[rand() % 6];

	catList[0].weight = rand() % 30 + 1;
	strcpy_s(catList[0].name, length, catName);
	catList[0].neutered = neutered[rand() % 2];

in main(){...} ?
Topic archived. No new replies allowed.