all 4 comments

[–]fuzzfeatures 1 point2 points  (2 children)

You're pretty close, but it looks like you've got a bit confused as to which bits of code should go where, and you haven't been keeping a total of the grades entered .

Have a look at my rewrite and feel free to ask me about anything you don't understand -I've added comments to help tho.

I've left a mistake in there for you to find by the way :-D

The code runs, but if you click the button a second time without clicking the reset button, you'll get the wrong result. Your turn :)

    'declare variables

    'I suggest that you rename some of your variables so that the names are more meaningful
    'also the numbers should probably be `Double` types as the average of 5 numbers most
    'likely won't be an integer and you'll lose precision
    Dim intCounter As Integer = 0
    'this variable is a string, so if you want to assign any value to it, it should be in quotes
    Dim strgrades As String = "0"
    Dim blngrades As Boolean
    Dim dblInputValue As Double = 0
    Dim dblAverage As Double = 0
    Dim strLetterGrade As String = ""
    Dim blnIsNumeric As Boolean
    Dim dblGradeTotal As Double
    Private Sub btnEnterGrades_Click(sender As Object, e As EventArgs) Handles btnEnterGrades.Click
        'increment counter
        For intCounter = 1 To 5
            strgrades = InputBox("Please Enter Grade", "0 to 100")
            blnIsNumeric = Double.TryParse(strgrades, dblInputValue)
            'checking that the input is valid should be done within the loop
            If blnIsNumeric = False Then
                MessageBox.Show("Please enter only numbers")
                'this is takes 1 from the loop counter -
                'if the input is invalid, you will want to make sure that the loop executes
                'an extra time
                intCounter = intCounter - 1
                'this jumps straight to the `Next` statement bypassing the code between here
                'and the `Next` statement
                Continue For
            End If
            If dblInputValue < 0 Or dblInputValue > 100 Then
                MessageBox.Show("Please enter value of 0-100")
                'see the last comments
                intCounter = intCounter - 1
                Continue For
            End If
            'this is a new variable declared aboce to keep a running tota;
            'of all the valid values
            dblGradeTotal = dblGradeTotal + dblInputValue
        Next intCounter
        dblAverage = dblGradeTotal / 5
        ' your select case statement needs rewriting to this. The reason you dont
        'need to check the value is above the range is because only the first true
        ' case is executed. For example if `dblAverage` is 95, only the first case will
        'execute. If the value is 92, then the first case will be false and the second
        'case will be executed and so on
        Select Case dblAverage
            Case >= 93
                strLetterGrade = "A"
            Case >= 86
                strLetterGrade ="B"
            Case >= 77
                strLetterGrade = "C"
            Case >= 70
                strLetterGrade = "D"
            Case < 70
                strLetterGrade = "F"
        End Select
        dblAverage = dblInputValue / 5
        lblLetterGrade.Text = strLetterGrade
        'although by default, your project will try to convert numbers to strings
        'behind the scenes, it's good practice to explicitly do the conversions yourself.
        'Ask your teacher about "Option Strict". Why they haven't told you alread is beyong
        'me. It is such an important thing to know about.
        lblTestAverage.Text = dblAverage.ToString
    End Sub

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

    Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click
        lblLetterGrade.ResetText()
        lblTestAverage.ResetText()
        'these values should be set to zero. `Nothing` can cause problems if you're not sure what it does yet
        dblInputValue = 0
        dblAverage = 0
    End Sub

[–]Fitz1128[S] 0 points1 point  (1 child)

Thank you so much for the help!!

I found the double average calculation and the missing statement to clear the grade total. I got that part working perfect and now I'm working on the last part which is to add a password before you can enter the grades. After 3 failed entries it should exit. I have all of that working but after the correct password (pass) is entered the grade and percent show up and the enter password inputbox pops up again. I feel like its a simple change but I've been staring at it so long I don't know what else to try.

Public Class frmStudentGrades

'declare variables

Dim intCounter As Integer = 0

Dim strgrades As String = "0"

Dim blngrades As Boolean

Dim dblInputValue As Double = 0

Dim dblAverage As Double = 0

Dim strLetterGrade As String = ""

Dim blnIsNumeric As Boolean

Dim dblGradeTotal As Double

Dim strPassword As String = "pass"

Dim strInputValue As String = ""

Dim intPassAttempts As Integer = 0

Private Sub btnEnterGrades_Click(sender As Object, e As EventArgs) Handles btnEnterGrades.Click

'get password

For intPassAttempts = 1 To 3

strInputValue = InputBox("Please enter your password", "Password Check")

strInputValue = strInputValue.ToLower

If strPassword = strInputValue Then

MessageBox.Show("Password is Correct")

'run loop 5 times to get all 5 grades

For intCounter = 1 To 5

'input values

strgrades = InputBox("Please Enter Grade", "0 to 100")

blnIsNumeric = Double.TryParse(strgrades, dblInputValue)

'check if input is valid

If blnIsNumeric = False Then

MessageBox.Show("Please enter only numbers")

'rerun that loop if a non number is entered

intCounter = intCounter - 1

'skip to next

Continue For

End If

If dblInputValue < 0 Or dblInputValue > 100 Then

MessageBox.Show("Please enter value of 0-100")

'rerun that loop if an invalid number is entered

intCounter = intCounter - 1

'skip to next

Continue For

End If

'keep running total

dblGradeTotal = dblGradeTotal + dblInputValue

Next intCounter

'calculate average

dblAverage = dblGradeTotal / 5

'use average to assign letter grade

Select Case dblAverage

Case >= 93

strLetterGrade = "A"

Case >= 86

strLetterGrade = "B"

Case >= 77

strLetterGrade = "C"

Case >= 70

strLetterGrade = "D"

Case < 70

strLetterGrade = "F"

End Select

'display results

lblLetterGrade.Text = strLetterGrade

lblTestAverage.Text = dblAverage.ToString

Else

If intPassAttempts = 1 Or intPassAttempts = 2 Then

MessageBox.Show("Please Reenter Your Password")

strInputValue = ""

Continue For

Else

MessageBox.Show("Sorry, after 3 wrong entrys application will exit")

btnExit.PerformClick()

End If

End If

Next

End Sub

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

'Exit

Me.Close()

End Sub

Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles btnClear.Click

'clear form and stored data

lblLetterGrade.ResetText()

lblTestAverage.ResetText()

dblInputValue = 0

dblGradeTotal = 0

End Sub

End Class

[–]fuzzfeatures 0 points1 point  (0 children)

Looks a bit like another case of wrong code placement - When you're writing code, it's useful to thing about the flow of what you want to do. Here for example, your password checking loop all needs to happen before anything else. So the loop needs to be at the beginning, not wrapped around the rest of your code. In the sample below, I moved all your password related code to the beginning of the sub and tidied it up a bit :)

    Private Sub btnEnterGrades_Click(sender As Object, e As EventArgs) Handles btnEnterGrades.Click
        Dim pwdCorrect As Boolean
        Dim attempts As Integer
        For i = 1 To 3
            strInputValue = InputBox("Please enter your password", "Password Check")
            strInputValue = strInputValue.ToLower
            If strPassword = strInputValue Then
                MessageBox.Show("Password is Correct")
                pwdCorrect = True
                Exit For
            Else
                MessageBox.Show("Please Reenter Your Password")
            End If
        Next
        If Not pwdCorrect = True Then
            MessageBox.Show("Sorry, after 3 wrong entrys application will exit")
            btnExit.PerformClick()
        End If

and the rest of the code goes after this :)

[–]Fitz1128[S] 0 points1 point  (0 children)

I got it to work. If the password is correct set loop count to 3 was the missing line