GPA Calculator Problem

First VB.Net Project. Need help, please!!!

Page 1 of 1

3 Replies - 7712 Views - Last Post: 26 January 2010 - 04:40 AM Rate Topic: -----

#1 tspark007   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-January 10

GPA Calculator Problem

Posted 24 January 2010 - 08:44 PM

Hi DIC!

I'm trying to create a GPA calculator as my first VB.NET project. I've read an introductory book on VB.Net, and it's my second programming language that I've learned, so I'm trying to get the language down pat. The calculator gives the user 6 textboxes to enter his GPA for the current class and 6 textboxes for the amount of credits for the class. A button will trigger the calculation for the current semester. This part is fine. The second part of the calculator is what's giving me the problem. The second part has calculates the cumulative grade, which calculates the current semester into the cumulative grade. The user has one textbox to enter his cumulative GPA and one textbox for his cumulative credits. I get the correct answer, but the weird thing is that I get it the second time i click the button to calculate it. The first time gives me some weird output, and I have no clue how it gets the number it does. I was hoping you guys could look at the code to see what's wrong with it:

Public Class frmMain

	'ClassCount() Variables
	Private myclass1 As Double
	Private myclass2 As Double
	Private myclass3 As Double
	Private myclass4 As Double
	Private myclass5 As Double
	Private myclass6 As Double

	'CreditsCount() Variables
	Private myCredits1 As Integer
	Private myCredits2 As Integer
	Private myCredits3 As Integer
	Private myCredits4 As Integer
	Private myCredits5 As Integer
	Private myCredits6 As Integer

	'CumulativeCount() Variables
	Private cumClass As Double
	Private cumCredits As Integer

	'ValidateClass() Variables
	Private myGPA1 As Double
	Private myGPA2 As Double
	Private myGPA3 As Double
	Private myGPA4 As Double
	Private myGPA5 As Double
	Private myGPA6 As Double
	Private finalmyGPA As Double

	'ValidateCredits() Variables
	Private finalmyCredits As Integer

' Comment used to make the code in this post easier to read

	Private Sub btnCurrent_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCurrent.Click

		Dim current As String = AddCurrent()

		If current > 0 AndAlso current <= 4 Then
			MsgBox("Your GPA is: " & current)
		ElseIf current <= 0 OrElse current > 4 Then
			Exit Sub
		Else
			MsgBox("ERROR:" & vbCrLf & vbLf & "There has been a problem calculating your GPA. " & _
				   "Please check to see if your data is formatted correctly.")
			Exit Sub
		End If
	End Sub

	Private Sub btnCumulative_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCumulative.Click
		Dim cumulative As String = AddCumulative()

		If cumulative > 0 AndAlso cumulative <= 4 Then
			MsgBox("Your cumulative GPA is: " & cumulative)
		Else
			MsgBox("ERROR:" & vbCrLf & vbLf & "There has been a problem calculating your GPA. " & _
			"Please check to see if your data is formatted correctly.")
			Exit Sub
		End If
	End Sub

	Private Sub txtClass1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtClass1.TextChanged
		If txtClass1.Text = "" Then btnCurrent.Enabled = False

		If txtClass1.Text.Length >= 1 AndAlso IsNumeric(txtClass1.Text) Then
			If txtCredits1.Text.Length >= 1 AndAlso IsNumeric(txtCredits1.Text) Then
				btnCurrent.Enabled = True
			Else
				btnCurrent.Enabled = False
			End If
		End If
	End Sub

	Private Sub txtCredits1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCredits1.TextChanged
		If txtCredits1.Text = "" Then btnCurrent.Enabled = False

		If txtCredits1.Text.Length >= 1 AndAlso IsNumeric(txtCredits1.Text) Then
			If txtClass1.Text.Length >= 1 AndAlso IsNumeric(txtClass1.Text) Then
				btnCurrent.Enabled = True
			Else
				btnCurrent.Enabled = False
			End If
		End If
	End Sub

	Private Sub txtPriorClass_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPriorClass.TextChanged
		If txtPriorClass.Text = "" Then btnCumulative.Enabled = False

		If txtPriorClass.Text.Length >= 1 AndAlso IsNumeric(txtPriorClass.Text) Then
			If txtPriorCredits.Text.Length >= 1 AndAlso IsNumeric(txtPriorCredits.Text) Then
				btnCumulative.Enabled = True
			Else
				btnCumulative.Enabled = False
			End If
		End If
	End Sub

	Private Sub txtPriorCredits_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtPriorCredits.TextChanged
		If txtPriorCredits.Text = "" Then btnCumulative.Enabled = False

		If txtPriorCredits.Text.Length >= 1 AndAlso IsNumeric(txtPriorCredits.Text) Then
			If txtPriorClass.Text.Length >= 1 AndAlso IsNumeric(txtPriorClass.Text) Then
				btnCumulative.Enabled = True
			Else
				btnCumulative.Enabled = False
			End If
		End If
	End Sub

	Private Sub txtClass1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtClass1.KeyPress
		If (e.KeyChar < "0" OrElse e.KeyChar > "9") _
	AndAlso e.KeyChar <> ControlChars.Back AndAlso e.KeyChar <> "." Then
			e.Handled = True
		End If
	End Sub

' Erased the rest of the KeyPress events to reduce the size of this post. They are all similar to the one above.
' Comment used to make the code in this post easier to read

	Public Sub ClassCount()

		If txtClass1.Text <> "" Then
			myclass1 = CDbl(txtClass1.Text)
		Else
			Exit Sub
		End If

		If txtClass2.Text <> "" Then
			myclass2 = CDbl(txtClass2.Text)
		Else
			Exit Sub
		End If

		If txtClass3.Text <> "" Then
			myclass3 = CDbl(txtClass3.Text)
		Else
			Exit Sub
		End If

		If txtClass4.Text <> "" Then
			myclass4 = CDbl(txtClass4.Text)
		Else
			Exit Sub
		End If

		If txtClass5.Text <> "" Then
			myclass5 = CDbl(txtClass5.Text)
		Else
			Exit Sub
		End If

		If txtClass6.Text <> "" Then
			myclass6 = CDbl(txtClass6.Text)
		End If
	End Sub

	Public Sub CreditsCount()

		If txtCredits1.Text <> "" Then
			myCredits1 = CInt(txtCredits1.Text)
		Else
			Exit Sub
		End If

		If txtCredits2.Text <> "" Then
			myCredits2 = CInt(txtCredits2.Text)
		Else
			Exit Sub
		End If

		If txtCredits3.Text <> "" Then
			myCredits3 = CInt(txtCredits3.Text)
		Else
			Exit Sub
		End If

		If txtCredits4.Text <> "" Then
			myCredits4 = CInt(txtCredits4.Text)
		Else
			Exit Sub
		End If

		If txtCredits5.Text <> "" Then
			myCredits5 = CInt(txtCredits5.Text)
		Else
			Exit Sub
		End If

		If txtCredits6.Text <> "" Then
			myCredits6 = CInt(txtCredits6.Text)
		End If
	End Sub

	Public Sub CumulativeCount()

		If txtPriorClass.Text <> "" Then
			cumClass = CDbl(txtPriorClass.Text)
		End If

		If txtPriorCredits.Text <> "" Then
			cumCredits = CInt(txtPriorCredits.Text)
		End If
	End Sub

	Public Sub ValidateClass()

		If myclass1 <= 4 Then
			myGPA1 = myclass1 * myCredits1
			finalmyGPA = myGPA1
		Else
			MsgBox("Your GPA can only be in a range of 0.0 - 4.0." & vbCrLf & vbLf & "EX: " & _
			"4.0, 3.5, 2.75, 0.5, etc.")
			txtClass1.Focus()
			txtClass1.SelectAll()
			Exit Sub
		End If

		If myclass2 <= 4 Then
			myGPA2 = myclass2 * myCredits2
			finalmyGPA = myGPA1 + myGPA2
		Else
			MsgBox("Your GPA can only be in a range of 0.0 - 4.0." & vbCrLf & vbLf & "EX: " & _
			"4.0, 3.5, 2.75, 0.5, etc.")
			txtClass1.Focus()
			txtClass1.SelectAll()
			Exit Sub
		End If

		If myclass3 <= 4 Then
			myGPA3 = myclass3 * myCredits3
			finalmyGPA = myGPA1 + myGPA2 + myGPA3
		Else
			MsgBox("Your GPA can only be in a range of 0.0 - 4.0." & vbCrLf & vbLf & "EX: " & _
			"4.0, 3.5, 2.75, 0.5, etc.")
			txtClass1.Focus()
			txtClass1.SelectAll()
			Exit Sub
		End If

		If myclass4 <= 4 Then
			myGPA4 = myclass4 * myCredits4
			finalmyGPA = myGPA1 + myGPA2 + myGPA3 + myGPA4
		Else
			MsgBox("Your GPA can only be in a range of 0.0 - 4.0." & vbCrLf & vbLf & "EX: " & _
			"4.0, 3.5, 2.75, 0.5, etc.")
			txtClass1.Focus()
			txtClass1.SelectAll()
			Exit Sub
		End If

		If myclass5 <= 4 Then
			myGPA5 = myclass5 * myCredits5
			finalmyGPA = myGPA1 + myGPA2 + myGPA3 + myGPA4 + myGPA5
		Else
			MsgBox("Your GPA can only be in a range of 0.0 - 4.0." & vbCrLf & vbLf & "EX: " & _
			"4.0, 3.5, 2.75, 0.5, etc.")
			txtClass1.Focus()
			txtClass1.SelectAll()
			Exit Sub
		End If

		If myclass6 <= 4 Then
			myGPA6 = myclass6 * myCredits6
			finalmyGPA = myGPA1 + myGPA2 + myGPA3 + myGPA4 + myGPA5 + myGPA6
		Else
			MsgBox("Your GPA can only be in a range of 0.0 - 4.0." & vbCrLf & vbLf & "EX: " & _
			"4.0, 3.5, 2.75, 0.5, etc.")
			txtClass1.Focus()
			txtClass1.SelectAll()
		End If
	End Sub

	Public Sub ValidateCredits()

		If myCredits1 >= 1 AndAlso myCredits1 <= 5 Then
			finalmyCredits = myCredits1
		Else
			Exit Sub
		End If

		If myCredits2 >= 1 AndAlso myCredits2 <= 5 Then
			finalmyCredits = myCredits1 + myCredits2
		Else
			Exit Sub
		End If

		If myCredits3 >= 1 AndAlso myCredits3 <= 5 Then
			finalmyCredits = myCredits1 + myCredits2 + myCredits3
		Else
			Exit Sub
		End If

		If myCredits4 >= 1 AndAlso myCredits4 <= 5 Then
			finalmyCredits = myCredits1 + myCredits2 + myCredits3 + _
				myCredits4
		Else
			Exit Sub
		End If

		If myCredits5 >= 1 AndAlso myCredits5 <= 5 Then
			finalmyCredits = myCredits1 + myCredits2 + myCredits3 + _
				myCredits4 + myCredits5
		Else
			Exit Sub
		End If

		If myCredits6 >= 1 AndAlso myCredits6 <= 5 Then
			finalmyCredits = myCredits1 + myCredits2 + myCredits3 + _
				myCredits4 + myCredits5 + myCredits6
		End If
	End Sub

	Public Function AddCurrent() As Double

		Dim currentGPA As Double
		Dim result As Double

		CreditsCount()
		ValidateCredits()

		ClassCount()
		ValidateClass()

		currentGPA = finalmyGPA / finalmyCredits

		result = currentGPA.ToString("f3")

		Return result

	End Function

	Public Function AddCumulative() As Double

		Dim totalcumClass As Integer
		Dim totalcumCredits As Integer
		Dim totalcumGPA As Double
		Dim finalcumClass As Integer = cumClass * cumCredits
		Dim result As Double

		CreditsCount()
		ValidateCredits()

		ClassCount()
		ValidateClass()

		CumulativeCount()

		If txtCredits1.Text <> "" Then
			totalcumCredits = finalmyCredits + cumCredits
		Else
			totalcumCredits = cumCredits
		End If

		If txtClass1.Text <> "" Then
			totalcumClass = finalmyGPA + finalcumClass
		Else
			totalcumClass = finalcumClass
		End If

		totalcumGPA = totalcumClass / totalcumCredits

		result = totalcumGPA.ToString("f3")

		Return result

	End Function
End Class



Also, as a new VB.Net programmer, I would like to hear constructive criticism on my code (Tips, more efficient way of writing it, etc.)

Thanks :) ,
tspark

Edit: Reduced some unnecessary stated code to make this post smaller and easier to read.

This post has been edited by tspark007: 25 January 2010 - 02:21 PM


Is This A Good Question/Topic? 0
  • +

Replies To: GPA Calculator Problem

#2 tspark007   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 13-January 10

Re: GPA Calculator Problem

Posted 25 January 2010 - 02:26 PM

Nothing? This is my first project, and I'm soooooo close to finishing it. I looked over my code again, and I still can't figure out why it's doing this. Why is it not posting the correct answer the first time I click the button but it does on the second click? Please help :crazy:
Was This Post Helpful? 0
  • +
  • -

#3 pdkharkar   User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 345
  • Joined: 19-January 09

Re: GPA Calculator Problem

Posted 25 January 2010 - 07:08 PM

Please reduce the length of your code
so that we will be comfortable to read and fine out the error
also tell me where exactly the error is
Was This Post Helpful? 0
  • +
  • -

#4 PDUNZ   User is offline

  • D.I.C Regular
  • member icon

Reputation: 50
  • View blog
  • Posts: 376
  • Joined: 17-July 09

Re: GPA Calculator Problem

Posted 26 January 2010 - 04:40 AM

You may like to use the Debug feature to find out where and how and why its giving the wrong answer on the first click. Set a break point and then step through the code and watch the variables and see where its picking the first answer from.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1