I'm having that worst time trying to figure out what I am doing wrong. I've written a program that reads an input file containing daily pollution levels for the year 200 in Baltimore. The user should be able to click on one of three interface choices: Highest Pollution, Lowest Pollution, and Average Pollution. The only problem is it never selects from the text file, the answer always coms out to be a listing of everyday in the year with the level of 0, even the average comes out to 0. Here is the program I've written, please tell me what I'm doing wrong. I've also attached the text file.
Option Explicit
Dim arPollution(0 To 364) As Long
Private Sub cmdAvg_Click()
Dim lngCtr As Long
Dim lngSum As Long
Dim dblAvg As Double
picAnswer.Cls
lngSum = 0
For lngCtr = LBound(arPollution) To UBound(arPollution)
lngSum = arPollution(lngCtr) + lngSum
Next lngCtr
dblAvg = lngSum / (UBound(arPollution) + 1) picAnswer.Print "Average:" & Format(dblAvg, "00.000")
End Sub
Private Sub cmdHigh_Click()
Dim lngCtr As Long
Dim lngHigh As Long
Dim lngIdx As Long
Dim arHigh()
Dim strMsg As String
picAnswer.Cls
lngIdx = 0
lngHigh = 0
'First find the high pollution
For lngCtr = LBound(arPollution) To UBound(arPollution)
If arPollution(lngCtr) > lngHigh Then
lngHigh = arPollution(lngCtr)
End If
Next
'Now loop again finding all matching days
For lngCtr = LBound(arPollution) To UBound(arPollution)
If arPollution(lngCtr) = lngHigh Then
'2 dimensional array, 2 elements are DAY (position 0) and POLLUTION(position 1)
ReDim Preserve arHigh(2, lngIdx)
arHigh(0, lngIdx) = lngCtr
arHigh(1, lngIdx) = arPollution(lngCtr)
lngIdx = lngIdx + 1
End If
Next lngCtr
Private Sub cmdLow_Click()
Dim lngCtr As Long
Dim lngLow As Long
Dim lngIdx As Long
Dim arLow()
Dim strMsg As String
picAnswer.Cls
lngIdx = 0
lngLow = 999999
'First find the Low pollution
For lngCtr = LBound(arPollution) To UBound(arPollution)
If arPollution(lngCtr) < lngLow Then
lngLow = arPollution(lngCtr)
End If
Next
'Now loop again finding all matching days
For lngCtr = LBound(arPollution) To UBound(arPollution)
If arPollution(lngCtr) = lngLow Then
'2 dimensional array, 2 elements are DAY (position 0) and POLLUTION(position 1)
ReDim Preserve arLow(1, lngIdx)
arLow(0, lngIdx) = lngCtr
arLow(1, lngIdx) = arPollution(lngCtr)
lngIdx = lngIdx + 1
End If
Next lngCtr
Private Sub Form_Load()
Dim textline As String
Dim lngCtr As Long
lngCtr = 0
Open "pollution.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, textline
lngCtr = lngCtr + 1
Loop
Close #1
End Sub
First:
If you want the file to be publicly accessable: You have to put "Dim textline as String" at the very top of your code, in the Declarations section, and nowhere else...this will let textline be available to all of the subs and functions in your code. Do this with every variable that you want to be available to more than one part.
I haven't run it, but just by looking at it I can see one thing that'll screw everything else up. When you're inputing the text file, you keep inputing every line into the same variable, which means that if on one line you have "28" and on the next you have "31", you won't get "28 31", you'll only get "31"; that is, you'll always wind up with the last line of the file. Here's a solution:
Dim tmp as String
Open "pollution.txt" For Input As #1
Do While Not EOF(1)
Line Input #1, tmp
textline = textline & tmp ' This appends the current data onto the whole
lngCtr = lngCtr + 1
Loop
Close #1
Things I've Said:
"Life's funny like that...elephants can wear frilly lace panties, and Dubya still looks like a monkey in a big chair"
"Take four goats and strap one to each foot of a llama. Presto, goat-powered llama!"
"You want to get me to work more, get me a Coke. No? Then deal with inferior garbage, I'm not coding another line and your clients can go to......thanks, I'd love a Coke right about now!"