Revision Master - Each item on a different line
In this approach, each question and answer set is on a different line. The numbers at the beginning of each question block 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, you will need to read exactly the right number of lines for each question, otherwise you'll get muddled between the questions and the answers. You can decide here either - always to have a set number of answers - which will make the coding a little bit easier, or to have a variable number of answers. Either way, the challenge is to split the lines into the right parts.
We're going to do this with a single procedure. To make it work we will need to tell it which file we are reading. We'll do that by passing a parameter with the name of the Textfile that we want to read. Our call to the procedure will look like this:
When you read this file, you will need to read exactly the right number of lines for each question, otherwise you'll get muddled between the questions and the answers. You can decide here either - always to have a set number of answers - which will make the coding a little bit easier, or to have a variable number of answers. Either way, the challenge is to split the lines into the right parts.
We're going to do this with a single procedure. To make it work we will need to tell it which file we are reading. We'll do that by passing a parameter with the name of the Textfile that we want to read. Our call to the procedure will look like this:
analyzeLines('N:\lineanswers.txt');
.. and we'll define the procedure to expect a string containing the file name ...
procedure analyzeLines(filename:string);
begin
end;
Here's how the procedure will work. We'll open the file and get it ready to be read, line by line. We'll set up three new variables - one as a temporary string variable to hold the results of reading the file. The second will be an integer and will keep track of what it is that we're supposed to be reading. The third will keep count of the overall number of questions, making sure that we put our information in the right place.
In PseudoCode it looks like this:
In PseudoCode it looks like this:
SET NUMBER OF QUESTIONS TO 1
SET INDICATOR TO 0
SET TEMPORARY TO ''
OPEN FILE
REPEAT WHILE NOT END OF FILE
READ A LINE FROM THE FILE
ADD 1 TO THE INDICATOR
IF THE INDICATOR IS GREATER THAN 8 (ASSUMING FIVE ANSWERS) THEN
SET THE INDICATOR BACK TO 1
ADD 1 TO THE NUMBER OF QUESTIONS
END IF
IF THE INDICATOR IS 1 THEN CONVERT LINE TO INTEGER AND PUT IN NUMBER OF QUESTIONS ARRAY
IF THE INDICATOR IS 2 THEN CONVERT LINE TO INTEGER AND PUT IN RIGHT ANSWER ARRAY
IF THE INDICATOR IS 3 THEN ADD LINE TO QUESTIONS ARRAY
IF THE INDICATOR IS GREATER THAN 3 THEN ADD LINE TO ANSWERS ARRAY
(answersArray[indicator-3,numberOfQuestions])
END
Your challenge is to conver this pseudoCode into real code that works. Use the following to help you:
- Assign, open and read lines from a file like this:
assign(whichfile,filename);
reset(whichfile);
readln(whichfile,tempstring);
- To read until the end of a file, use a whlle loop:
while not eof(whichfile) do
begin
end;
- Convert strings to integers with the function strtoint(stringName)
noOfAnswers[questionCounter] := strtoint(tempstring);
- Use one-dimensional arrays for the questions, the number of answers and the right answer. Use a two-dimensional array for the answers. Click here for more on arrays.