hi!
I'm trying to read lines from a text file line by line and store it in an array. the txt file has some question in it which will be asked to the player !
I want it to produce the following output:
When the user chooses it moves on to the next question ! it's a quiz i'm kinda of new to the language !
here is the code but i run into errors !! idk why!
I'm trying to read lines from a text file line by line and store it in an array. the txt file has some question in it which will be asked to the player !
Code:
1: När kom potatisen till Europa?;A:1300-talet; B:1500-talet; C:900-talet;D:1700-talet\n rätt svar : B 2: I vilken enhet mats elektrisk spänning ?;A:Ampere;B:Volt;C:Joule;D:Watt\n Rätt svar: A 3: Från vilket land har vi fått lego?;A:Tyskland;B:Australien;C:Japan;D:Danmark\n rätt svar : D
I want it to produce the following output:
Code:
1: När kom potatisen till Europa? A:1300-talet B:1500-talet C:900-talet D:1700-talet
here is the code but i run into errors !! idk why!
Code:
#pragma warning(disable:4996)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct quiz
{
char questions[50];
char* alt[4];
char correctanswer[1];
};
int main(){
struct quiz all_ques[10];
int i = 0;
FILE *haidar;
haidar = fopen("gameee.txt", "r");
char str[500];
char *ptr;
while (fgets(str, 500, haidar)) // read 500 characters
{
ptr = strtok(str, ";"); // split our findings around the " "
strcpy(all_ques[i].questions, ptr); // store the question
ptr = strtok(NULL, ";"); // and keep splitting
all_ques[i].alt[0] = malloc(10);
strcpy(all_ques[i].alt[0], ptr); // store the first option
ptr = strtok(NULL, ";"); // and keep splitting
all_ques[i].alt[1] = malloc(10);
strcpy(all_ques[i].alt[1], ptr); // store the second option
ptr = strtok(NULL, ";"); // and keep splitting
all_ques[i].alt[2] = malloc(10);
strcpy(all_ques[i].alt[2], ptr); // store the third option
ptr = strtok(NULL, ";"); // and keep splitting
all_ques[i].alt[3] = malloc(10);
strcpy(all_ques[i].alt[3], ptr); // store the fourth option
fgets(str, 500, haidar);
strcpy(all_ques[i].correctanswer, str); // store the correct answer
i++;
}
}
Comment