CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2010
    Posts
    7

    Help with list and loop?

    New to programming and looking for some help. I need this program to allow me to enter 12 temperatures and give me an average. I have everything working correctly ( I think) except where the average should be I get 0.0.

    Not sure if it matters but I'm using VB 2005.

    Code:
    Option Strict On
    
    Public Class frmBoard
    
        
        Private Sub frmBoard_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    
        End Sub
    
        Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click
    
            Dim strTemperature As String
            Dim decTemperature As Decimal
            Dim decAverageTemperature As Decimal
            Dim decTotalOfAllTemperatures As Decimal = 0D
            Dim strInputBoxMessage As String = "Enter the temperature for the month #"
            Dim strInputBoxHeading As String = "Monthly Temperature"
            Dim strNormalBoxMessage As String = "Enter the temperature for the month #"
            Dim strNonNumericErrorMessagae As String = "Error - Enter a number for the temperature"
            Dim strNegativeNumberErrorMessage As String = "Error - Enter a postive number for the temperature"
    
            ' Declare and initialize loop variables
            Dim strCancelButtonClicked As String = ""
            Dim intMaximumNumberOfEntries As Integer = 12
            Dim intNumberOfEntries As Integer = 1
    
            ' This loop allows the user to enter the temperature of up to 12 months.
            ' The loop terminates when the user has entered 12 temperatures or the user
            ' clicks the Cancel button or the Close button in the InputBox
    
            strTemperature = InputBox(strInputBoxMessage _
             & intNumberOfEntries, strInputBoxHeading, " ")
    
            Do Until intNumberOfEntries > intMaximumNumberOfEntries _
             Or strTemperature = strCancelButtonClicked
    
                If IsNumeric(strTemperature) Then
                    decTemperature = Convert.ToDecimal(strTemperature)
                    If decTemperature > 0 Then
                        Me.lstTemp.Items.Add(decTemperature)
                        decTotalOfAllTemperatures += decTemperature
                        intNumberOfEntries += 1
                        strInputBoxMessage = strNormalBoxMessage
                    Else
                        strInputBoxMessage = strNegativeNumberErrorMessage
                    End If
                Else
                    strInputBoxMessage = strNonNumericErrorMessagae
                End If
    
                If intNumberOfEntries <= intMaximumNumberOfEntries Then
                    strTemperature = InputBox(strInputBoxMessage _
                        & intNumberOfEntries, strInputBoxHeading, " ")
    
                End If
    
            Loop
    
    
            ' Makes Label Visible
            Me.lblAverageTemperature.Visible = True
    
            ' Calculates and displays average temperature
            If intNumberOfEntries > 1 Then
                decTemperature = decTotalOfAllTemperatures / (intNumberOfEntries - 1)
                Me.lblAverageTemperature.Text = "Average Temperature is " & _
                    decAverageTemperature.ToString("F1") & " degrees"
            Else
                Me.lblAverageTemperature.Text = "No Temperature Entered"
            End If
    
            ' Disables the Enter Temperature button
            Me.btnEnter.Enabled = False
    
        End Sub
    
        Private Sub mnuClearItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuClearItem.Click
            Me.lstTemp.Items.Clear()
            Me.lblAverageTemperature.Visible = False
            Me.btnEnter.Enabled = True
    
        End Sub
    
        Private Sub mnuExitItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles mnuExitItem.Click
            Me.Close()
    
        End Sub
    End Class

  2. #2
    Join Date
    Jan 2006
    Location
    Fox Lake, IL
    Posts
    15,007

    Re: Help with list and loop?

    I'd start again. If you STEP THRU the program, you should see the problem. Press F8 a few times, enter a temp, press F8 a few more times, and use the mouse to hover over variables. Or Print out temp results to make sure they are correct. Debugging is the most important tool in the arsenal, ya know?
    David

    CodeGuru Article: Bound Controls are Evil-VB6
    2013 Samples: MS CODE Samples

    CodeGuru Reviewer
    2006 Dell CSP
    2006, 2007 & 2008 MVP Visual Basic
    If your question has been answered satisfactorily, and it has been helpful, then, please, Rate this Post!

  3. #3
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: Help with list and loop?

    At a quick glance
    Code:
    If intNumberOfEntries > 1 Then
                decTemperature = decTotalOfAllTemperatures / (intNumberOfEntries - 1)
                Me.lblAverageTemperature.Text = "Average Temperature is " & _
                    decAverageTemperature.ToString("F1") & " degrees"
            Else
                Me.lblAverageTemperature.Text = "No Temperature Entered"
            End If
    Notice the issue?
    Always use [code][/code] tags when posting code.

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

Featured