CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jan 2009
    Posts
    2

    [2008] Quiz Program array

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: [2008] Quiz Program array

    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.

    Code:
    '  This is a code tag
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jun 2005
    Location
    JHB South Africa
    Posts
    3,772

    Re: [2008] Quiz Program array

    A few things before we continue ....

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

    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 ..

    Quote Originally Posted by siradammeathead View Post
    I am looking to declare an array of 20 intergers to store the answers to the questions....
    I can see you're doing that..
    Code:
    Dim answers(19) As Integer
    Quote Originally Posted by siradammeathead View Post
    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 here
    Code:
    If Not ((txtAnswer.Text = "1" And RadioButton1.Checked) Or (txtAnswer.Text = "2" And RadioButton2.Checked) _
    Or (txtAnswer.Text = "3" And RadioButton3.Checked)) Then
    Quote Originally Posted by siradammeathead View Post
    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 too
    Code:
    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()
    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..
    Articles VB6 : Break the 2G limit - Animation 1, 2 VB.NET : 2005/8 : Moving Images , Animation 1 , 2 , 3 , User Controls
    WPF Articles : 3D Animation 1 , 2 , 3
    Code snips: VB6 Hex Edit, IP Chat, Copy Prot., Crop, Zoom : .NET IP Chat (V4), Adv. ContextMenus, click Hotspot, Scroll Controls
    Find me in ASP.NET., VB6., VB.NET , Writing Articles, My Genealogy, Forum
    All VS.NET: posts refer to VS.NET 2008 (Pro) unless otherwise stated.

  4. #4
    Join Date
    Jan 2009
    Posts
    2

    Re: [2008] Quiz Program array

    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?
    Code:
    Sub Main()
    For i As Integer = 0 To 19
    Do
    answers(i) = Console.ReadLine()
    
    Loop Until answers(i) > 0
    Next
    Last edited by siradammeathead; January 30th, 2009 at 12:07 PM.

  5. #5
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: [2008] Quiz Program array

    You can edit your first post, to add code tags. What that doesn't do is allow the user to stop the loop early.

    Code:
    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...
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured