Click to See Complete Forum and Search --> : [2008] Quiz Program array


siradammeathead
January 29th, 2009, 07:54 AM
I am looking to declare an array of 20 intergers to store the answers to the questions and then for the next button i am looking to get the answer and write it to the array. If one radio button is checked then i want it to write 1 to the array. Then finally i want to write the username, the array of answers and score to the txt file. How can I do that?

below is my code:

Imports System
Imports System.IO
Public Class Question

Dim answers(19) As Integer
Sub Main()
For i As Integer = 0 To 19
Do
answers(i) = Console.ReadLine()

Loop Until answers(i) > 0
Next

End Sub
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click

If Not ((txtAnswer.Text = "1" And RadioButton1.Checked) Or (txtAnswer.Text = "2" And RadioButton2.Checked) _
Or (txtAnswer.Text = "3" And RadioButton3.Checked)) Then

'take one live away
lives -= 1
score = (correct * 2) + lives
lblScore.Text = score
txtLives.Text = lives

Else
'if correct +1 to score
correct += 1
score = (correct * 2) + lives
lblScore.Text = score

End If

If lives = 0 Or quizcurrencymanager.Position >= quizcurrencymanager.Count - 1 Then
Dim fw As StreamWriter
Dim readstring As String
Dim readstring2 As String
fw = New StreamWriter("J:\quizfile.txt", True)
fw.Write(ControlChars.CrLf)
readstring = Intro.txtName.Text
readstring2 = lblScore.Text
fw.Write(readstring)
fw.Write(" Score: ")
fw.Write(readstring2)
fw.Close()
If MessageBox.Show(Intro.txtName.Text & " you scored " & score & " points", "Do you wish to try again?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, _
MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.No Then

End
Else
'restart quiz
Close()
Intro.Show()
Intro.txtName.Clear()
Intro.txtName.Focus()
quizcurrencymanager.Position = 0
lblScore.Text = ""

End If
Else
quizcurrencymanager.Position += 1
End If


End Sub
Private WithEvents quizcurrencymanager As CurrencyManager
Private lives As Integer, correct As Integer
Public score As Integer

Private Sub Question1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'TODO: This line of code loads data into the 'QuestionsDataSet.Questions' table. You can move, or remove it, as needed.
Me.QuestionsTableAdapter.Fill(Me.Questio...
quizcurrencymanager = DirectCast(Me.BindingContext(QuestionsBi... CurrencyManager)

lives = 3
score = 0
correct = 0

txtLives.Text = lives
lblScore.Text = score


End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click
If MessageBox.Show("Are you sure?", "Exit?", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then

End
End If
End Sub
End Class

dglienna
January 29th, 2009, 06:18 PM
Sounds like homework (and another lotto wheel program this past week)

I answered pretty much the same way.

If you can't use CODE TAGS so that we can see what you are doing, then we can't really help you. You have to help yourself.


' This is a code tag

GremlinSA
January 30th, 2009, 01:00 AM
A few things before we continue ....

1) Please use code tags when posting code... Your Code Here

2) Be more specific about what problem your having .. It's doesn't help saying "it's not working", Tell us what is it doing, or not doing.. what line of code gives an error, and exactly what error your getting ..

I am looking to declare an array of 20 intergers to store the answers to the questions....I can see you're doing that..Dim answers(19) As Integer
and then for the next button i am looking to get the answer and write it to the array. If one radio button is checked then i want it to write 1 to the array. You doing something like that hereIf Not ((txtAnswer.Text = "1" And RadioButton1.Checked) Or (txtAnswer.Text = "2" And RadioButton2.Checked) _
Or (txtAnswer.Text = "3" And RadioButton3.Checked)) Then

Then finally i want to write the username, the array of answers and score to the txt file. How can I do that? Again it looks like your doing something like that toofw = New StreamWriter("J:\quizfile.txt", True)
fw.Write(ControlChars.CrLf)
readstring = Intro.txtName.Text
readstring2 = lblScore.Text
fw.Write(readstring)
fw.Write(" Score: ")
fw.Write(readstring2)
fw.Close()

So looking at what you posted you've already answered your question..

If you are more specific, we can be more specific in helping you..

Gremmy..

siradammeathead
January 30th, 2009, 09:58 AM
Hi sorry i will use comment tags in my program in the future, i just haven't needed to use a forum like this before and all the things required in it. I've only been learning VB for 3/4 months and this is not really homework per se, its was a college exercise generally for problem solving, storyboarding and writing structured english for software development. I kind of guessed i have nearly got it working correctly. However, i am struggling to incoporating the array (answers) into the how the radio buttions are checked, this what i was needing help in just and advice if had the right structure, as i have looked at examples of arrays all over the internet and after trying a few different ways i was getting lost.


Like does this code below look ok for what i am trying to do and in the correct position?
Sub Main()
For i As Integer = 0 To 19
Do
answers(i) = Console.ReadLine()

Loop Until answers(i) > 0
Next

dglienna
January 30th, 2009, 01:49 PM
You can edit your first post, to add code tags. What that doesn't do is allow the user to stop the loop early.

Sub Main()

For i As Integer = 0 To 19
Dim Ans as String = ""
Do
Ans = Console.ReadLine()
if Ans = "" then exit for
answers(i) = Ans.ToInt
Loop Until answers(i) > 0
Next


of course, you could remove the counter, and just keep going...