Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Strange error, calculating highest, lowest, average - missing data

$
0
0
This is the strangest error I have encountered thus far in VB. I am writing a program that is to take input from a user, for the amount of annual rainfall. It should be able to accept decimals. My input method is InputBox. When a user clicks "Input" they're able to input the data. The data is stored into an array. Then it's paired with some additional information (including a string array) to output the data into a list box and some label controls.

It seems to be working fine, however it doesn't always display the average. Everything else works 100% of the time, but the average seems to only want to work with certain inputs.

Here's the code:
Public Class Form1
    Const intMONTHS As Integer = 12
    Dim strMonths() = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}
    Dim decRainfall(intMONTHS) As Decimal

    Sub GetRainfall(ByRef decRainfall() As Decimal)
        Dim intCount As Integer = 0 ' Loop Counter
        Do While intCount < intMONTHS
            Try
                decRainfall(intCount) = CDec(InputBox("Enter the amount of rainfall for month " &
                                                      strMonths(intCount).ToString()))
                intCount += 1
            Catch
                MessageBox.Show("There was an error with the input box")
            End Try
        Loop
    End Sub
    Private Function TotalRainfall(ByVal decRainfall() As Decimal) As Decimal
        Dim decTotal As Decimal = 0 ' Accumulator
        Dim intCount As Integer = 0 ' Loop Counter

        For intCount = 0 To (decRainfall.Length - 1)
            decTotal += decRainfall(intCount)
        Next
        Return decTotal
    End Function
    Private Function AverageRainfall(ByVal decRainfall() As Decimal) As Decimal
        '      Dim decTotal As Decimal = 0 ' Accumulator
        '       Dim intCount As Integer = 0 ' Loop Counter
        '        Dim decAverage As Decimal = 0   ' The Average of all the months

        '  For intCount = 0 To (decRainfall.Length - 1)
        '      decTotal += decRainfall(intCount)
        '  Next
        Return (TotalRainfall(decRainfall) / intMONTHS)
    End Function
    Private Function HighestRainfall(ByVal decRainfall() As Decimal) As Decimal
        Dim intCount As Integer = 0
        Dim decHighest As Decimal = 0
        decHighest = decRainfall(0)

        For intCount = 1 To (decRainfall.Length - 1)
            If decRainfall(intCount) > decHighest Then
                decHighest = decRainfall(intCount)
            End If
        Next
        Return decHighest
    End Function
    Private Function LowestRainfall(ByVal decRainfall() As Decimal) As Decimal
        Dim intCount As Integer = 0
        Dim decLowest As Decimal = 0
        decLowest = decRainfall(0)
        Do While intCount < intMONTHS
            Try
                If decLowest > decRainfall(intCount) Then
                    decLowest = decRainfall(intCount)
                End If
                intCount += 1
            Catch
                MessageBox.Show("There was a LOWEST error.")
            End Try
        Loop

        '        For intCount = 1 To (decRainfall.Length - 1)
        '            If decLowest > decRainfall(intCount) Then
        '                decLowest = decRainfall(intCount)
        '            End If
        '        Next
        Return decLowest
    End Function
    Private Sub btnInput_Click(sender As Object, e As EventArgs) Handles btnInput.Click
        GetRainfall(decRainfall)    ' Get input from the user
    End Sub

    Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
        Dim decTotalRainfall As Decimal = 0     ' Hold Total
        Dim decAverageRainfall As Decimal = 0   ' Hold Average
        Dim decLowestRainfall As Decimal = 0    ' Hold Lowest
        Dim decHighestRainfall As Decimal = 0   ' Hold Highest

        decTotalRainfall = CDec(TotalRainfall(decRainfall))
        decAverageRainfall = CDec(AverageRainfall(decRainfall))
        decLowestRainfall = CDec(LowestRainfall(decRainfall))
        decHighestRainfall = CDec(HighestRainfall(decRainfall))

        lblTotal.Text = "The total amount of rainfall was " & decTotalRainfall.ToString()
        lblAverage.Text = "The average amount of rainfall was " & decAverageRainfall.ToString()
        lblMinimum.Text = "The least amount of rainfall was " & decLowestRainfall.ToString()
        lblMaximum.Text = "The most amount of rainfall was " & decHighestRainfall.ToString()

        Dim intCount As Integer = 0

        lstOutput.Items.Add("Monthly Rainfall Input")
        lstOutput.Items.Add("----------------------------")
        Do While intCount < intMONTHS
            Try
                lstOutput.Items.Add(decRainfall(intCount) &
                                    " for " &
                                    strMonths(intCount))
                intCount += 1
            Catch
                MessageBox.Show("There was a listbox error.")
            End Try
        Loop
    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Close()
    End Sub

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        lstOutput.Items.Clear()
        lblTotal.Text = String.Empty
        lblAverage.Text = String.Empty
        lblMaximum.Text = String.Empty
        lblMinimum.Text = String.Empty
    End Sub
End Class



I've tried searching for Visual Basic Average problems, I haven't found anything that relates to the problem I'm experiencing.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>