Just looking for more experienced programmers opinions on this code. It's a framework for making simple multiple-choice quizzes. I am aware that there is the potential to make the arrays dynamic, but at small sizes this shouldn't matter much.
The formatting of the .txt files used to compose quizzes is simple- start a line with q for question, a for incorrect answer and r for the right answer, meaning almost anyone can easily compose quizzes. As noted in the code, the current form also means that you have to start the quiz with an endline ("enter"), and that long questions should have spaces on all subsequent lines.
#include<stdio.h>
#include<stdlib.h>
///IMPORTANT! Always start the quiz with a new-line, or won't be saved; alternatively I could just add separate code for start of file.
///long questions are problematic; at moment, Get around by insisting on space? could choose to go down the special-character route.
///This version a-b-c labeling has to be done by the quiz author
char text[10000];
int answers[300];
int ansamount[300];
int startpoint[300];
int end;
int convert(char a)
{
switch (a)
{
case 'a':
case 'A':
return 1;
case'b':
case 'B':
return 2;
case 'c':
case 'C':
return 3;
case 'd':
case 'D':
return 4;
case 'e':
case 'E':
return 5;///up to five
default:
return 100;
}
}
int quiz()
{
int print=0,run=1,asked=1,wrong=0;///asked will be adapted if randomisation is added as feature
char choice='e';
while (run==1)
{
while (print<startpoint[asked+1])///+1 as using new questions start point. so question one uses 2's start-point as end
{
printf("%c",text[print]);
print++;
}
choice=getchar();
while (convert(choice)>ansamount[asked])///make sure right
{
choice=getchar();///get rid of endline
printf("Invalid choice. Please choose again\n");
choice=getchar();
}
if (convert(choice)!=answers[asked])///currently no mechanism to stop invalid choice from being 'wrong'. latter add an answer-telling facility
{
printf("You got the answer wrong!\n");
wrong++;
}
choice=getchar();///remove the end-line.
printf("You have got %d out of %d questions right.\n\n",asked-wrong,asked);
asked++;
if (asked==end)
{
return 0;
}
}
}
int load()
{
char filename [25];///can do dynamic, just prefer this for ease-of code for now
int temp=0,position=0,run=1,question=1, answer=1;///starting from question 1, as that's how I feel like doing it.
FILE *fp;
printf("Welcome to Console . Please choose the quiz you wish to take.\n\n");///eventually, add list
while (temp<19)///why stop at new line?
{
filename[temp]=getchar();
if (filename[temp]=='\n')
{
filename[temp]='.';///all files have to be .txt
filename[temp+1]='t';
filename[temp+2]='x';
filename[temp+3]='t';
filename[temp+4]='\0';
break;
}
temp++;
}
fp=fopen(filename,"r");///alternative using %s? check st.ov. again. Add error check
if (fp==NULL)
{
printf("Disaster! The file doesn't open! Please try again, or choose a different file \n");
return 0;
}
while (run==1)
{
text[position]=getc(fp);
if (text[position]==EOF)///loading finished
{
ansamount[question-1]=answer;///for last question
text[position]='\n';///to make sure at least on new line for input, regardless of what quiz-writer did.
end=question;///so that quiz()knows to end.
startpoint[question]=position;///so that can priint till there
return 1;///sucessful load, breaks do-while loop;
}
if (text[position]=='\n')
{
while (text[position]=='\n')///solves multiple spaces.
{
position++;
text[position]=getc(fp);
{
if (text[position]=='Q'||text[position]=='q')///could do switch, choose a 3 way if
{
startpoint[question]=position;///as start from after the 'q'
ansamount[question-1]=answer;///-1 as answer is what last question was up to, as this just start now. also had to add to eof, since only does at start of new question
question++;///so next question is saved in right place. thats it. answer saved should before q-1.
answer=1;///resets for next question
}
else if (text[position]=='a'||text[position]=='A')
{
answer++;///so that we know what number the correct answer is
}
else if (text[position]=='r'||text[position]=='R')/// else to prevent the position -- causing probs, even though end line does that.
{
answers [question-1]=answer;///answer is saved for question before, as there was++ at Q
}
}
}
}
else
{
position++;///if A Q or R, is overwritten by loop, so doesn't appear in final .
}
}
}
main()
{
int run=1;
while (run==1)
{
int success=0;
do
{
success=load();
}
while (success==0);///do while to keep on re-running load
///debug
if (quiz()==0)
{
printf("To exit, enter 'e'. To take another quiz, choose any other letter\n\n");
int temp=getchar();
while (temp=='\n')
{
temp=getchar();
}
if (temp=='e'||temp=='E')///has to be here, or else eaten. change on other
{
return 0;
}
temp=getchar(); /// newline clearup for new quiz
}
}
}