So far as you can see below Ive got the program accessing the file and just reading the lines; Ive got the file set up like so...
What is the Answers?
A Do this
B Do that
C Dont do that
D Definitely this
A
So this method is set up that ive got 12 questions in this format stored in the file to display to the user....
BUT the requirements of what I need to do are to display 12 questions randomly each time from a block of say 100 questions which would be stored on the file. Ive a rough idea Id use the Random function in Pascal but am so far baffled.
(Obviously this section is a procedure ive lifted from a much bigger program so the recordtype isnt shown in case anyone was wondering)
Any help would be greatly appreciated!
Assign(Quizfile, 'S:\quizfile.dat');
Reset(Quizfile);
While not EOF (Quizfile) DO
Begin
For J:= 1 TO 5 DO
Begin
Readln(Quizfile, line);
Writeln(line);
End;
Readln(Quizfile, line);
Writeln('Please enter the letter you think is correct');
Readln(answer);
Writeln;
IF UPCASE(answer) = line THEN
Begin
Writeln('Correct');
Writeln;
UserList.List[i].Money := UserList.List[i].Money + 5;
UserList.List[i].Score := UserList.List[i].Score +1;
End
ELSE
Begin
Writeln('Wrong');
Writeln;
UserList.List[i].Money := UserList.List[i].Money - 5;
UserList.List[i].Score := 0+UserList.List[i].Score;
End;
End;
Close(Quizfile);
End;
End;I might as well post the whole program to assist anyone reading this.... also I forsee a problem with the later procedure where I append the file with user created questions....
Again thanks to anyone who can help me...
Program PubQuiz;
Uses Crt;
CONST
LIM =20;
Type
UserRecordType = RECORD
Firstname, Surname : STRING;
Money, Score : Integer;
End;
UserListType = Record
List : Array[1..LIM] OF UserRecordType;
MAX : Integer;
End;
Procedure Display_Menu(Var choice : Integer);
Begin
Writeln('**********************************');
Writeln('* "Pub Quiz Game" *');
Writeln('* A Production *');
Writeln('* by The Usual Suspects *');
Writeln('* Copyright 2007 *');
Writeln('**********************************');
Writeln('* 1). New Question *');
Writeln('* 2). Play Game *');
Writeln('* 3). Display Results *');
Writeln('* 4). Exit Game *');
Writeln('**********************************');
Writeln;
Write('Please enter your choice : ');
Readln(choice);
End;
Procedure CreateQuiz;
VAR
Reply : CHAR;
Quiz, ans, ansA, ansB, ansC, ansD, line : string;
QuizFile : Text;
I : Integer;
BEGIN
CLRSCR;
ASSIGN( QuizFile, 'E:quizfile.DAT' );
APPEND( QuizFile );
REPEAT
WRITELN('Please first type Bonus followed by a space then the Quiz question : ');
READLN(Quiz);
WRITELN('Please first type A followed by a space then Answer option : ');
READLN(ansA);
WRITELN('Please first type B followed by a space then Answer option : ');
READLN(ansB);
WRITELN('Please first type C followed by a space then Answer option : ');
READLN(ansC);
WRITELN('Please first type D followed by a space then Answer option : ');
READLN(ansD);
WRITELN('Please type the letter (in capitals) which contains the correct Answer : ');
READLN(ans);
WRITELN(QuizFile, Quiz);
WRITELN(QuizFile, ansA);
WRITELN(Quizfile, ansB);
WRITELN(Quizfile, ansC);
WRITELN(Quizfile, ansD);
WRITELN(Quizfile, ans);
WRITELN('Do you want to enter another question? Y or N');
READLN(Reply);
UNTIL ( (Reply = 'n') OR (Reply = 'N'));
CLOSE(QuizFile);
END;
Procedure GetUserData(VAR UserList : UserListType);
Var
Quizfile : Text;
Line : String;
answer, Ch,Ch2, option : Char;
Money, Score, Ch_code, I, J : Integer;
Firstname, Surname : String[20];
Begin
clrscr;
Money := 0;
Score := 0;
Writeln('Please enter number of users (numerical response) ');
Readln(UserList.MAX);
FOR I := 1 TO UserList.MAX DO
Begin
Write('Type in your firstname : ');
Readln(UserList.List[i].Firstname);
Write('Type in your surname : ');
Readln(UserList.List[i].Surname);
Ch2 := UserList.List[i].Firstname[1];
IF (Ch2 >= 'a') AND (Ch2 <= 'z') THEN
Ch2 := CHR(ORD(Ch2) - 32);
UserList.List[i].Firstname[1] := Ch2;
Ch := UserList.List[i].Surname[1];
IF (Ch >= 'a') AND (Ch <= 'z') THEN
Ch := CHR(ORD(Ch) - 32);
UserList.List[i].Surname[1] := Ch;
Writeln;
Readln;
Assign(Quizfile, 'E:\quizfile.dat');
Reset(Quizfile);
While not EOF (Quizfile) DO
Begin
For J:= 1 TO 5 DO
Begin
Readln(Quizfile, line);
Writeln(line);
End;
Readln(Quizfile, line);
Writeln('Please enter the letter you think is correct');
Readln(answer);
Writeln;
IF UPCASE(answer) = line THEN
Begin
Writeln('Correct');
Writeln;
UserList.List[i].Money := UserList.List[i].Money + 5;
UserList.List[i].Score := UserList.List[i].Score +1;
End
ELSE
Begin
Writeln('Wrong');
Writeln;
UserList.List[i].Money := UserList.List[i].Money - 5;
UserList.List[i].Score := 0+UserList.List[i].Score;
End;
End;
Close(Quizfile);
End;
End;
Procedure DisplayUserRes(UserList : UserListType);
Var
I : Integer;
ResultFile : Text;
Begin
ASSIGN(ResultFile, 'E:\User.bin');
REWRITE(ResultFile);
For I := 1 to UserList.MAX DO
Begin
Writeln('Player Name :: ',UserList.List[i].Firstname,' ',UserList.List[i].Surname);
Writeln('Money Won is £', UserList.List[i].Money);
Writeln('Score is (inclusive of Bonus Questions Created) ', UserList.List[i].Score);
Writeln(ResultFile, UserList.List[i].Firstname,' ',UserList.List[i].Surname);
Writeln(ResultFile, UserList.List[i].Money);
Writeln(ResultFile, UserList.List[i].Score);
Readln;
clrscr;
End;
CLOSE(ResultFile);
End;
VAR
UserList : UserListType;
Choice : Integer;
Begin
Repeat
Clrscr;
Display_Menu(choice);
Case (choice) Of
1 : CreateQuiz;
2 : GetUserData(UserList);
3 : DisplayUserRes(UserList);
End;
Until (choice=4);
readln;
End.i haven't read your entire program because i'm in a hurry, but from what i figure it out , you only read from file the question. try to design a record structure for the question and answers. in this way you can read all from file into a array of records, and from there to randomize is not so dificult
best regards,
Thats a great idea, thanks. Really my problem is realising how to use the random function in pascal. I know you declare randomize near the start then use Random but have only seen examples using integers, would I have to link a number to each record or something like that?
Thats a great idea, thanks. Really my problem is realising how to use the random function in pascal. I know you declare randomize near the start then use Random but have only seen examples using integers, would I have to link a number to each record or something like that?
if you have an array, e.g:
ado array[1..100]of yourrecord then ado[1],ado[2] are elements of your array. i think it's obvious from now....
best regards,
So far Ive got this for the random section...hopefully assigning each element of the array within this will work...
program random;
uses crt;
var a,b, i:integer;
begin
clrscr;
randomize;
for i:= 1 to 12 DO
begin
b:=random(100)+1; {this makes "b" a random number from 1 to 100}
writeln(b);
readln;
end;
end.This what ive got so far with the questions in record form....
PROGRAM Quiz questions
USES CRT,DOS;
TYPE
QuizType = STRING;
QuizFileType = FILE OF QuizType;
QuizDetailsType =
RECORD
Question : STRING;
OptionA : STRING;
OptionB : STRING;
OptionC : STRING;
OptionD : STRING;
Answer : STRING;
END;
QuizDetailsTableType =
ARRAY [1..6] OF QuizDetailsType;
QuizDetailsListType =
RECORD
Length : INTEGER;
Table : QuizDetailsTableType;
END;
VAR
QuizFile : QuizFileType;
a,b, i, Count,Money, Score, J : INTEGER;
Question : QuizType;
OptionA : QuizType;
OptionB : QuizType;
OptionC : QuizType;
OptionD : QuizType;
Answer : QuizType;
Quizfile : Text;
Line : String;
answer : Char;
BEGIN
Count := 0;
ASSIGN( QuizFile, 'E:quizupdate.DAT' );
RESET( QuizFile );
WHILE( ( NOT EOF( QuizFile ) ) AND ( Count < 6 ) ) DO
BEGIN
Count := Count + 1;
READ( QuizFile, Question );
READ( QuizFile, OptionA );
READ( QuizFile, OptionB );
READ( QuizFile, OptionC );
READ( QuizFile, OptionD );
READ( QuizFile, Answer );
Writeln(Question);
Writeln(OptionA);
Writeln(OptionB);
Writeln(OptionC);
Writeln(OptionD);
Writeln('Please enter the letter you think is correct');
Readln(answer);
IF UPCASE(answer) = line THEN
Begin
Writeln('Correct');
Money := Money + 5;
Score := Score +1;
End
ELSE
Begin
Writeln('Wrong');
Writeln;
Money := Money - 5;
Score := 0+Score;
End;
Writeln('Money Won is £', money);
Writeln('Score out of 12 questions is ', score);
Readln;
clrscr;
End;
BEGIN
Success := FALSE;
EXIT;
END;
QuizDetailsList.Table[Count].Question := Question
QuizDetailsList.Table[Count].OptionA := OptionA
QuizDetailsList.Table[Count].OptionB := OptionB
QuizDetailsList.Table[Count].OptionC := OptionC
QuizDetailsList.Table[Count].OptionD := OptionD
QuizDetailsList.Table[Count].Answer := Answer
END;
BEGIN
Success := FALSE;
EXIT;
END;
QuizDetailsList.Table[Count].Question := Question
QuizDetailsList.Table[Count].OptionA := OptionA
QuizDetailsList.Table[Count].OptionB := OptionB
QuizDetailsList.Table[Count].OptionC := OptionC
QuizDetailsList.Table[Count].OptionD := OptionD
QuizDetailsList.Table[Count].Answer := Answer
END;
IF Count = 6 THEN
BEGIN
Success := FALSE;
EXIT;
END;
Success := TRUE;
QuizDetailsList.Length := Count;
CLOSE( QuizFile );
END;And also Ive done the create question program...really just the Random part being inserted Im stuck on...
PROGRAM CreateQuiz;
USES CRT;
TYPE
QuizType = STRING;
QuizFileType = FILE OF QuizType;
VAR
Reply : CHAR;
Question : QuizType;
OptionA : QuizType;
OptionB : QuizType;
OptionC : QuizType;
OptionD : QuizType;
Answer : QuizType;
QuizFile : QuizFileType;
BEGIN
CLRSCR;
ASSIGN( QuizFile, 'E:quizupdate.DAT' );
REWRITE( QuizFile );
REPEAT
WRITELN( 'Enter question' );
READLN( Question );
WRITE( QuizFile, Question );
WRITELN( 'Enter Option A' );
READLN( OptionA );
WRITE( QuizFile, OptionA );
WRITELN( 'Enter Option B' );
READLN( OptionB );
WRITE( QuizFile, OptionB );
WRITELN( 'Enter Option C' );
READLN( OptionC );
WRITE( QuizFile, OptionC );
WRITELN( 'Enter Option D' );
READLN( OptionD );
WRITE( QuizFile, OptionD );
WRITELN( 'Enter Answer' );
READLN( Answer );
WRITE( QuizFile, Answer );
WRITELN( 'Do you want to enter another question?' );
READLN( Reply );
UNTIL( (Reply = 'n') OR (Reply = 'N') ;
CLOSE( QuizFile );
END.Arghhhhhhhh Im really stuck with this damn Random function...
Ive a rough idea that I would use the concept below...
program random;
uses crt;
var a,b, i:integer;
begin
clrscr;
randomize;
for i:= 1 to 12 DO
begin
b:=random(100)+1; {this makes "b" a random number from 1 to 100}
writeln(b);
readln;
end;
end. ........and link the b to the counter for my array of the quiz questions, so that a random quiz question is displayed 12 times.
Seriously if anyone can give me that final push for this I would be eternally grateful. As you can see Ive done a lot of work.
Its for a project and even tho my original program does what is asked I just thought Id be clever with the Random thing but have zero experience with it so what may be obvious to someone isnt to me.
Cheers anyway.
from i what i saw in your code you read from the file with questions and print the question on screen. now, why you don't read the entire file, built that array of records, apply the randomize and then put a question from that array?
e.g: ado is that array of records.
randomize;
cycle
put_question: ado[random(i)+a_value]
end_of_cycle
i hope you understand the principle.
best regards,
Yes I fully understand now, thanks for your help mate!
I'll go away and try this.
edit
Im pretty sure Cycle is a mac extension but Continue does exactly the same and the main reason was to understand your point for which Im grateful.
ok, i'm sorry for my mistake, i only tried to explain it in a pseudocode manner.
so,
e.g: ado is that array of records.
randomize;
cycle//this can be a while, repeat,ect. you must think until when this cycle is going.
put_question: ado[random(i)+a_value]
end_of_cycle
hope that now is more obvious
best regards,
Yep thats great, I totally get it and I thank you again for your help!
this is the scope of a forum, to help between us. now i will ask you then when the program is finished to post it here so when a begginer will search the same thing to find it and learn from it
best regards,
Sure but its not going to be done for a while cos I have a big exam to study for and as I stated at the start my program already meets the project requirements so I'll just hand it in without the random function.
Although I plan to do the random feature after my exam (end of May) and when it is done I will happily post it up here.