Ok so my program is to help solve an issue with a game you can play called the murder game. Usually you need someone to organise it for everyone else, but that obviously means they cannot play, so the aim of my program is for the computer to set it up, so that everybody can play.
So currently I am trying to get the user to type in the list of names of people playing, the computer will then randomly order those names into a list. Then it takes a list of weapons, and randomly assigns one to each player, ignoring any excess. Finaly the same for each location.
I have made two linked lists, one just stores a list of words so that as the user types them in it can temporarily store them, and then put them into the main linked list after, as described above.
The problem I have is as soon as the get names function closes i lose my main linked list.
Code:
typedef struct list{
char name[MAXLENGTH];
int ID;
struct list *next;
} List;
typedef struct gamedata{
char name[MAXLENGTH];
char weapon[MAXLENGTH];
char place[MAXLENGTH];
char password[MAXLENGTH];
int kills;
int evasions;
int alive;
int ID;
struct gamedata *next;
} GameData;
void myfgets(char array[MAXLENGTH]); /**this just removes new line character from input**/
void getpeople();
void getweapons();
void waitfunc(double period);
int main(){
srand(time(NULL));
printf("Welcome!\n");
getpeople();
system("pause");
}
void getpeople(){
int nplay, i, j, check, randn;
char line[MAXLENGTH];
struct list *root = NULL;
struct list *p;
struct gamedata *start = NULL;
struct gamedata *pointer;
printf("How many players are there?\n");
fgets(line, MAXLENGTH, stdin);
sscanf(line, "%d", &nplay);
root = malloc(sizeof *root);
if(root==NULL){
fprintf(stderr, "Out of Memory!\n");
exit(1);
}
root->next=NULL;
p = root;
printf("Thank you,");
printf("Please enter the name for player 1\n");
fgets(p->name, MAXLENGTH, stdin);
myfgets(p->name);
for(i=1;i<nplay;i++){
p->next = malloc(sizeof *p);
if(p==NULL){
fprintf(stderr, "Out of Memory!\n");
exit(1);
}
p=p->next;
p->next=NULL;
printf("Please enter the name for player %d\n", i+1);
fgets(p->name, MAXLENGTH, stdin);
myfgets(p->name);
}
p=root;
while(p->next != NULL){
printf("%s\n", p->name);
p = p->next;
}
printf("%s\n", p->name);
start = malloc(sizeof *start);
if(start==NULL){
fprintf(stderr, "Out of Memory!\n");
exit(1);
}
start->next=NULL;
pointer = start;
pointer->next=NULL;
pointer->alive = 0;
pointer->evasions = 0;
pointer->kills = 0;
for(i=1;i<nplay;i++){
pointer->next = malloc(sizeof *pointer);
if(pointer==NULL){
fprintf(stderr, "Out of Memory!\n");
exit(1);
}
pointer=pointer->next;
pointer->next=NULL;
pointer->alive = 0;
pointer->evasions = 0;
pointer->kills = 0;
}
p=root;
srand(time(NULL));
for(i=0;i<nplay;i++){
while(1==1){
randn = rand() % nplay;
pointer=start;
for(j=0;j<randn;j++){
pointer=pointer->next;
}
if(pointer->alive==0){
break;
}
}
strcpy(pointer->name, p->name);
pointer->alive=1;
p=p->next;
}
pointer=start;
while(pointer->next != NULL){
printf("%s\n", pointer->name);
pointer = pointer->next;
}
printf("%s\n", pointer->name);
p = root;
while(root != NULL){
root=p->next;
free(p);
p=root;
}
}