1
Answer

Some code help needed...

Sonny Thai

Sonny Thai

16y
2.7k
1

I am trying to write a program that calculates the GPA from 7 classes the student has taken. This is purely practice for me as this is what we are going over in class, but the teacher is not going to deep into it and I am trying better myself at it. The problem is that when I try to calulate the gpa, it keeps saying I am dividing by zero. Heres the code:

 

Public Class frmMain

Dim intGrade1 As Integer

Dim intGrade2 As Integer

Dim intGrade3 As Integer

Dim intGrade4 As Integer

Dim intGrade5 As Integer

Dim intGrade6 As Integer

Dim intGrade7 As Integer

Dim decCreditHours

Dim intFinalGPA As Integer

 

Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

' Sets the txtCourse1 & txtHour1 box with instructions

txtCourse1.Text = "Enter Course Name"

txtHour1.Text = "Hours?"

End Sub

Private Sub txtCourse1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtCourse1.GotFocus

'Selects the Hour1 box for deletion

txtCourse1.SelectAll()

End Sub

Private Sub txtHour1_GotFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtHour1.GotFocus

'Selects the Hour1 box for deletion

txtHour1.SelectAll()

End Sub

Public Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalc.Click

Dim sngTotalGPA As Decimal

' Validate for numbers

If Not IsNumeric(txtHour1.Text) Or Not IsNumeric(txtHour2.Text) Or Not IsNumeric(txtHour3.Text) Or Not IsNumeric(txtHour4.Text) Or Not IsNumeric(txtHour5.Text) Or Not IsNumeric(txtHour6.Text) Or Not IsNumeric(txtHour7.Text) Then

MessageBox.Show("Enter numbers only", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error, MessageBoxDefaultButton.Button1)

'Clear the course textboxes

txtCourse1.Clear()

txtCourse2.Clear()

txtCourse3.Clear()

txtCourse4.Clear()

txtCourse5.Clear()

txtCourse6.Clear()

txtCourse7.Clear()

'Clear the hour textboxes

txtHour1.Clear()

txtHour2.Clear()

txtHour3.Clear()

txtHour4.Clear()

txtHour5.Clear()

txtHour6.Clear()

txtHour7.Clear()

'Clear the grade comboboxes

cboGrade1.SelectedItem = -1

cboGrade2.SelectedItem = -1

cboGrade3.SelectedItem = -1

cboGrade4.SelectedItem = -1

cboGrade5.SelectedItem = -1

cboGrade6.SelectedItem = -1

cboGrade7.SelectedItem = -1

'Return the focus & clear the GPA & Total labels

txtCourse1.Focus()

lblGPA.Text = ""

lblTotal.Text = ""

Return

Else

sngTotalGPA = intFinalGPA

'Calculate the total hours

decCreditHours = CDec(txtHour1.Text) + CDec(txtHour2.Text) + CDec(txtHour3.Text) + CDec(txtHour4.Text) + CDec(txtHour5.Text) + CDec(txtHour6.Text) + CDec(txtHour7.Text)

 

'Display the total credit hours

lblTotal.Text = decCreditHours

'Display the final GPA

lblGPA.Text = FormatNumber(decCreditHours / sngTotalGPA)

End If

End Sub

Private Sub btnClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClear.Click

'Clear the course textboxes

txtCourse1.Clear()

txtCourse2.Clear()

txtCourse3.Clear()

txtCourse4.Clear()

txtCourse5.Clear()

txtCourse6.Clear()

txtCourse7.Clear()

'Clear the hour textboxes

txtHour1.Clear()

txtHour2.Clear()

txtHour3.Clear()

txtHour4.Clear()

txtHour5.Clear()

txtHour6.Clear()

txtHour7.Clear()

'Clear the grade comboboxes

cboGrade1.SelectedItem = -1

cboGrade2.SelectedItem = -1

cboGrade3.SelectedItem = -1

cboGrade4.SelectedItem = -1

cboGrade5.SelectedItem = -1

cboGrade6.SelectedItem = -1

cboGrade7.SelectedItem = -1

'Return the focus to the first hour textbox

txtCourse1.Focus()

End Sub

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

End Sub

Private Sub btnExit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnExit.Click

'Display a messagebox confirming closure of the program

Dim intExit = MessageBox.Show("Are you sure you want to exit?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button1)

If intExit = Windows.Forms.DialogResult.Yes Then

Me.Close()

Else

intExit = Windows.Forms.DialogResult.No

End If

End Sub

Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click

End Sub

' The choices of grades are A, B, C, D, F, IN, and W. They equal 4, 3, 2, 1, 0, 0, and 0, respectivly.

Function CalcGrade1()

If cboGrade1.SelectedItem = 0 Then

intGrade1 = 4

ElseIf cboGrade1.SelectedItem = 1 Then

intGrade1 = 3

ElseIf cboGrade1.SelectedItem = 2 Then

intGrade1 = 2

ElseIf cboGrade1.SelectedItem = 3 Then

intGrade1 = 1

ElseIf cboGrade1.SelectedItem = 4 Then

intGrade1 = 0

ElseIf cboGrade1.SelectedItem = 6 Then

intGrade1 = 0

ElseIf cboGrade1.SelectedItem = 7 Then

intGrade1 = 0

End If

Return intGrade1

End Function

Function CalcGrade2()

If cboGrade1.SelectedItem = 0 Then

intGrade1 = 4

ElseIf cboGrade1.SelectedItem = 1 Then

intGrade1 = 3

ElseIf cboGrade1.SelectedItem = 2 Then

intGrade1 = 2

ElseIf cboGrade1.SelectedItem = 3 Then

intGrade1 = 1

ElseIf cboGrade1.SelectedItem = 4 Then

intGrade1 = 0

ElseIf cboGrade1.SelectedItem = 6 Then

intGrade1 = 0

ElseIf cboGrade1.SelectedItem = 7 Then

intGrade1 = 0

End If

Return intGrade2

End Function

Function CalcGrade3()

If cboGrade1.SelectedItem = 0 Then

intGrade1 = 4

ElseIf cboGrade1.SelectedItem = 1 Then

intGrade1 = 3

ElseIf cboGrade1.SelectedItem = 2 Then

intGrade1 = 2

ElseIf cboGrade1.SelectedItem = 3 Then

intGrade1 = 1

ElseIf cboGrade1.SelectedItem = 4 Then

intGrade1 = 0

ElseIf cboGrade1.SelectedItem = 6 Then

intGrade1 = 0

ElseIf cboGrade1.SelectedItem = 7 Then

intGrade1 = 0

End If

Return intGrade3

End Function

Function CalcGrade4()

If cboGrade1.SelectedItem = 0 Then

intGrade1 = 4

ElseIf cboGrade1.SelectedItem = 1 Then

intGrade1 = 3

ElseIf cboGrade1.SelectedItem = 2 Then

intGrade1 = 2

ElseIf cboGrade1.SelectedItem = 3 Then

intGrade1 = 1

ElseIf cboGrade1.SelectedItem = 4 Then

intGrade1 = 0

ElseIf cboGrade1.SelectedItem = 6 Then

intGrade1 = 0

ElseIf cboGrade1.SelectedItem = 7 Then

intGrade1 = 0

End If

Return intGrade4

End Function

Function CalcGrade5()

If cboGrade1.SelectedItem = 0 Then

intGrade1 = 4

ElseIf cboGrade1.SelectedItem = 1 Then

intGrade1 = 3

ElseIf cboGrade1.SelectedItem = 2 Then

intGrade1 = 2

ElseIf cboGrade1.SelectedItem = 3 Then

intGrade1 = 1

ElseIf cboGrade1.SelectedItem = 4 Then

intGrade1 = 0

ElseIf cboGrade1.SelectedItem = 6 Then

intGrade1 = 0

ElseIf cboGrade1.SelectedItem = 7 Then

intGrade1 = 0

End If

Return intGrade5

End Function

Function CalcGrade6()

If cboGrade1.SelectedItem = 0 Then

intGrade1 = 4

ElseIf cboGrade1.SelectedItem = 1 Then

intGrade1 = 3

ElseIf cboGrade1.SelectedItem = 2 Then

intGrade1 = 2

ElseIf cboGrade1.SelectedItem = 3 Then

intGrade1 = 1

ElseIf cboGrade1.SelectedItem = 4 Then

intGrade1 = 0

ElseIf cboGrade1.SelectedItem = 6 Then

intGrade1 = 0

ElseIf cboGrade1.SelectedItem = 7 Then

intGrade1 = 0

End If

Return intGrade6

End Function

Function CalcGrade7()

If cboGrade1.SelectedItem = 0 Then

intGrade1 = 4

ElseIf cboGrade1.SelectedItem = 1 Then

intGrade1 = 3

ElseIf cboGrade1.SelectedItem = 2 Then

intGrade1 = 2

ElseIf cboGrade1.SelectedItem = 3 Then

intGrade1 = 1

ElseIf cboGrade1.SelectedItem = 4 Then

intGrade1 = 0

ElseIf cboGrade1.SelectedItem = 6 Then

intGrade1 = 0

ElseIf cboGrade1.SelectedItem = 7 Then

intGrade1 = 0

End If

Return intGrade7

End Function

 

Function CalcFinalGPA()

intFinalGPA = (decCreditHours / (intGrade1 + intGrade2 + intGrade3 + intGrade4 + intGrade5 + intGrade6 + intGrade7))

Return intFinalGPA

End Function

End Class


Answers (1)