Hi everyone,

My program is meant to be / do:

A quiz about the bible ( < my teacher decided that <).
Using an array of records with a text file (working) it runs the quiz
and lets user print off their results at the end. each question must be on
a different screen, and possibly be randomized.

Yes, i have been through these boards and found other people have posted similar
threads, BUT, mine is unfortunately more complex thanks to my teacher's requirements.

Now, it is all working alright for now, i just would like to now how i could cycle through 12 different screens (each with it's own question), i would say using some loop or array loop?? you will see in my coding that it is FAR LONGER than it needs to be, and i could do it simpler.

Here is my code (for the UNIT that runs with it, the main program is fine :) )

UNIT Bible;

USES CRT, PRINTER, DOS;

CONST	  MaxQs = 12;						  
	  AnyKeyMessage = 'Press ANY KEY to continue';
      EndMessage = 'Are you sure you want to quit? (y/n): ';	  
	  BGColour = LightBlue; {BG and FG Colour are constants so they can be easily changed, } 
      FGColour = White; {without having to go through all the code.} 

TYPE Quiz = RECORD
        QNo : STRING[2];
        Answer : STRING[12];
        Hint : STRING[30];
        Question : STRING[45];
     END;	  

	 DataBase = ARRAY [1..MaxQs] OF Quiz;
	 
VAR QuizInfo : DataBase; Choice, UserChoice, AnyKeyIN : CHAR; Year,Month,Day,WDay : word;
    ACounter, LCounter, CorrectCounter, InCorrectCounter, Count : INTEGER; 
    HelpFile, QuizFile : TEXT; HelpInfo : STRING; UserAnswer : STRING[12];

{~~~~~~~~~~~~~~~~~~~~~~~~~~~ANY KEY~~~~~~~~~~~~~~~~~~~~~~~~~~~~~} 
PROCEDURE AnyKey; {Makes the program move on when any alphanumerical key is pressed}
BEGIN {AnyKey}
     GOTOXY(40-(LENGTH(AnyKeyMessage)DIV 2), 23);
	 WRITE (AnyKeyMessage);
	 AnyKeyIN := readkey;
	 WHILE (AnyKeyIN = #0) DO
	 BEGIN {WHILE}
		AnyKeyIN := readkey
	 END; {WHILE}
END; {AnyKey}	

{~~~~~~~~~~~~~~~~~~~~~~~READ FROM DATA FILE~~~~~~~~~~~~~~~~~~~~~~} 
PROCEDURE ReadFile; {Reads data from the text data file} 
{Uses I/O error check for the data file} 
BEGIN {Read File}
     ASSIGN(QuizFile, 'quiz.dat');
     {$I-} {I/O checking off}
     RESET(QuizFile); {Opens data file for reading} 
	 {$I+} {I/O checking on} 
	 WHILE (IOResult <> 0) DO BEGIN {generates error message if program can't open/find data file}
		 Textbackground(BGColour); {Sets the background colour}
		 CLRSCR;
		 TextColor(FGColour); {Sets the text colour} 
		 ScreenBorder; {Unit draws screen border} 
		 GOTOXY(40-(LENGTH('-------------------------------')DIV 2),2);
		 WRITELN ('BIBLE BOOK QUIZ: ADMINISTRATION');
		 GOTOXY(40-(LENGTH('-------------------------------')DIV 2),3);
		 FOR LCounter := 1 TO 31 DO WRITE(CHR(205));
	     GOTOXY(40-(LENGTH('------------------------------------------')DIV 2), 8);
	     WRITELN ('An error has occured! Cannot open quiz.dat');
		 AnyKey;
		 CLRSCR;
	 END; {WHILE}	 
         FOR Count := 1 TO MaxQs DO
              BEGIN {FOR}
			      READLN(QuizFile, QuizInfo[Count].QNo);
                  READLN(QuizFile, QuizInfo[Count].Answer);
                  READLN(QuizFile, QuizInfo[Count].Hint);
                  READLN(QuizFile, QuizInfo[Count].Question);
         END; {FOR}
     CLOSE(QuizFile); {Closes data file after reading} 
END; {Read File} 

{+++++++++++++++++++++++++++++++++QUIZ PROCEDURES++++++++++++++++++++++++++++++++++} 
     {These procedures are used by the program for running the bible quiz} 
{+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++} 

{~~~~~~~~~~~~~~~~~~~~~~~RESET BG COLOUR~~~~~~~~~~~~~~~~~~~~~~~~~} 
PROCEDURE ResetBGColour; {Resets the background colour} 
BEGIN {Reset BG Colour} 
     TextBackground(BGColour); {Resets the background to the BG constant}
END; {Reset BG Colour} 	 

{~~~~~~~~~~~~~~~~~~~~~QUIZ INSTRUCTIONS~~~~~~~~~~~~~~~~~~~~~~~~} 
PROCEDURE QuizInstructions; {Displays instructions to user} 
BEGIN {Quiz Instructions} 
	 TextBackground(Red);
	 GOTOXY(40-(LENGTH('-------------------------------------------')DIV 2), 11);
	 WRITE ('Press ''a'' to answer the question, ''h'' for');
	 GOTOXY(40-(LENGTH('-------------------------------------------')DIV 2), 12);
	 WRITE ('help, ''c'' for a clue or ''g'' to give up.  ');
END; {Quiz Instructions} 

{~~~~~~~~~~~~~~~~~~~~~~~~~QUESTION 1~~~~~~~~~~~~~~~~~~~~~~~~~~~~} 
PROCEDURE Question1; {Question 1 of quiz} 
BEGIN {Question 1}
     REPEAT
		  Textbackground(BGColour); {Sets the background colour}
	      CLRSCR;
	      TextColor(FGColour); {Sets the text colour} 
	      ScreenBorder; {Unit draws screen border} 
          GOTOXY(40-(LENGTH('---------------')DIV 2),2);
	      WRITELN ('BIBLE BOOK QUIZ');
	      GOTOXY(40-(LENGTH('---------------')DIV 2),3);
	      FOR LCounter := 1 TO 15 DO WRITE(CHR(205));
	      ReadFile; {Reads File for question information} 
	      GOTOXY(40-(LENGTH('-----------')DIV 2), 6);
	      WRITE ('Question #',QuizInfo[1].QNo,''); {Shows Question #} 
          GOTOXY(40-(LENGTH(QuizInfo[1].Question)DIV 2), 8);
	      WRITE(QuizInfo[1].Question); {Shows Question} 
	      InstructionsBorder; {Unit Used} 
          QuizInstructions; {Unit Used} UserChoice := ReadKey;
	      CASE UserChoice OF {Case} 
	           'a' : UserGuessQ1; {Unit Used} 
		       'h' : HelpScreen; {Unit Used} 
		       'c' : ShowHintQ1; 
		       'g' : ShowAnswerQ1;
	      END; {User Choice Case} 
	 {Repeat loop drops out when user chooses 'a' or 'g', moving on to next question} 	  
     UNTIL (UserChoice = 'a') OR (UserChoice = 'g');		  
END; {Question 1} 

{~~~~~~~~~~~~~~~~~~~~~~~~~QUESTION 2~~~~~~~~~~~~~~~~~~~~~~~~~~~~} 
PROCEDURE Question2; {Question 1 of quiz} 
BEGIN {Question 2}
     REPEAT
		  Textbackground(BGColour); {Sets the background colour}
	      CLRSCR;
	      TextColor(FGColour); {Sets the text colour} 
	      ScreenBorder; {Unit draws screen border} 
          GOTOXY(40-(LENGTH('---------------')DIV 2),2);
	      WRITELN ('BIBLE BOOK QUIZ');
	      GOTOXY(40-(LENGTH('---------------')DIV 2),3);
	      FOR LCounter := 1 TO 15 DO WRITE(CHR(205));
	      ReadFile; {Reads File for question information} 
	      GOTOXY(40-(LENGTH('-----------')DIV 2), 6);
	      WRITE ('Question #',QuizInfo[2].QNo,''); {Shows Question #} 
          GOTOXY(40-(LENGTH(QuizInfo[2].Question)DIV 2), 8);
	      WRITE(QuizInfo[2].Question); {Shows Question} 
	      InstructionsBorder; {Unit Used} 
          QuizInstructions; {Unit Used} UserChoice := ReadKey;
	      CASE UserChoice OF {Case} 
	           'a' : UserGuessQ2; {Unit Used} 
		       'h' : HelpScreen; {Unit Used} 
		       'c' : ShowHintQ2; 
		       'g' : ShowAnswerQ2;
	      END; {User Choice Case} 
	 {Repeat loop drops out when user chooses 'a' or 'g', moving on to next question} 	  
     UNTIL (UserChoice = 'a') OR (UserChoice = 'g');		  
END; {Question 2} 
	 

{~~~~~~~~~~~~~~~~SHOW HINT (CLUE) FOR QUESTION 1~~~~~~~~~~~~~~~~~} 
PROCEDURE ShowHintQ1; {Displays the hint for the question} 
BEGIN {Show Hint Question 1} 
     ResetBGColour; {Unit Used}
	 ReadFile;
	 GOTOXY(40-(LENGTH('-----')DIV 2), 15);
	 WRITELN ('Clue:');
	 GOTOXY(40-(LENGTH(QuizInfo[1].Hint)DIV 2), 17);
	 WRITE(QuizInfo[1].Hint);
	 AnyKey; {Unit Used} 
	 CLRSCR;
END; {Show Hint Questions 1} 	

{~~~~~~~~~~~~~~~~SHOW HINT (CLUE) FOR QUESTION 2~~~~~~~~~~~~~~~~~} 
PROCEDURE ShowHintQ2; {Displays the hint for the question} 
BEGIN {Show Hint Question 2} 
     ResetBGColour; {Unit Used}
	 ReadFile;
	 GOTOXY(40-(LENGTH('-----')DIV 2), 15);
	 WRITELN ('Clue:');
	 GOTOXY(40-(LENGTH(QuizInfo[2].Hint)DIV 2), 17);
	 WRITE(QuizInfo[2].Hint);
	 AnyKey; {Unit Used} 
	 CLRSCR;
END; {Show Hint Question 2}  	

{~~~~~~~~~~~SHOW ANSWER (BOOK NAME) FOR QUESTION 1~~~~~~~~~~~~~} 
PROCEDURE ShowAnswerQ1; {Displays the answer for the question}
BEGIN {Show Answer Question 1} 
     ResetBGColour; {Unit Used} 
	 ReadFile;
	 GOTOXY(40-(LENGTH('-----------')DIV 2), 15);
	 WRITELN ('Answer was:');
	 GOTOXY(40-(LENGTH(QuizInfo[1].Answer)DIV 2), 17);
	 WRITE(QuizInfo[1].Answer);
	 AnyKey; {Unit Used} 
	 CLRSCR;
END; {Show Answer Question 1} 	 

{~~~~~~~~~~~SHOW ANSWER (BOOK NAME) FOR QUESTION 1~~~~~~~~~~~~~} 
PROCEDURE ShowAnswerQ2; {Displays the answer for the question}
BEGIN {Show Answer Question 2} 
     ResetBGColour; {Unit Used} 
	 ReadFile;
	 GOTOXY(40-(LENGTH('-----------')DIV 2), 15);
	 WRITELN ('Answer was:');
	 GOTOXY(40-(LENGTH(QuizInfo[2].Answer)DIV 2), 17);
	 WRITE(QuizInfo[2].Answer);
	 AnyKey; {Unit Used} 
	 CLRSCR;
END; {Show Answer Question 2} 	
 
{~~~~~~~~~~~~~~~~USER GUESS (USERS' ANSWER) Q1~~~~~~~~~~~~~~~~~~~} 
PROCEDURE UserGuessQ1; {Allows user to enter their answer} 
BEGIN {User Guess Question 1} 
     ResetBGColour;
     GOTOXY(40-(LENGTH('-------------------------')DIV 2), 15);
	 WRITE ('Your answer: '); READLN(UserAnswer);
	 IF UserAnswer = QuizInfo[1].Answer 
	 THEN CorrectCounter := CorrectCounter + 1 {Add 1 to correct score if Q is correct} 
	 ELSE InCorrectCounter := InCorrectCounter + 1; {Add 1 to incorrect score if Q is incorrect} 
	 AnyKey;	 
END; {User Guess Question 1} 	

{~~~~~~~~~~~~~~~~USER GUESS (USERS' ANSWER) Q2~~~~~~~~~~~~~~~~~~~} 
PROCEDURE UserGuessQ2; {Allows user to enter their answer} 
BEGIN {User Guess Question 2} 
     ResetBGColour;
     GOTOXY(40-(LENGTH('-------------------------')DIV 2), 15);
	 WRITE ('Your answer: '); READLN(UserAnswer);
	 IF UserAnswer = QuizInfo[2].Answer 
	 THEN CorrectCounter := CorrectCounter + 1 {Add 1 to correct score if Q is correct} 
	 ELSE InCorrectCounter := InCorrectCounter + 1; {Add 1 to incorrect score if Q is incorrect} 
	 AnyKey;	 
END; {User Guess Question 2} 

{~~~~~~~~~~~~~~~~USER GUESS (USERS' ANSWER) Q3~~~~~~~~~~~~~~~~~~~} 
PROCEDURE UserGuessQ3; {Allows user to enter their answer} 
BEGIN {User Guess Question 3} 
     ResetBGColour;
     GOTOXY(40-(LENGTH('-------------------------')DIV 2), 15);
	 WRITE ('Your answer: '); READLN(UserAnswer);
	 IF UserAnswer = QuizInfo[3].Answer 
	 THEN CorrectCounter := CorrectCounter + 1 {Add 1 to correct score if Q is correct} 
	 ELSE InCorrectCounter := InCorrectCounter + 1; {Add 1 to incorrect score if Q is incorrect} 
	 AnyKey;	 
END; {User Guess Question 3} 

{~~~~~~~~~~~~~~~~USER GUESS (USERS' ANSWER) Q4~~~~~~~~~~~~~~~~~~~} 
PROCEDURE UserGuessQ4; {Allows user to enter their answer} 
BEGIN {User Guess Question 4} 
     ResetBGColour;
     GOTOXY(40-(LENGTH('-------------------------')DIV 2), 15);
	 WRITE ('Your answer: '); READLN(UserAnswer);
	 IF UserAnswer = QuizInfo[4].Answer 
	 THEN CorrectCounter := CorrectCounter + 1 {Add 1 to correct score if Q is correct} 
	 ELSE InCorrectCounter := InCorrectCounter + 1; {Add 1 to incorrect score if Q is incorrect} 
	 AnyKey;	 
END; {User Guess Question 4} 

{~~~~~~~~~~~~~~~~USER GUESS (USERS' ANSWER) Q5~~~~~~~~~~~~~~~~~~~} 
PROCEDURE UserGuessQ5; {Allows user to enter their answer} 
BEGIN {User Guess Question 5} 
     ResetBGColour;
     GOTOXY(40-(LENGTH('-------------------------')DIV 2), 15);
	 WRITE ('Your answer: '); READLN(UserAnswer);
	 IF UserAnswer = QuizInfo[5].Answer 
	 THEN CorrectCounter := CorrectCounter + 1 {Add 1 to correct score if Q is correct} 
	 ELSE InCorrectCounter := InCorrectCounter + 1; {Add 1 to incorrect score if Q is incorrect} 
	 AnyKey;	 
END; {User Guess Question 5} 

{~~~~~~~~~~~~~~~~USER GUESS (USERS' ANSWER) Q6~~~~~~~~~~~~~~~~~~~} 
PROCEDURE UserGuessQ6; {Allows user to enter their answer} 
BEGIN {User Guess Question 6} 
     ResetBGColour;
     GOTOXY(40-(LENGTH('-------------------------')DIV 2), 15);
	 WRITE ('Your answer: '); READLN(UserAnswer);
	 IF UserAnswer = QuizInfo[6].Answer 
	 THEN CorrectCounter := CorrectCounter + 1 {Add 1 to correct score if Q is correct} 
	 ELSE InCorrectCounter := InCorrectCounter + 1; {Add 1 to incorrect score if Q is incorrect} 
	 AnyKey;	 
END; {User Guess Question 6} 

{~~~~~~~~~~~~~~~~USER GUESS (USERS' ANSWER) Q7~~~~~~~~~~~~~~~~~~~} 
PROCEDURE UserGuessQ7; {Allows user to enter their answer} 
BEGIN {User Guess Question 7} 
     ResetBGColour;
     GOTOXY(40-(LENGTH('-------------------------')DIV 2), 15);
	 WRITE ('Your answer: '); READLN(UserAnswer);
	 IF UserAnswer = QuizInfo[7].Answer 
	 THEN CorrectCounter := CorrectCounter + 1 {Add 1 to correct score if Q is correct} 
	 ELSE InCorrectCounter := InCorrectCounter + 1; {Add 1 to incorrect score if Q is incorrect} 
	 AnyKey;	 
END; {User Guess Question 7} 
	     
{~~~~~~~~~~~~~~~~USER GUESS (USERS' ANSWER) Q8~~~~~~~~~~~~~~~~~~~} 
PROCEDURE UserGuessQ8; {Allows user to enter their answer} 
BEGIN {User Guess Question 8} 
     ResetBGColour;
     GOTOXY(40-(LENGTH('-------------------------')DIV 2), 15);
	 WRITE ('Your answer: '); READLN(UserAnswer);
	 IF UserAnswer = QuizInfo[8].Answer 
	 THEN CorrectCounter := CorrectCounter + 1 {Add 1 to correct score if Q is correct} 
	 ELSE InCorrectCounter := InCorrectCounter + 1; {Add 1 to incorrect score if Q is incorrect} 
	 AnyKey;	 
END; {User Guess Question 8} 		 

{~~~~~~~~~~~~~~~~USER GUESS (USERS' ANSWER) Q9~~~~~~~~~~~~~~~~~~~} 
PROCEDURE UserGuessQ9; {Allows user to enter their answer} 
BEGIN {User Guess Question 9} 
     ResetBGColour;
     GOTOXY(40-(LENGTH('-------------------------')DIV 2), 15);
	 WRITE ('Your answer: '); READLN(UserAnswer);
	 IF UserAnswer = QuizInfo[9].Answer 
	 THEN CorrectCounter := CorrectCounter + 1 {Add 1 to correct score if Q is correct} 
	 ELSE InCorrectCounter := InCorrectCounter + 1; {Add 1 to incorrect score if Q is incorrect} 
	 AnyKey;	 
END; {User Guess Question 9} 

{~~~~~~~~~~~~~~~~USER GUESS (USERS' ANSWER) Q10~~~~~~~~~~~~~~~~~~~} 
PROCEDURE UserGuessQ10; {Allows user to enter their answer} 
BEGIN {User Guess Question 10} 
     ResetBGColour;
     GOTOXY(40-(LENGTH('-------------------------')DIV 2), 15);
	 WRITE ('Your answer: '); READLN(UserAnswer);
	 IF UserAnswer = QuizInfo[10].Answer 
	 THEN CorrectCounter := CorrectCounter + 1 {Add 1 to correct score if Q is correct} 
	 ELSE InCorrectCounter := InCorrectCounter + 1; {Add 1 to incorrect score if Q is incorrect} 
	 AnyKey;	 
END; {User Guess Question 10} 

{~~~~~~~~~~~~~~~~USER GUESS (USERS' ANSWER) Q11~~~~~~~~~~~~~~~~~~~} 
PROCEDURE UserGuessQ11; {Allows user to enter their answer} 
BEGIN {User Guess Question 11} 
     ResetBGColour;
     GOTOXY(40-(LENGTH('-------------------------')DIV 2), 15);
	 WRITE ('Your answer: '); READLN(UserAnswer);
	 IF UserAnswer = QuizInfo[11].Answer 
	 THEN CorrectCounter := CorrectCounter + 1 {Add 1 to correct score if Q is correct} 
	 ELSE InCorrectCounter := InCorrectCounter + 1; {Add 1 to incorrect score if Q is incorrect} 
	 AnyKey;	 
END; {User Guess Question 11} 

{~~~~~~~~~~~~~~~~USER GUESS (USERS' ANSWER) Q12~~~~~~~~~~~~~~~~~~~} 
PROCEDURE UserGuessQ12; {Allows user to enter their answer} 
BEGIN {User Guess Question 12} 
     ResetBGColour;
     GOTOXY(40-(LENGTH('-------------------------')DIV 2), 15);
	 WRITE ('Your answer: '); READLN(UserAnswer);
	 IF UserAnswer = QuizInfo[12].Answer 
	 THEN CorrectCounter := CorrectCounter + 1 {Add 1 to correct score if Q is correct} 
	 ELSE InCorrectCounter := InCorrectCounter + 1; {Add 1 to incorrect score if Q is incorrect} 
	 AnyKey;	 
END; {User Guess Question 12} 
		 
{~~~~~~~~~~~~~~~~~~~~~~~~~HELP SCREEN~~~~~~~~~~~~~~~~~~~~~~~~~~~} 
PROCEDURE HelpScreen; {Allows user to view help on how to play the quiz} 
BEGIN {Help Screen}
     Textbackground(BGColour); {Sets the background colour}
	 CLRSCR;
	 TextColor(FGColour); {Sets the text colour} 
	 ScreenBorder; {Unit draws screen border} 
     GOTOXY(40-(LENGTH('---------------------')DIV 2),2);
	 WRITELN ('BIBLE BOOK QUIZ: HELP');
	 GOTOXY(40-(LENGTH('---------------------')DIV 2),3);
	 FOR LCounter := 1 TO 21 DO WRITE(CHR(205));
     RESET(HelpFile); {Opens the Help File for reading} 
	 READLN(HelpFile, HelpInfo); {Reads the text within the help file}
     WRITELN;	 
	 WRITELN (HelpInfo); {Displays help file information} 	 
	 CLOSE(HelpFile); {Closes the Help File after reading} 
	 ScreenBorder;
	 AnyKey;
     CLRSCR;
END; {Help Screen}

As you can tell, too long, why all those procedures for each answer guess?, etc

Please note: I am programming in Turbo Pascal 5.5, not delphi :p

Any help will be GREATLY appreciated :)

Regards,

Quagmire

Recommended Answers

All 2 Replies

The only difference between two UserGuess procedures is the index that is used for the QuizInfo array. You can use a single procedure for this, if you pass this index as a parameter to it. It's definition could be:

procedure UserGuessQ(QuestionNumber: Integer);

I'm sure this will get you on the right track.

Thanks pritaeas! That worked fine, the quiz runs in a single procedure now!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.