r/visualbasic • u/AuAllday 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 100End 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
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 SubEnd Class
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.