Need help; stuck with this; read file + compare with user input
Hi all,
I'm stuck with another problem.
I'm trying to make a multiple choice quiz.
Each question has 2 answers, one of them is the right one (A or B).
The questions + answers are stored in a .txt file like this;
What is football?;A car;A game;B
Now i want my code to stop showing the line at
What is football?;A car;A game
and let the user input A or B, then compare with the last character of the line in my file, which is ;A or ;B.
Here's my code;
Code:
void spelen(void)
{
static const char filename[] = "vragen.txt"; /* the name of a file to open */
FILE *file = fopen(filename, "r+"); /* try to open the file */
if ( file )
{
char line[BUFSIZ]; /* space to read a line into */
int k = 0;
while ( fgets(line, sizeof line, file) ) /* read each line */
{
int i;
char *token = line; /* point to the beginning of the line */
printf("line %d:\n", ++k);
for ( i = 0; *token; ++i ) /* loop through each character on the line */
{
/* search for delimiters */
size_t len = strcspn(token, ";\n");
/* print the found text: use *.* in format to specify a maximum print size */
printf("[%2d] = \"%*.*s\"\n", i, (int)len, (int)len, token);
/* advance pointer by one more than the length of the found text */
token += len + 1;
}
}
fclose(file);
}
}
and here's my output:
Code:
[0]What is football?
[1]A car
[2]A game
[3]B
Any advice, tips or help with this would be great.
Thanks in advance