Results 1 to 10 of 10

Thread: [RESOLVED] Writing average to TXT

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    22

    Resolved [RESOLVED] Writing average to TXT

    I need some help writing ten(10) input numbers to a text file, and displaying the average in the program, with the two lowest numbers dropped. The project is supposed to allow a user to input 10 number scores and the program will display the average of the scores with the 2 lowest dropped, and write the 10 scores to a text file to display.
    The problem is I cant get it to display the average correctly
    Here is my code so far

    Any help is appreciated

    Code:
        Private Sub btnEnterTemp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnterTemp.Click
            '   This button accepts and displays up to 10 Scores from user
            '   then calculates and displays the average score, with 2 lowest dropped, and writes to a TXT file
    
            Dim strScores As String
    
            Dim decAverage As Decimal
            Dim decTotalOfAllScores As Decimal = 0
            Dim strInputMessage As String = "Enter Project score #"
            Dim strInputHeading As String = "Project Score"
            Dim strNormalMessage As String = "Enter the Project Score #"
            Dim strNonNumericError As String = "Error- Enter a Number for Project #"
            Dim strNegativeError As String = "Error- Enter a Positive Number  #"
    
            Dim strProjectScore(10) As String
    
    
            Dim objWriter As New IO.StreamWriter("scores.txt")
            Dim intCount As Integer
    
            For intCount = 0 To (strProjectScore.Length - 1)
                strProjectScore(intCount) = InputBox("Please enter score")
    
                If IO.File.Exists("scores.txt") Then
                    objWriter.WriteLine(strProjectScore(intCount))
    
                Else
                    MsgBox("File not available")
                    Close()
    
                End If
            Next
            objWriter.Close()
    
    
            ' Loop Variables
    
            Dim strCancelClick As String = ""
            Dim intMaxNumberOfEntries As Integer = 10
            Dim intNumberOfEntries As Integer = 1
    
            ' This loop allows the user to enter up to 10 grades.
            ' The loop terminates when the user has entered 10 grades or the user 
            ' clicks the Cancel or Close Button in the input box.
    
            strScores = InputBox(strInputMessage & intNumberOfEntries, strInputHeading, "")
    
            'Makes label visible
            lblAverageTemps.Visible = True
    
            ' Calculates and displays the Average Scores
            If intCount > 1 Then
                decAverage = decTotalOfAllScores / (intNumberOfEntries - 1)
                lblAverageTemps.Text = "Average project score is  " & _
                    decAverage.ToString("F1")
            Else
                lblAverageTemps.Text = "No score Entered"
            End If
    
            'Disables Enter Temp Button
            btnEnterTemp.Enabled = False
        End Sub

  2. #2
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Writing average to TXT

    Here ya go:
    Code:
            Dim marks As New List(Of Single)
            marks.Add(7.9)
            marks.Add(4.6)
            marks.Add(9.2)
            marks.Add(3.1)
            marks.Add(5.4)
            'etc
            'remove lowest values (sort and remove first two)
            marks.Sort()
            marks.RemoveRange(0, 2)
            'get average
            Dim avg As Single
            For Each mark As Single In marks
                avg += mark / marks.Count
            Next
            MsgBox(avg)
    First you make sure the "Marks" list gets filled with the (10 or more) marks.
    Then you sort the marks from low to high
    Then you remove the first two marks
    Then you get the average

    To write to a text file use a streamwriter:
    Code:
            Dim s As New IO.StreamWriter("mymarks.txt", True)
            For Each m As Single In marks
                s.WriteLine(m)
            Next
            s.WriteLine("Average: " & avg)
            s.Close()
    To display all marks, place the For write loop before the "RemoveRange" and, if you want to leave the marks unsorted, before the "Sort()" line.
    You can always add the stream declaration on top of everything and write lines in-code. Just make sure you close the stream in the end.

  3. #3
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Writing average to TXT

    i'd do it differently:

    vb Code:
    1. Public Class Form1
    2.  
    3.     Dim numbers As List(Of Integer)
    4.  
    5.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    6.         numbers = New List(Of Integer)(9)
    7.         For x As Integer = 1 To 10
    8.             Dim number As Integer
    9.             If Integer.TryParse(InputBox("Enter an Integer"), number) Then
    10.                 numbers.Add(number)
    11.             Else
    12.                 x -= 1
    13.             End If
    14.         Next
    15.         numbers.Sort()
    16.         IO.File.WriteAllLines("numbers.txt", Array.ConvertAll(numbers.ToArray, Function(i) i.ToString))
    17.         Process.Start("numbers.txt")
    18.         MsgBox(numbers.Skip(2).Average)
    19.     End Sub
    20.  
    21. End Class

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    22

    Re: Writing average to TXT

    I'm trying to make this simpler for me to understand. I already have a program that when I press a button, it asks for 10 scores and when you enter each one, it populates a Listbox, then on the last score entered, it shows the average in a LabelObject. I need to drop the 2 lowest from the equation and write all 10 to a TXT

    Is there a way to write the objects in a Listbox to a TXT file?

  5. #5
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Writing average to TXT

    Yep, I guess using:
    Code:
    numbers.Skip(2).Average
    is a bit more clear, although I guess marks are given with decimals, so not sure about using an integer. Used the loop to demonstrate "calculating an average" btw.

    EDIT

    I guess you can use the "writealllines" function, but I never really liked that function. (there is no moment of closing visible)
    Any ways, a listbox contains an "Items()" property, you can use that to write out all items:
    Code:
            For Each mark As String In mymarklistbox.Items
                s.WriteLine(mark)
            Next
    This also seems possible, but not sure if it works:
    Code:
    s.WriteLine(mymarklistbox.Items)
    Or may be there is some sort of build-in function for it, but I rather stay with writing from a loop.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    22

    Re: Writing average to TXT

    Excellent help, this is EXACTLY what I needed!

    Thanks for quick responses

    Only thing is that I still need to compute the average

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Writing average to TXT

    to write your listbox items:

    vb Code:
    1. IO.File.WriteAllLines("numbers.txt", Array.ConvertAll(ListBox1.Items.Cast(Of Object).ToArray(), Function(o) o.ToString))

  8. #8
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,422

    Re: Writing average to TXT

    Quote Originally Posted by bergerkiller View Post
    Yep, I guess using:
    Code:
    numbers.Skip(2).Average
    is a bit more clear
    yep

    Quote Originally Posted by bergerkiller View Post
    not sure about using an integer.
    that's easily changed. it wasn't intended as a copy + paste instant fix.
    you learn far more reading + understanding the code.


    Quote Originally Posted by bergerkiller View Post
    I guess you can use the "writealllines" function.
    see my previous post


    Quote Originally Posted by bergerkiller View Post
    This also seems possible, but not sure if it works:

    Code:
    s.WriteLine(mymarklistbox.Items)
    it doesn't

  9. #9
    Fanatic Member
    Join Date
    Jul 2009
    Posts
    629

    Re: Writing average to TXT

    Only thing is that I still need to compute the average
    That was dealt with a few posts ago: store your marks in a list (of single/decimal/integer/double/your choice) and use:
    Code:
    Dim average As Single = marks.Skip(2).Average

  10. #10

    Thread Starter
    Junior Member
    Join Date
    Feb 2011
    Posts
    22

    Re: Writing average to TXT

    Yes, got it working.

    Thanks again

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