Results 1 to 4 of 4

Thread: Adding values in a textfile

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    23

    Adding values in a textfile

    I'm trying to multiple two values in a textfile together and then add all the totals together into one value.
    This is my form:
    Name:  Untitled.jpg
Views: 329
Size:  16.8 KB
    When someone clicks the "Values" in the menu bar, a drop down menu pops up and shows 3 options: all, non fic, or fic.

    This is my textfile:
    Left Behind, Lahaye, F, 7, 11.25
    A Tale of Two Cities, Dickens, F, 100, 8.24
    Hang a Thousand Trees with Ribbons, Rinadi, F, 30, 16.79
    Saffy's Angel, McKay, F, 20, 8.22
    Each Little Bird that Sings, Wiles, F, 10, 7.70
    Abiding in Christ, Murray, N, 3, 12.20
    Bible Prophecy, Lahaye and Hindson, N, 5, 14.95
    Captivating, Eldredge, N, 12, 16.00
    Growing Deep in the Christian Life, Swindoll, N, 11, 19.95
    Prayers that heal the Heart, Virkler, N, 4, 12.00
    Grow in Grace, Ferguson, N, 3, 11.95
    The Good and Beautiful God, Smith, N, 7, 11.75
    Victory Over the Darkness, Anderson, N, 12, 16.00

    Depending on which one the person clicks in the dropdown menu, the total value of either all the books, all the nonfic books, or all the fic books will be displayed. I'm trying the "If...Then" and at first all my values were displaying as 0, but now it wont't even finish debugging. Any ideas?

    Code:
    Private Sub menuValues_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles menuValues.Click
    Dim inventory() As inspecs
    Dim books() As String = File.ReadAllLines("Books.txt")
    Array.Sort(books)
    Dim n As Integer = books.Count - 1
    ReDim inventory(n)
    Dim line As String
    Dim data() As String


    For i As Integer = 0 To n
    line = books(i)
    data = line.Split(","c)
    inventory(i).title = data(0)
    inventory(i).author = data(1)
    inventory(i).category = data(2)
    inventory(i).stock = CDbl(data(3))
    inventory(i).price = CInt(data(4))
    Next
    Dim nfv As Integer
    menuValuesNonFic.Checked = True
    menuValuesAll.Checked = False
    menuValuesFic.Checked = False
    If menuValuesNonFic.Checked = True Then
    If data(2) = "N" Then
    nfv = (data(3) * data(4))
    End If
    MessageBox.Show(FormatCurrency("Values: " & nfv))
    End If
    Dim ficv As Integer
    If menuValuesNonFic.Checked = False Then
    If menuValuesFic.Checked = True Then
    If data(2) = "F" Then
    ficv = (data(3) * data(4))
    End If
    MessageBox.Show(FormatCurrency("Values: " & ficv))
    End If
    End If
    Dim allv As Integer
    If menuValuesNonFic.Checked = False Then
    If menuValuesAll.Checked = True Then
    allv = (data(3) * data(4))
    MessageBox.Show(FormatCurrency("Values: " & allv))
    End If
    End If
    End Sub

    I'm new to Visual basic so any help would be appreciated!

  2. #2
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Adding values in a textfile

    One problem is you have the conversions for data(3) & (4) backwards, you are converting the prices data(4) as Integers so for example "11.25" would be counted as only "11".

    quick test...
    Code:
    Dim books() As String = IO.File.ReadAllLines("Books.txt")
    Dim data() As String
    Dim allBooksCost, allNonFictCost, allFictionCost As Double
    
    For i As Integer = 0 To books.Count - 1
        data = books(i).Split(","c)
        If data(2).Trim = "N" Then
            allNonFictCost += (CInt(data(3)) * CDbl(data(4)))
        ElseIf data(2).Trim = "F" Then
            allFictionCost += (CInt(data(3)) * CDbl(data(4)))
        End If
    Next
    
    ' all books cost
    allBooksCost = allFictionCost + allNonFictCost
    
    ' show result
    If menuValuesAll.Checked Then
        MessageBox.Show("All books: " & FormatCurrency(allBooksCost))
    ElseIf menuValuesFic.Checked Then
        MessageBox.Show("All Fiction: " & FormatCurrency(allFictionCost))
    ElseIf menuValuesNonFic.Checked Then
        MessageBox.Show("All Non-Fiction: " & FormatCurrency(allNonFictCost))
    End If

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Apr 2013
    Posts
    23

    Re: Adding values in a textfile

    Thank you! It works! I definitely was doing it the hard way!

  4. #4
    Junior Member
    Join Date
    Jul 2013
    Posts
    19

    Re: Adding values in a textfile

    i figured it out

Tags for this Thread

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