Donate to us via Paypal!
Become the change you seek in the world. - Gandhi
Sunday, February 28, 2021
Navigation
Home
 Find:
 Information:
Learn
Communicate
Submit
Shop
Challenges
 Exploit:
 Programming:
 Think:
 Track:
 Patch:
 Other:
 Need Help?
Other
Members Online
Total Online: 136
Guests Online: 120
Members Online: 16

Registered Members: 133756
Newest Member: Cleaners01
Latest Articles

View Thread

HellBound Hackers | Computer General | Programming

Author

Alright another schism in my code

LordChiron
Member



Posts: 7
Location:
Joined: 23.12.13
Rank:
HBH Guru
Posted on 20-02-15 00:47
PHP
  1. <?php
  2. Private Sub rbs_CheckedChanged(sender As Object, e As EventArgs) Handles rb4.CheckedChanged, rb3.CheckedChanged, rb2.CheckedChanged, rb1.CheckedChanged
  3.         'when we call LoadQuestion we don't want any of these events to trigger
  4.         If Not updatingQuestion Then
  5.             Dim rb = DirectCast(sender, RadioButton)
  6.             Dim qandaAnswer = curQandA.Answers.Where(Function(a) a.AnswerText = rb.Text).FirstOrDefault
  7.             If Not qandaAnswer Is Nothing Then qandaAnswer.AnswerChecked = True
  8.             For Each ans In curQandA.Answers
  9.                 If ans Is qandaAnswer Then
  10.                     ans.AnswerChecked = True
  11.                 Else
  12.                     ans.AnswerChecked = False
  13.                 End If
  14.             Next
  15.         End If
  16.     End Sub
  17. ?>


Ok, rb.Text needs to match the answer. my text file gets parsed with this:

PHP
  1. <?php
  2. Private Sub loadme() Handles Me.Load
  3.         Using sr As New StreamReader("C:\Quiz\questions.txt")
  4.             While Not sr.EndOfStream
  5.                 Dim data() = sr.ReadLine.Split(","c)      // parsing
  6.                 Dim qAndA As New QuestionWithAnswers
  7.                 qAndA.ItemNumber = Integer.Parse(data(0))
  8.                 qAndA.Question = data(1)
  9.                 Dim answers() = data(2).Split("|"c)     // parsing
  10.                 For Each answer In answers
  11.                     qAndA.Answers.Add(New QuestionWithAnswers.Answer With {.AnswerText = answer, .AnswerChecked = False})
  12.                 Next
  13.                 qAndA.CorrectAnswer = data(3)    //  holds value of answer 
  14.                 AllQuestions.Add(qAndA)
  15.             End While
  16.         End Using
  17.         // get the first question qIndex starts at 0 - first element
  18.         curQandA = AllQuestions.ElementAt(qIndex)
  19.         LoadQuestion()
  20.     End Sub
  21. ?>


this is my text file format:

PHP
  1. <?php
  2.  1,question, answer|answer|answer|answer, correct answer
  3. ?>


This is how my radiobuttons are setup:

PHP
  1. <?php
  2.  Private Sub LoadQuestion()
  3.         If AllQuestions.Count = 0 Then Exit Sub                                     // little protection
  4.         updatingQuestion = True
  5.         curQandA = AllQuestions.ElementAt(qIndex)
  6.         lbQuestion.Text = curQandA.Question                                        // displays question
  7.         rb1.Text = curQandA.Answers.ElementAt(0).AnswerText                // displays answer
  8.         rb2.Text = curQandA.Answers.ElementAt(1).AnswerText             
  9.         rb3.Text = curQandA.Answers.ElementAt(2).AnswerText
  10.         rb4.Text = curQandA.Answers.ElementAt(3).AnswerText
  11.         rb1.Checked = curQandA.Answers.ElementAt(0).AnswerChecked       // pick an answer
  12.         rb2.Checked = curQandA.Answers.ElementAt(1).AnswerChecked 
  13.         rb3.Checked = curQandA.Answers.ElementAt(2).AnswerChecked
  14.         rb4.Checked = curQandA.Answers.ElementAt(3).AnswerChecked
  15.         updatingQuestion = False
  16.     End Sub
  17. ?>


This is my message box saying how many right out of number of questions:

PHP
  1. <?php
  2.  Private Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
  3.         Dim correctAnswers As Integer = 0
  4.         For Each question As QuestionWithAnswers In AllQuestions
  5.             For Each answer As QuestionWithAnswers.Answer In question.Answers
  6.                 If answer.AnswerChecked AndAlso answer.AnswerText = question.CorrectAnswer Then    // check answer against correct answer
  7.                     correctAnswers += 1    // increment correct answer
  8.                     Exit For
  9.                 End If
  10.             Next
  11.         Next
  12.         MessageBox.Show(String.Format("You have correctly answered {0} out of {1} questions!", correctAnswers, AllQuestions.Count.ToString))
  13.     End Sub
  14. ?>


Now when I go to submit my answers whether I submit 1 answer or all answers at the end....the only answers I get right are the answers
that are in the first slot of my text file. If the correct answer is not at the front slot then it would read it as a correct answer.

example:
rb1 is checked which is Monday
1,what is today?, Monday|Tuesday|Wednesday|Thursday, Monday <-------This will show a correct answer and give a point

rb2 is checked which is Monday
2,what is today?, Tuesday|Monday|Wednessday|Thursday, Monday <------This will not register as a correct answer even though I checked the right radio button.

A little confused, could someone help me find the issue? Thanks!
Author

RE: Alright another schism in my code

LordChiron
Member



Posts: 7
Location:
Joined: 23.12.13
Rank:
HBH Guru
Posted on 21-02-15 01:35
Found the schism...it was with my questions.txt format.

I changed it to:
PHP
  1. <?php
  2. 1,question,answer|answer|answer|answer,correct answer
  3. ?>

as opposed to this:
PHP
  1. <?php
  2. 1,question, answer|answer|answer|answer, correct answer
  3. ?>

problem solved - quiz works like a charm - took out the white spaces in my change.
Thanks for all who had a look!