Reputation: 1
Public Class GPA_Form
Private Sub exitButton_Click(sender As System.Object, e As System.EventArgs) Handles exitButton.Click
Me.Close()
End Sub
Private Sub entdatButton_Click(sender As System.Object, e As System.EventArgs) Handles entdatButton.Click
Const Prompt As String = "Enter number of Credit Hours:"
Const Title As String = "Credit Hours"
Const Prompt2 As String = "Enter grades:"
Const Title2 As String = "Grades"
Dim inputCredit As String
Dim inputGrades As String
Dim creditHrs As Integer
Dim grades As Char
Dim gradesCounter As Integer
Dim creditHrsAccumulator As Integer
Dim point As Integer
Dim gpaTot As Integer
Dim pntAccumulator As Integer
inputCredit = InputBox(Prompt, Title)
inputGrades = InputBox(Prompt2, Title2)
Do While inputCredit <> String.Empty
Integer.TryParse(inputCredit, creditHrs)
Char.TryParse(inputGrades, grades)
gradesCounter += 1
creditHrsAccumulator += creditHrs
Select Case grades
Case Is >= "A"
point = 4
Case Is >= "B"
point = 3
Case Is >= "C"
point = 2
Case Is >= "D"
point = 1
Case Is >= "F"
point = 0
End Select
pntAccumulator += point
gpaTot = pntAccumulator / gradesCounter
tchData.Text = creditHrsAccumulator.ToString("N0")
numGrEnt.Text = gradesCounter.ToString("N0")
gpaData.Text = gpaTot.ToString("N2")
inputCredit = InputBox(Prompt, Title)
inputGrades = InputBox(Prompt2, Title2)
Loop
End Sub
End Class
I'm just a beginner in visual basic but would like to know where I'm going wrong here in calculating GPA and even if accumulating something from Select...Case is possible. If not then I would have to type this in differently then shown above, of course. If anyone can give me hints as to what I'm doing wrong it would be greatly appreciated.
Upvotes: 0
Views: 5483
Reputation: 1
when comparing a string you cant use >= you are saying if grade is greater than or equal to a string, where it can only be equal too or not equal too.
<= pr >= only works with comparing numbers.
Upvotes: 0
Reputation: 29254
The GPA formula is not gpaTot = pntAccumulator / gradesCounter
but
gpaTot = 0
creditHrsAccumulator = 0
Do While inputCredit <> String.Empty
Integer.TryParse(inputCredit, creditHrs)
Char.TryParse(inputGrades, grades)
Select Case grades
Case "A"
point = 4
Case "B"
point = 3
Case "C"
point = 2
Case "D"
point = 1
Case "F"
point = 0
End Select
gpaTot= (creditHrsAccumulator*gpaTot + point*creditHrs)/(creditHrsAccumulator + creditHrs)
gradesCounter += 1
creditHrsAccumulator += creditHrs
...
And you can get rid of the pntAccumulator
.
Upvotes: 1
Reputation: 16697
Select ... Case is formatted differently from what you used.
We all know that for grades, A > B > C, but Visual Basic doesn't know that. In Visual Basic, these are just chars, and they are not comparable thus. A Case
statement does not support comparison operators like >=
, they are implicitly ==
, and you don't write "Case Is", just "Case". Try this:
Select Case grades
Case "A"
point = 4
Case "B"
point = 3
Case "C"
point = 2
Case "D"
point = 1
Case "F"
point = 0
End Select
Upvotes: 1