Revision Master: Everything on one line
In this approach, each question and answer set is on one line, separated by commas or some other obvious 'delimiter' or separator. That delimiter needs to be unique - ie: it must not appear in the question or answer text, or there'll be chaos. The numbers at the beginning in the example above, indicate how many answers there are to this question - and which answer is the correct one.
When you read this file one line at a time, you will have a line that contains everything that you need for one question. The challenge is to split it up into its parts.
We're going to do this with a single procedure. To make it work we need to give it two pieces of information as parameters. The first is the line containing the question and answers. The second is the number of the array element that we want to put the finished information into. Our call to the procedure will look like this:
When you read this file one line at a time, you will have a line that contains everything that you need for one question. The challenge is to split it up into its parts.
We're going to do this with a single procedure. To make it work we need to give it two pieces of information as parameters. The first is the line containing the question and answers. The second is the number of the array element that we want to put the finished information into. Our call to the procedure will look like this:
splitMeUp(questionLine,questionNumber)
Where questionLine is a string variable holding the line that we've just read from the file, and questionNumber is the number of the question (we'll need to keep count).
The procedure itself will be defined in such a way as to provide variables for the expected parameters. These will have slightly different names to avoid confusing them with the originals.
The procedure itself will be defined in such a way as to provide variables for the expected parameters. These will have slightly different names to avoid confusing them with the originals.
procedure splitMeUp(qLine:string;qNumber:integer);
begin
end;
Here's how the procedure will work. We will work from the beginning of the line to the end, one character at a time. We'll look at the character and - if it's a comma - then mark the fact that we're in a new section by adding one to a controlling integer variable. That controlling variable tells us what we're looking at. If not, we'll add the current character to a temporary string. When we hit a comma we'll regard the previous bit as complete and write it into the correct array.
In pseudocode it looks like this:
In pseudocode it looks like this:
SET INDICATOR TO 0
SET TEMPORARY TO ''
LOOP FOR LENGTH OF INPUT STRING
READ NEXT CHARACTER FROM INPUT STRING
IF CHARACTER IS A COMMA
IF INDICATOR IS 0 THEN CONVERT TEMPORARY TO INTEGER AND PUT IN NUMBEROFQUESTIONS
IF INDICATOR IS 1 THEN CONVERT TEMPORARY TO INTEGER AND PUT IN NUMBEROFANSWERS
IF INDICATOR IS 2 THEN PUT TEMPORARY IN QUESTION
IF INDICATOR IS GREATER THAN 2
PUT TEMPORARY IN ANSWER[ANSWERS,INDICATOR-2]
ADD ONE TO INDICATOR
SET TEMPORARY TO ''
ELSE
ADD CHARACTER TO TEMPORARY
END LOOP
Your challenge is to turn this into real code. Here are some pointers that will help:
- Use a FOR loop. You can find out the length of the input string with the function length(stringName)
for charcounter := 1 to length(qLine) do
- To read a single character out of a string, use square brackets with the number of the character (starting from one) that you want (in the example below, qLine is the line containing the questions and answers and charcounter is the integer variable from the FOR loop above.
thischar := qLine[charcounter];
- I've actually defined thischar as a variable of type 'char'. You can use a string if you like, but char is a special Delphi type that contains exactly one character. It's quite useful and very efficient if you know that you only want one single character.
thischar : char;
- Convert strings to integers with the function strtoint(stringName)
noOfAnswers[qNumber] := strtoint(stringreader);
- Use one dimensional arrays for your questions, the number of answers and the right answer. Use a two dimensional array for the answers. Click here to find out more about arrays