I was board so wrote a little program as an example for you.
Code:
#include <stdio.h>
#include <stdlib.h>
#define BUFFSIZE 256
void flush_input()
{
int ch;
while ((ch = getchar()) != '\n' && ch != EOF);
}
int main(int argc, char **argv)
{
FILE *fp;
char buffer[BUFFSIZE];
char answer;
int correct = 0;
int total = 0;
int i = 0;
if ((fp = fopen("questions.txt", "r")) == NULL)
{
perror("Unable to open file");
exit(1);
}
while (!feof(fp))
{
fgets(buffer, BUFFSIZE, fp);
switch (i)
{
/* question number case */
case 0: i++; break; /* can add functionality here if wanted */
case 1: printf("Question: %s", buffer); i++; break;
case 2: printf("Choose one: %s", buffer);
printf("> ");
answer = getchar();
flush_input();
i++;
break;
case 3: if (buffer[0] == answer) /* could also use strcmp and check a full answer */
{
printf("Correct!\n");
correct++;
}
else printf("Incorrect\n");
total++;
i = 0;
}
}
fclose(fp);
printf("Your Score: %d out of %d\n", correct, total);
printf("%f percent\n", ((float)correct / (float)total) * 100);
return 0;
}
File contents:
q=1
This is question 1
a) answer1, b) answer b, c) answer c
c
q=2
This is question 2
a) another a, b) another b, c) another c
b
q=3
Final question
a) choice 1, b) choice 2, c) choice 3
a
Output:
Question: This is question 1
Choose one: a) answer1, b) answer b, c) answer c
> c
Correct!
Question: This is question 2
Choose one: a) another a, b) another b, c) another c
> a
Incorrect
Question: Final question
Choose one: a) choice 1, b) choice 2, c) choice 3
> a
Correct!
Your Score: 2 out of 3
66.666667 percent
so basically you can just keep adding questions to the file and the program doesn't need to change