Witam.
Piszę grę quiz. Napotkałem problem, który objawia się tym, że przy tworzeniu więcej niż jednego elementu listy, następują błędne przypisania danych. Przy jednym (pierwszym) elemencie wszystko jest w porządku. Poniżej podaję kod.
Pozdrawiam
struct question
{
char question [250];
char answer1 [50];
char answer2 [50];
char answer3 [50];
char answer4 [50];
char correct_answer [2];
int id;
struct question *next;
};
struct question *load_questions (struct question *list_pointer, char question [250], char answer1 [50], char answer2 [50], char answer3 [50], char answer4 [50], char correct [2], int id)
{
struct question *new_node = (struct question *) malloc (sizeof(struct question));
if (new_node){
if (list_pointer == NULL){
new_node->next = new_node;
}else{
new_node->next = list_pointer->next;
list_pointer->next = new_node;
}
strcpy (new_node->question, question);
strcpy (new_node->answer1, answer1);
strcpy (new_node->answer2, answer2);
strcpy (new_node->answer3, answer3);
strcpy (new_node->answer4, answer4);
strcpy (new_node->correct_answer, correct);
new_node->id = id;
}
return new_node;
}
int main()
{
char nick [11] = {0};
int tmp;
//----------WPROWADZANIE GRACZY -------------------------------------
puts ("Ilu graczy chcesz wprowadzic?");
scanf ("%d", &tmp);
int i;
struct player *players_pointer = NULL;
for(i=0; i<tmp; i++){
printf ("Wprowadz nick gracza %d: ", i);
scanf("%s", nick);
players_pointer = add_players (players_pointer, nick, i);
}
players_pointer = find_player (players_pointer, 0);
//-----------------------------------------------------
FILE *questions;
struct question *questions_pointer = NULL;
if ((questions = fopen ("pytania.txt", "r"))){
char question_tmp [250] = {'\0'};
char answer1_tmp [50] = {'\0'};
char answer2_tmp [50] = {'\0'};
char answer3_tmp [50] = {'\0'};
char answer4_tmp [50] = {'\0'};
char correct_tmp [2] = {'\0'};
for (i=0; i<2; i++){
fgets( question_tmp, 250 , questions );
fgets( answer1_tmp, 50 , questions );
fgets( answer2_tmp, 50 , questions );
fgets( answer3_tmp, 50 , questions );
fgets( answer4_tmp, 50 , questions );
fgets( correct_tmp, 2 , questions );
questions_pointer = load_questions(questions_pointer, question_tmp, answer1_tmp, answer2_tmp, answer3_tmp, answer4_tmp, correct_tmp, i );
}
}
questions_pointer = find_question(questions_pointer, 0);
printf("%s", questions_pointer->question);
printf("%s", questions_pointer->next->question);
return 0;
}