Ok... If you were to use your code it could prove difficult and require using tempfiles or something. If you use my way you can simply add one extra bit of information to the structure.
Example:
Code:
struct question {
char quest [100];
char choice[4][50];
char ans; // that doesn't need to be an array
// char complete[250]; <- what is this even for?
char correct;
};
/* This function will probably need to be modified for your needs. But this is just an example :) */
size_t parse_questions(FILE *input, struct question **pool)
{
size_t cnt = 0, i;
struct question *array;
char buffer[256], *p;
if(!pool || !infile)
return 0;
fseek(input, 0, SEEK_SET);
while(fgets(buffer, sizeof(buffer), input))
++cnt;
array = malloc(sizeof(*array) * cnt);
if(!array)
return 0;
*pool = array;
fseek(input, 0, SEEK_SET);
while(fgets(buffer, sizeof(buffer), input))
{
/* I am not doing a loop here for simplicity's sake if you decide to reformat your
* file as I suggested.
*/
if((p = strtok(buffer, ";")))
strncpy(array->quest, p, sizeof(array->quest));
else
*array->quest = 0;
for(i = 0; i < 4; ++i)
if((p = strtok(NULL, ";")))
strncpy(array->choice[i], p, sizeof(*array->choice));
else
*array->choice[i] = 0;
if((p = strtok(NULL, ";")))
array->ans = *p
else
array->ans = 0;
array->correct = 0;
++array;
}
return cnt;
}
int ask_question(struct question *q)
{
int i;
if(!q)
return 0;
if(q->correct)
return 1;
fputs(q->quest, stdout);
for(i = 0; i < 4; ++i)
fputs(q->choice[i]);
i = getchar();
if(i == q->ans)
q->correct = 1;
return q->correct;
}
int main(void)
{
FILE *file = fopen("whatever.txt", "r");
struct quesiton *q;
size_t i, count, correct;
if(file)
{
count = parse_questions(file, &q);
while(correct < count)
for(i = 0, correct = 0; i < count; ++i)
correct += ask_question(q + i);
free(q);
fclose(file);
}
return 0;
}