3 Replies - 6910 Views - Last Post: 15 September 2008 - 02:44 PM Rate Topic: -----

#1 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Fat Calculator

Posted 14 September 2008 - 11:03 PM

Hello everyone, I am using Visual Basic 2005 Express Edition to code the Fat Calculator. I coded the fat calculator with calculations no problem it returns the value as they suppose to be.

fatCalories = fatGrams * 9 'renders result as 315
fatPercent = fatCalories / calories 'renders result as 31.50%

But the problem that I am having now is that when I code the following

If Not IsNumeric(calories) Then
Messagebox.Show('calories must be numeric")
ElseIf Not IsNumeric(fatGrams) Then
Messagebox.Show('fat grams must be numeric")
End If

I get an error stating that I am trying to divide by zero.

Can anyone assist, thanks I really appreciate it.

Here is the code:
Option Explicit On
Option Strict On
Imports System.Math
Imports System.Globalization
Public Class Mainform

	Private Sub exitButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles exitButton.Click
		Me.Close()

	End Sub

	Private Sub calcButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles calcButton.Click
		Dim calories As Integer
		Dim fatGrams As Integer
		Dim fatCalories As Single
		Dim fatPercent As Decimal
		Dim message As String
		Dim isConverted1 As Boolean
		Dim isConverted2 As Boolean

		isConverted1 = Integer.TryParse(caloriesTextBox.Text, calories)
		isConverted2 = Integer.TryParse(fatGramsTextBox.Text, fatGrams)

		If isConverted1 = True AndAlso isConverted2 = True Then

		End If

		fatCalories = fatGrams * 9
		fatPercent = Convert.ToDecimal(fatCalories) / Convert.ToDecimal(calories)
	   
		fatCalsLabel.Text = Convert.ToString(fatCalories)
		fatPercentLabel.Text = fatPercent.ToString("P2")

		If Not IsNumeric(fatGramsTextBox.Text) Then
			MessageBox.Show("fat grams must be numeric")

		ElseIf Not IsNumeric(caloriesTextBox.Text) Then
			MessageBox.Show("calories must be numeric")

		End If
		
		If fatPercent > 0.3D Then
			message = "This food is high in fat."
			MessageBox.Show(message, "Fat Calculator", _
			MessageBoxButtons.OK, MessageBoxIcon.Information)

		Else
			message = "This food is not high in fat."
			MessageBox.Show(message, "Fat Calculator", _
			MessageBoxButtons.OK, MessageBoxIcon.Information)

		End If

		

		caloriesTextBox.Focus()

	End Sub
End Class



Is This A Good Question/Topic? 0
  • +

Replies To: Fat Calculator

#2 Damage  Icon User is offline

  • Lord of Schwing
  • member icon

Reputation: 57
  • View blog
  • Posts: 1,243
  • Joined: 05-June 08

Re: Fat Calculator

Posted 14 September 2008 - 11:47 PM

Your error checking is a tiny bit off. You need to place it before there's any possibility of the checkboxes having been used, i would put it directly after your variable declarations, I guess you could do it before to optimize memory management but I don't think thats really an issue.

Plus you need to stop the program execution if the text is not numeric otherwise the error will still occur, try changing it to this
 If Not IsNumeric(fatGramsTextBox.Text) Then
			MessageBox.Show("fat grams must be numeric")
exit sub

		ElseIf Not IsNumeric(caloriesTextBox.Text) Then
			MessageBox.Show("calories must be numeric")
exit sub
		End If


This post has been edited by Damage: 14 September 2008 - 11:47 PM

Was This Post Helpful? 0
  • +
  • -

#3 bravo659  Icon User is offline

  • D.I.C Head

Reputation: -3
  • View blog
  • Posts: 181
  • Joined: 18-May 08

Re: Fat Calculator

Posted 15 September 2008 - 11:01 AM

View PostDamage, on 14 Sep, 2008 - 11:47 PM, said:

Your error checking is a tiny bit off. You need to place it before there's any possibility of the checkboxes having been used, i would put it directly after your variable declarations, I guess you could do it before to optimize memory management but I don't think thats really an issue.

Plus you need to stop the program execution if the text is not numeric otherwise the error will still occur, try changing it to this
 If Not IsNumeric(fatGramsTextBox.Text) Then
			MessageBox.Show("fat grams must be numeric")
exit sub

		ElseIf Not IsNumeric(caloriesTextBox.Text) Then
			MessageBox.Show("calories must be numeric")
exit sub
		End If



Yes that did worked great, I appreciate for your help. I did placed it after the variables declaration but it did returned that same error so moved it after the calculations and still got the same error, the part that I was missing was the Exit Sub, now I know and will document as my reference.
This time I did placed after the variable declaration and included the Exit Sub after each line of messagebox.show works like a charm.
Thanks again for your help, really do appreciate it.

This post has been edited by bravo659: 15 September 2008 - 11:03 AM

Was This Post Helpful? 0
  • +
  • -

#4 Damage  Icon User is offline

  • Lord of Schwing
  • member icon

Reputation: 57
  • View blog
  • Posts: 1,243
  • Joined: 05-June 08

Re: Fat Calculator

Posted 15 September 2008 - 02:44 PM

not a problem, glad i could help
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1