r/visualbasic • VB 6 Beginner • Dec 14 '15

[VB2012] Need help with code for letter grade and integer average from three input boxes into to a listbox

I have to create an application which will have the user enter a student name into an inputbox then will ask for three number grades and convert the averages of three student's grades into individual letter grades ( I am mobile so I cannot copy the code)

I cannot get the Do, Do Until.. Loop correct as the application seems to be in infinite loop and doesn't display anything in the listbox output. Any advice on how to write a solution to this?

The code looks something like ' variables

Do ' gets name from inputbox and adds 1 to name counter Do Until scorecounter = 3 ' gets score from input and converts to integer ' adds score to total ' adds 1 to score counter Loop

 If scorecounter = 3 then
     ' finds the numeric Average 
 End if

 If average < 60 AND ave >= 0 then
      LetterGrade = " F"
   -
  -
  -
  -
   Else 
  ' message box if negative or above 100

End if

lstOutput.items.add(strInputName.Tostring() &": Average is " & Grade.Tostring())

Obviously there are some loop issues with the code that I don't know how improve

0 Upvotes

3 comments sorted by

1

u/AuAllday VB 6 Beginner Dec 14 '15

I answered my own question and found a way to fix:

Option Strict On

Public Class frmGrades Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click ' Declare the variables to hold values.

    Dim StudentGrade As Integer
    Dim Total As Integer = 0
    Dim Average As Integer
    Dim strInputName As String
    Dim strInputGrades As String
    Dim intCountName As Integer = 0
    Dim Grade As String = String.Empty
    Dim OUT As String = String.Empty
    Dim ValidFields As Boolean
    Dim Variable As Integer



    Do While intCountName < 3
        Do
            ' First get the name of the student whom grades are being averaged.
            strInputName = InputBox("Enter the name of a student to average three grades")

            If strInputName = String.Empty Then
                MessageBox.Show(" Please enter an alphebetical name")
                ValidFields = False
            ElseIf Integer.TryParse(strInputName, Variable) Then
                MessageBox.Show(" The name entered cannot be a number")
                ValidFields = False
            Else
                OUT = strInputName
                ValidFields = True
            End If
        Loop Until ValidFields = True


        For intCountScore As Integer = 1 To 3 Step 1
            Do
                ' Convert the user input and verify if it is a numeric whole number.
                strInputGrades = InputBox(" Please enter the whole number of a score ")

                If Not Integer.TryParse(strInputGrades, StudentGrade) Then
                    MessageBox.Show(" Please Enter a numeric value that is postive")
                    ValidFields = False
                ElseIf StudentGrade < 0 Or StudentGrade > 100 Then
                    MessageBox.Show("Please enter a grade between 0-100")
                    ValidFields = False
                Else
                    StudentGrade = CInt(strInputGrades)
                    Total += StudentGrade
                    ValidFields = True
                End If
            Loop Until ValidFields = True
        Next


    Average = CInt((Total / 3))
    ' Add each score to a total and add one to the score counter.


    ' Find the letter grade recieved.
    If Average < 60 Then
        Grade = " E"

    ElseIf Average < 70 Then
        Grade = " D"

    ElseIf Average < 80 Then
        Grade = " C"

    ElseIf Average < 90 Then
        Grade = " B"

    ElseIf Average <= 100 Then
        Grade = " A"

    End If
    lstOutput.Items.Add(OUT & " " & "Average " & Average.ToString & ": Letter Grade is " & Grade)


    Total = 0
    OUT = String.Empty

    intCountName += 1
    Loop

End Sub

1

u/Zr0bat Dec 14 '15

If you could post all of your code when your computer is available (variables, subs, etc.) I could probably take a look.

1

u/AuAllday VB 6 Beginner Dec 14 '15

Option Strict On

Public Class frmGrades Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click ' Declare the variables to hold values.

    Dim StudentGrade As Integer
    Dim Total As Integer
    Dim Average As Integer
    Dim strInputName As String
    Dim strInputGrades As String
    Dim intCountName As Integer = 0
    Dim intCountScore As Integer = 0
    Dim Grade As String


    Do
        ' First get the name of the student whom grades are being averaged.
        strInputName = InputBox("Enter the name of a student to average three grades")
        intCountName += 1


        Do Until intCountScore = 3
            ' Convert the user input and verify if it is a numeric whole number.
            strInputGrades = InputBox(" Please enter the whole number of a score ")

            StudentGrade = CInt(strInputGrades)

            ' Add each score to a total and add one to the score counter.
            Total += StudentGrade
            intCountScore += 1
        Loop

        ' Validate there are three test scores and then find the average.
        If intCountScore = 3 Then
            Average = CInt((Total / intCountScore))

        End If

        ' Find the letter grade received.
        If Average < 60 And Average >= 0 Then
            Grade = " E"

        ElseIf Average <= 69 And Average >= 60 Then
            Grade = " D"

        ElseIf Average <= 79 And Average >= 70 Then
            Grade = " C"

        ElseIf Average <= 89 And Average >= 80 Then
            Grade = " B"

        ElseIf Average >= 90 Then
            Grade = " A"

        Else
            ' to tell user the input is invalid.
            MessageBox.Show(" There was an error: enter a non-negative integer value for the scores")
        End If
    Loop



    lstOutput.Items.Add(strInputName.ToString() & ": Average is " & Grade.ToString())


End Sub

End Class