Cant tell what im doing wrong no matter what numbers i enter it always return as a F and I cant get the average to calculate correctly plz help i uploaded the file also
Below is the assignment

Create an application that allows a teacher to enter three test scores each of the three students. The application should calculate each student’s average score and assign a letter grade based on the following grading scale:

Average Test Score Letter Grade
90 or greater A
80 through 89 B
70 through 79 C
60 through 69 D
Below 60 F

The application should prompt the user for each students name and three test scores. A box needs to appear after all the data has been entered showing the results.

Public Class Form1

        Private Sub btnEnter_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnEnter.Click

            ' Assigning variable as a string for student name
            Dim studentName As String

            Dim strScore As String
            Dim result As String
            ' Assigning Test Scores as a Decimal
            Dim numScore As Decimal
            ' Assnumigning average as a decimal
            Dim average As Decimal
            ' Assigning counter for repeatable names
            Dim counter As Integer = 1
            ' Assigning counter for Test scores
            Dim scoreCounter As Integer = 1

            For counter = 1 To 3
                ' Prompting user to enter names
                studentName = InputBox("Enter Student name" & counter, "Students name")
                numScore = scoreCounter + scoreCounter + scoreCounter
                ' Asking for the three given socres
                For scoreCounter = 1 To 3
                    ' Prompting user to enter students test scores.
                    strScore = InputBox("Enter Score " & scoreCounter, "Students Score", " ")
                    If strScore = "" Then
                        ' Canceling the operation.
                        MsgBox("You have cancelled the grading process")
                        Exit Sub
                    End If
                    If Not IsNumeric(strScore) Then
                        scoreCounter -= 1
                        ' Making sure the scores are numeric to prevent any errors.
                        MsgBox("Score has to be numeric..." & vbCrLf & "Click ok and enter score again")
                    End If
                average = numScore / 3
                ' Giving the end result and averages for the 3 students
                result &= studentName & "'s Average Grade is:" & calcGrade(average) & vbCrLf


            MsgBox(result, MsgBoxStyle.OkOnly, "Results")

        End Sub

        Private Function calcGrade(ByVal avg As Decimal) As String
            ' Assigning Grade values for the test scores
            If avg >= 90 Then
                Return "A"
            ElseIf avg >= 80 Then
                Return "B"
            ElseIf avg >= 70 Then
                Return "C"
            ElseIf avg >= 60 Then
                Return "D"
            ElseIf avg < 59 Then
                Return "F"
            End If
        End Function
    End Class

Edited by mike_2000_17: Fixed formatting

4 Years
Discussion Span
Last Post by Yolo03

Let's ignore the error checking for now and just get the logic right. In pseudo-code we have

clear output string

for 3 students

    get name
    set total to 0

    for 3 scores
        add next score into total

    calculate average as total / 3
    convert average to letter grade
    add line to output area


display output

For loop counters that you don't need after the loop completes it is better to declare them for the duration of the loop only as in

For students As Integer = 1 to 3

The same applies for the test scores as

For scores As Integer = 1 to 3

Your code says you want to Exit if a blank score is entered. Fair enough. You want to flag non-numeric scores. You should also flag invalid numbers like -5 and 110. The following loop will cycle until a valid score is entered or a null score is entered. If we drop out the bottom of the loop then we have a valid score. It would be convenient to make this into a separate function.

    get next score as string

    If score is null Then
        display message
    End If

    If score is a number from 0 to 100 Then
        Exit Loop
    End If

    display error message


calculate average

To convert from a number to a letter consider that you have five cases and you want to select one case. That's a hint. You want a select/case clause. As long as you arrange the case clauses in the correct order it works nicely as in

    Select Case score
        Case Is >= 90 : result = "A"
        Case Is >= 80 : result = "B"
        Case Is >= 70 : result = "C"
        Case Is >= 60 : result = "D"
        Case Else : result = "F"
    End Select

That's lot more straightforward than repeated ifs. I hope this makes it clearer.


Im still confused by what some of what you posted. I totally understand the part about making code stop on negative numbers and ones exceeding 100. The others went over my head atm. Could you go further in detail?


im not asking you to do that period. I know i have issues in the code but im not asking for the code bt when you state " For loop counters that you don't need after the loop completes it is better to declare them for the duration of the loop only as in "
What do you mean.


Ah. I see. Look at these two code samples

Dim student As Integer

For student = 1 To 3
    'do stuff here


For student As Integer = 1 To 3
    'do stuff here

In the first case, student has scope outside the loop where it isn't used. In the second case, student is only defined within the loop. It's similar to the practice of making a variable global only if it is used globally. The scope of a variable should reflect its use.


ok Im at work so i cant work on this live but i have my code with me. so i think i understand you a little bit.

For student As Integer = 1 To 3
'Retrieve Name 
 Retrieve test score1
 Than loop it till i have 3 test scores for three students?

Edited by keyontes

Public Class Form1 

    Private Sub BtnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnExit.Click 
    End Sub 

    Private Sub btnData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnData.Click 
        Dim sngTotal As Single 
        Dim intNumScores As Integer 
        Dim sngAverage As Single 
        Dim strNames As String 
        Dim strInput As String

        Dim intCount As Integer 
        Dim intCounter As Integer 
        Dim grade As String 

        strInput = InputBox("How many students test scores do you want " & _ 
        "to average?", "Enter a Value") 

        If Not Integer.TryParse(strInput, intNumScores) Then 
        End If 

        sngTotal = 0 
        intCount = 1 

        Do Until intCount > intNumScores 
            strNames = InputBox("Enter the name of the student:", "Student Name") 

            For intCounter = 0 To 2 Step 1 
                strInput = InputBox("Enter the value for test score " _ 
                & intCount.ToString, "Test Score Needed") 

                sngTotal += CSng(strInput) 

            If intNumScores > 0 Then 
                sngAverage = sngTotal / intNumScores 
                sngAverage = 0.0 
            End If 

            If sngAverage < 60 Then 
                grade = "F"
            ElseIf sngAverage < 70 Then 
                grade = "D" 
            ElseIf sngAverage < 80 Then 
                grade = "C" 
            ElseIf sngAverage < 90 Then 
                grade = "B" 
            ElseIf sngAverage <= 100 Then 
                grade = "A" 
            End If 

            lstGradeReport.Items.Add(lstGradeReport.Text & strNames & 
            ControlChars.Tab & "Average: " & sngAverage.ToString("n0") & ControlChars.Tab 
            & "Grade: " & grade) 
            intCount += 1 

    End Sub 
End Class

=======Here its what I have so far when I test it I get the first name correct and the average as well, but the second one is not correct? what am I missing?

Edited by Reverend Jim: Fixed code formatting


You should set

sngTotal = 0

just prior to

For intCounter = 0 To 2 

And you should probably change the loop to

For intCounter = 1 to 3

You should also validate the entered grade to ensure that it is a number and falls in the range 0-100. You can omit the test

If intNumScores > 0 Then 

Because you are doing

Do Until intCount > intNumScores

and because intCount starts at 1, you are guaranteed that intNumScores will never be zero.


Thaaaaaank you so much!!! I got it. I really appreciate the fast respond!!!!

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.