Revision Master - Structured file with each item on a different line
In this approach, each question and answer set is on a different line and there are additional lines which help the program to know which section it's dealing with. This is probably the most convenient kind of file for the person who makes the question sets.
When you read this file, you will need to distinguish between lines of data and lines of instruction, otherwise you'll get muddled between the questions and the answers. You will treat the lines that you read differently depending on which kind they are. The challenge is to get everything in the right place.
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 distinguish between lines of data and lines of instruction, otherwise you'll get muddled between the questions and the answers. You will treat the lines that you read differently depending on which kind they are. The challenge is to get everything in the right place.
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:
analyzeStructuredLines('N:\structuredanswers.txt');
.. and we'll define the procedure to expect a string containing the file name ...
procedure analyzeStructuredLines(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
SET NUMBER OF ANSWERS TO 0
READ A LINE FROM THE FILE
IF THE LINE READS '<no of answers>' THEN SET INDICATOR TO 1 AND READ NEXT LINE
IF THE LINE READS '<right answer>' THEN SET INDICATOR TO 2 AND READ NEXT LINE
IF THE LINE READS '<question>' THEN SET INDICATOR TO 3 AND READ NEXT LINE
IF THE LINE READS '<answers>' THEN SET INDICATOR TO 4 AND READ NEXT LINE
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 ANSWERS ARRAY
IF THE INDICATOR IS 4 THEN BEGIN
ADD ONE TO THE NUMBER OF ANSWERS
IF NUMBER OF ANSWERS < NUMBER OF ANSWERS EXPECTED THEN
BEGIN
ADD LINE TO QUESTIONS ARRAY[NUMBER OF ANSWERS,NUMBER OF QUESTIONS]
ADD ONE TO INDICATOR
READ NEXT LINE
END
END
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.