beginnerprogrammer
beginnerprogrammer

Reputation: 183

sequential access file in vb

I am working on a program in Visual Basic that incorporates using a sequential access file for a ballot type program. Each type the user clicks the save vote button, the amount of votes gets stored. What I am trying to do is in my access file is to display the number of votes as an actual number. The way my program is written right now, the name of the candidate appears as many times as the vote was saved. for example, if perez was voted for 4 times, in the access file perez is displayed on 4 different lines. How do I display the actual number of how many time they were voted for. I'm thinking using a counter variable, but not really sure how to really implement it in. this is what i have so far.

Public Class Voter

Dim file As IO.File

Dim infile As IO.StreamReader

Dim outfile As IO.StreamWriter



Private Sub Voter_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    outfile = IO.File.CreateText("Votes.txt")
End Sub

Private Sub btnVote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVote.Click
    lstResult.Items.Clear()
    'which buttons is clicked
    If radMark.Checked = True Then
        outfile.WriteLine(radMark.Text)
        radMark.Checked = False
    ElseIf radSheima.Checked = True Then
        outfile.WriteLine(radSheima.Text)
        radSheima.Checked = False
    ElseIf radSam.Checked = True Then
        outfile.WriteLine(radSam.Text)
        radSam.Checked = False
    Else
        MessageBox.Show("You should select one among them")
    End If
End Sub

Private Sub btnResult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResult.Click
    'Dim Mark, Sheima, Sam As Integer
    Dim Mark As Integer = 0
    Dim Sheima As Integer = 0
    Dim Sam As Integer = 0
    Dim name As String
    'Mark = Sheima = Sam = 0
    outfile.Close()
    infile = IO.File.OpenText("Votes.txt")
    'keep track of votes
    While Not infile.EndOfStream
        name = infile.ReadLine()
        If name.Equals("Mark Stone") Then
            Mark += 1
        ElseIf name.Equals("Sheima Patel") Then
            Sheima += 1
        Else
            Sam += 1
        End If
    End While
    'results
    lstResult.Items.Clear()
    lstResult.Items.Add("Mark Stone     " & CStr(Mark))
    lstResult.Items.Add("Shemia Patel   " & CStr(Sheima))
    lstResult.Items.Add("Sam Perez      " & CStr(Sam))
    infile.Close()
End Sub

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    Me.Close()

End Sub
End Class

Upvotes: 0

Views: 2268

Answers (1)

Nick
Nick

Reputation: 1128

In your btnVote_Click function you are writing the radiobutton text into the file everytime you click on it, that's how the issue to be created.

Also you are counting how many times the name appears in the file as the number of vote but not the number.

You should try putting the name and count in your vote file rather than just the name, e.g.

Mark,1
Sheima,2
Same,5

Then when you click Vote button, you should read the number for Mark, increment by 1 and write it back.

Try this,

Private Sub btnVote_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnVote.Click
    lstResult.Items.Clear()
    'which buttons is clicked
    If radMark.Checked = True Then
        AddCount(radMark.Text)
        radMark.Checked = False
    ElseIf radSheima.Checked = True Then
        AddCount(radSheima.Text)
        radSheima.Checked = False
    ElseIf radSam.Checked = True Then
        AddCount(radSam.Text)
        radSam.Checked = False
    Else
        MessageBox.Show("You should select one among them")
    End If
End Sub

Private Sub btnResult_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnResult.Click

    'results
    lstResult.Items.Clear()
    lstResult.Items.Add("Mark Stone     " & GetCount(radMark.Text))
    lstResult.Items.Add("Shemia Patel   " & CStr(radSheima.Text))
    lstResult.Items.Add("Sam Perez      " & CStr(radSam.Text))

End Sub

Private Sub AddCount(Byval VoteName As String)

    infile = New IO.File.StreamReader("Votes.txt")

    Dim whole_line As String
    Dim person As String
    Dim vote_count As Integer
    Dim found as boolean

    'read through the whole file until the end or find the name
    Do While infile.Peek() >= 0 And person <> VoteName

        'read each line
        whole_line = infile.ReadLine

        person = Split(whole_line, ",")(0)

        If person = VoteName Then

            vote_count = Split(whole_line, ",")(1)


            found = True
        End If
    Loop

    'Close the file after it is used.
    infile.Close()


    'Reopen the file with the StreamWriter
    outfile = IO.File.OpenText("Votes.txt")

    If found = True Then
        'the line will only be replace if the person name is found in the line
        whole_line = Whole_line.replace(VoteName & "," & vote_count, VoteName & "," & vote_count + 1)

        'Write back into the file
        outfile.WriteLine(whole_line)
    Else
        'if the vote name is not found in the file
        'Then create a new one
        outfile.WriteLine(VoteName & ",1" & VbCrLf)
    End If

        outfile.Close()

End Sub

Private Function GetCount(Byval VoteName As String)
    infile = New IO.File.StreamReader("Votes.txt")      

    Dim whole_line As String
    Dim person As String
    Dim vote_count As Integer

    'read through the whole file
    Do While infile.Peek() >= 0 And person <> VoteName

        'read each line
        whole_line = infile.ReadLine

        person = Split(Whole_line, ",")(0)

        If person = VoteName Then
            vote_count = Split(whole_line, ",")(1)
        End If
    Loop

    infile.Close()
    Return vote_count
End Sub

Upvotes: 2

Related Questions