|
-
Apr 3rd, 2011, 10:12 AM
#1
Thread Starter
Junior Member
[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
-
Apr 3rd, 2011, 10:25 AM
#2
Fanatic Member
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.
Last edited by bergerkiller; Apr 3rd, 2011 at 10:29 AM.
-
Apr 3rd, 2011, 10:37 AM
#3
Re: Writing average to TXT
i'd do it differently:
vb Code:
Public Class Form1
Dim numbers As List(Of Integer)
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
numbers = New List(Of Integer)(9)
For x As Integer = 1 To 10
Dim number As Integer
If Integer.TryParse(InputBox("Enter an Integer"), number) Then
numbers.Add(number)
Else
x -= 1
End If
Next
numbers.Sort()
IO.File.WriteAllLines("numbers.txt", Array.ConvertAll(numbers.ToArray, Function(i) i.ToString))
Process.Start("numbers.txt")
MsgBox(numbers.Skip(2).Average)
End Sub
End Class
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 3rd, 2011, 10:42 AM
#4
Thread Starter
Junior Member
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?
-
Apr 3rd, 2011, 10:42 AM
#5
Fanatic Member
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.
Last edited by bergerkiller; Apr 3rd, 2011 at 10:47 AM.
-
Apr 3rd, 2011, 10:46 AM
#6
Thread Starter
Junior Member
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
-
Apr 3rd, 2011, 10:50 AM
#7
Re: Writing average to TXT
to write your listbox items:
vb Code:
IO.File.WriteAllLines("numbers.txt", Array.ConvertAll(ListBox1.Items.Cast(Of Object).ToArray(), Function(o) o.ToString))
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 3rd, 2011, 10:59 AM
#8
Re: Writing average to TXT
 Originally Posted by bergerkiller
Yep, I guess using:
Code:
numbers.Skip(2).Average
is a bit more clear
yep
 Originally Posted by bergerkiller
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.
 Originally Posted by bergerkiller
I guess you can use the "writealllines" function.
see my previous post
 Originally Posted by bergerkiller
This also seems possible, but not sure if it works:
Code:
s.WriteLine(mymarklistbox.Items)
it doesn't
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Apr 3rd, 2011, 12:24 PM
#9
Fanatic Member
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
-
Apr 3rd, 2011, 12:27 PM
#10
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|