r/visualbasic • • May 15 '20

Input Box help

I have an assignment that I am working on where I am supposed to input a grade in an input box and repeat with a repetition structure 5 times and then display an average grade and letter grade. I think they key part I am struggling with is how to sum my 5 grades in one variable. The math is only being performed on the last enrty.

I was put in this class as an alternative to a LabVIEW class that was canceled so I'm really new to this and the instructor told me to find the information I need online. Can anyone help a newbie out a bit? Thanks!

Code<

Public Class frmStudentGrades

'declare variables

Dim intNum As Integer = 0

Dim intCounter As Integer = 0

Dim strgrades As String = 0

Dim blngrades As Boolean

Dim intGrades As Integer = 0

Dim intAverage As Integer = 0

Dim strLetterGrade As String = Nothing

Dim blnIsNumeric As Boolean

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 = Integer.TryParse(strgrades, intGrades)

Next intCounter

'if numeric and 0-100 assign grade

If blnIsNumeric Then

If intGrades >= 0 And intNum <= 100 Then

' intNum = intGrades

Select Case intAverage

Case intAverage >= 93

strLetterGrade = "A"

Case intAverage >= 86 To intAverage <= 92.99

strLetterGrade = "B"

Case intAverage >= 77 To intAverage <= 85.9

strLetterGrade = "C"

Case intAverage >= 70 To intAverage <= 76.9

strLetterGrade = "D"

Case intAverage < 70

strLetterGrade = "F"

End Select

intAverage = intGrades / 5

lblLetterGrade.Text = strLetterGrade

lblTestAverage.Text = intAverage

Else

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

End If

Else

MessageBox.Show("Please enter only numbers")

End If

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()

intGrades = Nothing

intAverage = Nothing

End Sub

End Class

6 Upvotes

4 comments sorted by

2

u/fuzzfeatures May 15 '20

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

1

u/Fitz1128 May 15 '20

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

1

u/fuzzfeatures May 16 '20

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 :)

1

u/Fitz1128 May 15 '20

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