1. How do I get this program to ask more than one question?
- Posted by bubba Jan 03, 2009
- 2647 views
I am trying to write a quiz program that will ask 10 or 20 random questions and then grade it. I got it working great for the first question, but can't figure out how to ask another question.
I am obviously missing something very big in my Euphoria knowledge.
This is a stripped down version for your perusal.
handler.exw - written by Ron Austin 386-688-4057 without warning with trace
include win32lib.ew win32 library include datetime.e include gstring.e George Walters string functions
atom correct object Win,Win1,Win2,Win3,record,Win4,ans1,ans2,ans3,ans4 sequence Data,text,key,test,keyindex,right,wrong,question,answer1,answer2,answer3,answer4,rightans integer ct,recs,line,Okay,ans, qcount,fields,RecNum
question=repeat(0,5) answer1=repeat(0,5) answer2=repeat(0,5) answer3=repeat(0,5) answer4=repeat(0,5) right=repeat(0,5) qcount=5 fields = 9 Okay=0 Data = repeat(0,fields)
constant Ver = "Version 1.0", App = "Handler Demo"
ans1 = create(Radio,"",Win2,5,22,12, 12,0) ans2 = create(Radio,"",Win2,5,62,12, 12,0) ans3 = create(Radio,"",Win2,5,102,12, 12,0) ans4 = create(Radio,"",Win2,5,142,12, 12,0)
setFont( Win1, "Arial", 12, Bold ) setFont( Win2, "Arial", 16, Bold+Italic ) setFont( Win3, "Arial", 12, Bold )
procedure LoadQuestions() question[1]="WHO IS BURIED IN GRANT'S TOMB?" question[2]="5 + 5 = ?" question[3]="HOW MANY TEETH DOES AN ADULT HUMAN HAVE?" question[4]="WHAT IS THE CAPITAL OF THE STATE OF FLORIDA?" question[5]="APPROXAMENTALY HOW MILES IS IT TO THE SUN?"
answer1[1]="GRANT" answer2[1]="LINCON" answer3[1]="NOBODY" answer4[1]="WASHINGTON"
answer1[2]="5" answer2[2]="10" answer3[2]="25" answer4[2]="50"
answer1[3]="36" answer2[3]="35" answer3[3]="32" answer4[3]="40"
answer1[4]="MIAMI" answer2[4]="ORLANDO" answer3[4]="JACKSONVILLE" answer4[4]="TALLAHASSEE"
answer1[5]="90,000,000" answer2[5]="89,000,000" answer3[5]="93,000,000" answer4[5]="91,000,000"
right[1]="A" right[2]="B" right[3]="C" right[4]="D" right[5]="C" end procedure
LoadQuestions()
procedure DisplayQuestion () line=15 setPenPosition(Win1,5,line) wPuts( Win1,question[RecNum])
setPenPosition(Win2,30,line) wPuts(Win2,answer1[RecNum]) line=line+40
setPenPosition(Win2,30,line) wPuts(Win2,answer2[RecNum]) line=line+40
setPenPosition(Win2,30,line) wPuts(Win2,answer3[RecNum]) line=line+40
setPenPosition(Win2,25,line) wPuts(Win2,answer4[RecNum]) end procedure
for i= 1 to 3 do repaintWindow(Win1) repaintWindow(Win2) repaintWindow(Win3) RecNum = rand(qcount) key=keyindex[RecNum] DisplayQuestion() end for end procedure
procedure Accept(integer self, integer event, sequence parms)
find out which radio button was picked ans=0 if isChecked(ans1) then ans=2 end if if isChecked(ans2) then ans=3 end if if isChecked(ans3) then ans=4 end if if isChecked(ans4) then ans=5 end if
convert letter of right answer to a number correct=0 if equal (right[RecNum],"A") then correct=2 rightans=answer1[RecNum] end if if equal (right[RecNum],"B") then correct=3 rightans=answer2[RecNum] end if if equal (right[RecNum],"C") then correct=4 rightans=answer3[RecNum] end if if equal (right[RecNum],"D") then correct=5 rightans=answer4[RecNum] end if
setPenPosition(Win3,10,10) wPuts(Win3,"The correct answer is") setPenPosition(Win3,10,40) wPuts(Win3,Data[correct]) wPuts(Win3,rightans)
create pushbutton Okay = create( PushButton, " Next Question", Win3, 500, 20, 100, 40, 0 ) end procedure
setHandler(Win, w32HActivate, routine_id("Activate_MainWin"))
setHandler({ans1,ans2,ans3,ans4}, w32HClick, routine_id( "Accept" ))
setHandler(Okay,w32HClick, routine_id( "Next_Question" ))
2. Re: How do I get this program to ask more than one question?
- Posted by bubba Jan 03, 2009
- 2608 views
Okay, seems as though some of the lines have wrapped around and I don't see an edit button on this forum. Sorry, I don't know how to fix the listing.
3. Re: How do I get this program to ask more than one question?
- Posted by jcmarsh1 Jan 03, 2009
- 2611 views
<snip>
Use "Quoted Reply" and:
<eucode> -- eucode </eucode>
Without the {} curly brackets.
Also Use "Preview" before you post a message so that you can edit your formatting mistakes.
I am trying to write a quiz program that will ask 10 or 20 random questions and then grade it. I got it working great for the first question, but can't figure out how to ask another question.
I am obviously missing something very big in my Euphoria knowledge.
This is a stripped down version for your perusal.
--handler.exw - written by Ron Austin 386-688-4057 without warning with trace include win32lib.ew -- win32 library include datetime.e include gstring.e -- George Walters string functions atom correct object Win,Win1,Win2,Win3,record,Win4,ans1,ans2,ans3,ans4 sequence Data,text,key,test,keyindex,right,wrong,question,answer1,answer2,answer3,answer4,rightans integer ct,recs,line,Okay,ans, qcount,fields,RecNum question=repeat(0,5) answer1=repeat(0,5) answer2=repeat(0,5) answer3=repeat(0,5) answer4=repeat(0,5) right=repeat(0,5) qcount=5 fields = 9 Okay=0 Data = repeat(0,fields) constant Ver = "Version 1.0", App = "Handler Demo" ----------------------------------------------------------------------------- -- @ MAIN CONTROLS ----------------------------------------------------------------------------- Win = createEx( Window,"Handler Test", 0, Center,Center,800,500,0,0) Win1 = create( Window, "Window one", Win, 5, 10, 770, 40, {WS_CHILD,WS_BORDER} ) Win2 = create( Window, "Window two", Win, 5, 60, 770, 250, {WS_CHILD,WS_BORDER} ) Win3 = create( Window, "Selection",Win, 5, 320, 770, 100, {WS_CHILD,WS_BORDER} ) ans1 = create(Radio,"",Win2,5,22,12, 12,0) ans2 = create(Radio,"",Win2,5,62,12, 12,0) ans3 = create(Radio,"",Win2,5,102,12, 12,0) ans4 = create(Radio,"",Win2,5,142,12, 12,0) setFont( Win1, "Arial", 12, Bold ) setFont( Win2, "Arial", 16, Bold+Italic ) setFont( Win3, "Arial", 12, Bold ) ----------------------------------------------------------------------------- -- @ LOAD QUESTIONS -- Final program will load questions and answers from disk file ----------------------------------------------------------------------------- procedure LoadQuestions() question[1]="WHO IS BURIED IN GRANT'S TOMB?" question[2]="5 + 5 = ?" question[3]="HOW MANY TEETH DOES AN ADULT HUMAN HAVE?" question[4]="WHAT IS THE CAPITAL OF THE STATE OF FLORIDA?" question[5]="APPROXAMENTALY HOW MILES IS IT TO THE SUN?" answer1[1]="GRANT" answer2[1]="LINCON" answer3[1]="NOBODY" answer4[1]="WASHINGTON" answer1[2]="5" answer2[2]="10" answer3[2]="25" answer4[2]="50" answer1[3]="36" answer2[3]="35" answer3[3]="32" answer4[3]="40" answer1[4]="MIAMI" answer2[4]="ORLANDO" answer3[4]="JACKSONVILLE" answer4[4]="TALLAHASSEE" answer1[5]="90,000,000" answer2[5]="89,000,000" answer3[5]="93,000,000" answer4[5]="91,000,000" right[1]="A" right[2]="B" right[3]="C" right[4]="D" right[5]="C" end procedure ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- -- @ INITIALIZE PROGRAM ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- LoadQuestions() procedure DisplayQuestion () line=15 setPenPosition(Win1,5,line) wPuts( Win1,question[RecNum]) setPenPosition(Win2,30,line) wPuts(Win2,answer1[RecNum]) line=line+40 setPenPosition(Win2,30,line) wPuts(Win2,answer2[RecNum]) line=line+40 setPenPosition(Win2,30,line) wPuts(Win2,answer3[RecNum]) line=line+40 setPenPosition(Win2,25,line) wPuts(Win2,answer4[RecNum]) end procedure ------------------------------------------------------------------------------- -- @ ACTIVATE PROGRAM -------------------------------------------------------------------------------- procedure Activate_MainWin(integer self, integer event, sequence parms) openWindow(Win1,Normal) openWindow(Win2,Normal) openWindow(Win3,Normal) RecNum = rand(qcount) DisplayQuestion() end procedure ----------------------------------------------------------------------- -- Accept Answer ----------------------------------------------------------------------- procedure Next_Question(integer self, integer event, sequence parms) for i= 1 to 3 do repaintWindow(Win1) repaintWindow(Win2) repaintWindow(Win3) RecNum = rand(qcount) key=keyindex[RecNum] DisplayQuestion() end for end procedure procedure Accept(integer self, integer event, sequence parms) -- find out which radio button was picked ans=0 if isChecked(ans1) then ans=2 end if if isChecked(ans2) then ans=3 end if if isChecked(ans3) then ans=4 end if if isChecked(ans4) then ans=5 end if -- convert letter of right answer to a number correct=0 if equal (right[RecNum],"A") then correct=2 rightans=answer1[RecNum] end if if equal (right[RecNum],"B") then correct=3 rightans=answer2[RecNum] end if if equal (right[RecNum],"C") then correct=4 rightans=answer3[RecNum] end if if equal (right[RecNum],"D") then correct=5 rightans=answer4[RecNum] end if setPenPosition(Win3,10,10) wPuts(Win3,"The correct answer is") setPenPosition(Win3,10,40) -- wPuts(Win3,Data[correct]) wPuts(Win3,rightans) -- create pushbutton Okay = create( PushButton, " Next Question", Win3, 500, 20, 100, 40, 0 ) end procedure ------------------------------------------------------------------------------- -- @ SETUP HANDLERS -------------------------------------------------------------------------------- setHandler(Win, w32HActivate, routine_id("Activate_MainWin")) setHandler({ans1,ans2,ans3,ans4}, w32HClick, routine_id( "Accept" )) setHandler(Okay,w32HClick, routine_id( "Next_Question" )) ------------------------------------------------------------------------------- -- @ MAIN PROGRAM ------------------------------------------------------------------------------- WinMain( Win, Normal
Peace jcmarsh
4. Re: How do I get this program to ask more than one question?
- Posted by bubba Jan 03, 2009
- 2596 views
Hey thanks for cleaning that up.
5. Re: How do I get this program to ask more than one question?
- Posted by DanM Jan 04, 2009
- 2616 views
I had difficulty getting your program to run correctly for the first question,
as it blanked both question and answers in about 5-10 seconds.
I think your problem with not working for subsequent questions was in
"Next_Question", where you ran thru repaint the three windows THREE TIMES,
which looks unnecessary.
But since I couldn't figure out why it was blanking for me, I just rewrote it
as follows, using LABELS instead of CHILD WINDOWS. That doesn't give the look
you had, but it does work. (the one thing I for some reason couldn't get to work
right was blanking the "Okay" button after it was pressed, so as to just make it
visible when an answer was selected, dunno why it wouldn't stay invisible)
oh, and you'll want to use a spell checker in your final version:
APPROXAMENTALY should be approximately (I had to use spell checker to get
it right here).
-- code generated by Win32Lib IDE v0.20.1 include Win32Lib.ew without warning -------------------------------------------------------------------------------- -- Window Win constant Win = createEx( Window, "Main Window", 0, Center, Center, 800, 500, 0, 0 ) constant Group12 = createEx( Group, "", Win, 16, 8, 764, 60, 0, 0 ) constant LText11 = createEx( LText, "", Group12, 12, 8, 732, 44, 0, 0 ) setFont( LText11,"Arial",12,Normal+Bold) constant Group20 = createEx( Group, "", Win, 24, 308, 752, 112, 0, 0 ) constant LText23 = createEx( LText, "", Group20, 20, 68, 476, 32, 0, 0 ) constant LText22 = createEx( LText, "", Group20, 20, 24, 452, 28, 0, 0 ) constant Group13 = createEx( Group, "", Win, 20, 88, 760, 188, 0, 0 ) constant Okay = createEx( PushButton, "Next Question", Group20, 504, 48, 88, 28, 0, 0 ) setVisible( Okay, w32False ) constant ans2 = createEx( Radio, "", Group13, 28, 64, 148, 20, 0, 0 ) constant ans1 = createEx( Radio, "", Group13, 28, 24, 148, 20, 0, 0 ) constant ans3 = createEx( Radio, "", Group13, 28, 104, 148, 20, 0, 0 ) constant ans4 = createEx( Radio, "", Group13, 28, 144, 148, 20, 0, 0 ) --------------------------------------------------------- -------------------------------------------------------------------------------- atom correct object record sequence Data,text,key,test,keyindex,right,wrong,question,answer1,answer2,answer3,answer4,rightans integer ct,recs,line,ans, qcount,fields,RecNum question=repeat(0,5) answer1=repeat(0,5) answer2=repeat(0,5) answer3=repeat(0,5) answer4=repeat(0,5) right=repeat(0,5) qcount=5 fields = 9 Data = repeat(0,fields) constant Ver = "Version 1.0", App = "Handler Demo" ----------------------------------------------------------------------------- -- @ LOAD QUESTIONS -- Final program will load questions and answers from disk file ----------------------------------------------------------------------------- procedure LoadQuestions() question[1]="WHO IS BURIED IN GRANT'S TOMB?" question[2]="5 + 5 = ?" question[3]="HOW MANY TEETH DOES AN ADULT HUMAN HAVE?" question[4]="WHAT IS THE CAPITAL OF THE STATE OF FLORIDA?" question[5]="APPROXAMENTALY HOW MILES IS IT TO THE SUN?" answer1[1]="GRANT" answer2[1]="LINCON" answer3[1]="NOBODY" answer4[1]="WASHINGTON" answer1[2]="5" answer2[2]="10" answer3[2]="25" answer4[2]="50" answer1[3]="36" answer2[3]="35" answer3[3]="32" answer4[3]="40" answer1[4]="MIAMI" answer2[4]="ORLANDO" answer3[4]="JACKSONVILLE" answer4[4]="TALLAHASSEE" answer1[5]="90,000,000" answer2[5]="89,000,000" answer3[5]="93,000,000" answer4[5]="91,000,000" right[1]="A" right[2]="B" right[3]="C" right[4]="D" right[5]="C" end procedure ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- -- @ INITIALIZE PROGRAM (moved into onActivate Win) ------------------------------------------------------------------------------- ------------------------------------------------------------------------------- procedure DisplayQuestion () setText(LText11, question[RecNum]) setText(ans1, answer1[RecNum]) setText(ans2, answer2[RecNum]) setText(ans3, answer3[RecNum]) setText(ans4, answer4[RecNum]) setVisible(Okay, w32False) end procedure ------------------------------------------------------------------------------- -- @ ACTIVATE PROGRAM -------------------------------------------------------------------------------- -------------------------------------------------------------------------------- procedure Win_onActivate (integer self, integer event, sequence params)--params is () LoadQuestions() RecNum = rand(qcount) DisplayQuestion() end procedure setHandler( Win, w32HActivate, routine_id("Win_onActivate")) -------------------------------------------------------------------------------- procedure Accept (integer self, integer event, sequence params)--params is () -- find out which radio button was picked --puts(1, "\nACCEPT routine accessed") ans=0 if isChecked(ans1) then ans=2 end if if isChecked(ans2) then ans=3 end if if isChecked(ans3) then ans=4 end if if isChecked(ans4) then ans=5 end if -- convert letter of right answer to a number correct=0 if equal (right[RecNum],"A") then correct=2 rightans=answer1[RecNum] end if if equal (right[RecNum],"B") then correct=3 rightans=answer2[RecNum] end if if equal (right[RecNum],"C") then correct=4 rightans=answer3[RecNum] end if if equal (right[RecNum],"D") then correct=5 rightans=answer4[RecNum] end if if not equal(ans, correct) then setText(LText22, "The correct answer is") setText(LText23, rightans) else setText(LText22, "YES!") end if setVisible(Okay, w32True) end procedure setHandler( {ans1,ans2,ans3,ans4}, w32HClick, routine_id("Accept")) -------------------------------------------------------------------------------- procedure Okay_onClick (integer self, integer event, sequence params)--params is () RecNum = rand(qcount) --key=keyindex[RecNum] setCheck({ans1,ans2, ans3, ans4}, 0) DisplayQuestion() setVisible(Okay, w32False) -- <-- this SHOULD make the okay button invisible, but doesn't? setText(LText22, "") setText(LText23, "") end procedure setHandler( Okay, w32HClick, routine_id("Okay_onClick")) WinMain( Win,Normal )
hope this helps. Dan
6. Re: How do I get this program to ask more than one question?
- Posted by bubba Jan 04, 2009
- 2613 views
Dan you are a genius! Thank you so much. I haven't looked at the code to see what you did yet. I wanted to thank you first for getting this blasted thing to work.
This is so frustrating. I am so used to just sitting down and writting out a program that works, and if it doesn't I can quickly see what's wrong. I just need more experience with Euphoria and more people like you who are willing to help others. Thank you again.
Spelling is definately not my strong suit. I guess I should spell check everything I do.
7. Re: How do I get this program to ask more than one question?
- Posted by DanM Jan 04, 2009
- 2617 views
Dan you are a genius!
Hardly! Just stubborn. When I couldn't see what might be wrong, I just thought it'd be easier to use Judith's IDE to semi-rewrite it, and that took longer than I thought it would too. The fact that you organized your code nicely helped a lot, though.
Thank you so much. I haven't looked at the code to see what you did yet. I wanted to thank you first for getting this blasted thing to work.
You're welcome, I just hope I sent you the final version that does work.
This is so frustrating. I am so used to just sitting down and writting out a program that works, and if it doesn't I can quickly see what's wrong.
heh heh heh, I'm the opposite: I'm used to writting code that DOESN'T work right at first, and the error(s) usually don't just leap out at me at all.
I just need more experience with Euphoria and more people like you who are willing to help others. Thank you again.
Spelling is definately not my strong suit. I guess I should spell check everything I do.
I think I've noticed an inverse relationship between spelling ability and coding ability!
Good luck,
Dan
8. Re: How do I get this program to ask more than one question?
- Posted by ghaberek (admin) Jan 05, 2009
- 2599 views
You could also streamline your sequences. Keep in mind that sequences may be nested to any depth, which can help when iterating through lists like you're doing.
sequence questions, answers, correct questions = { "WHO IS BURIED IN GRANT'S TOMB?", "5 + 5 = ?", "HOW MANY TEETH DOES AN ADULT HUMAN HAVE?", "WHAT IS THE CAPITAL OF THE STATE OF FLORIDA?", "APPROXAMENTALY HOW MILES IS IT TO THE SUN?"} answers = { -- question 1 { "GRANT", "LINCON", "NOBODY", "WASHINGTON" }, -- question 2 { "5", "10", "25", "50", -- question 3 { "36", "35", "32", "40" }, -- question 4 "MIAMI", "ORLANDO", "JACKSONVILLE", "TALLAHASSEE" }, -- question 5 { "90,000,000", "89,000,000", "93,000,000", "91,000,000" } } correct = { "A", "B", "C", "D", "C" }
Then you could loop through them as such:
for i = 1 to length( questions ) do -- question[i] for j = 1 to length( answers[i] ) do -- answers[i][j] end for -- correct[i] end for
Just a tip.
9. Re: How do I get this program to ask more than one question?
- Posted by bubba Jan 05, 2009
- 2588 views
Thanks ghaberek, that looks better. I'll keep that in mind.
The questions and answers in the real program will be stored in a file. The file will be opened and just the questions will be read into an array. Then a randon question will be chosen and the answers for the question will be read from the file. I stripped all that out for the demo program I posted here.
10. Re: How do I get this program to ask more than one question?
- Posted by znorq2 Jan 06, 2009
- 2596 views
How about this;
atom the_question constant questionid = 1, answerid = 2, correctid = 3 constant questions = { { "QUESTION 1", {"Q1 ANSWER1", "Q1 ANSWER2", "Q1 ANSWER3", ...}, CORRECT_ANSWER_INDEX }, { "QUESTION 2", {"Q2 ANSWER1", "Q2 ANSWER2", "Q2 ANSWER3", "Q2 ANSWER4", ...}, CORRECT_ANSWER_INDEX }, .... }
In my opinion, this is much more flexible, but might be abit confusing.. Anyway, here you can have any amount of questions and answers you'd like.
Code for retriving the question and the correct answer would be (question 1);
the_question = 1 questions[the_question][questionid] = the question questions[the_question][answerid][1..$] = possible answers questions[the_question][answerid][questions[the_question][correctid]] = Correct answer
the_question is an atom refering to the question you're asking.
Kenneth / ZNorQ