Log in

View Full Version : BMI Calculator


Jess
February 10th, 2013, 12:14 AM
Solved.

//..

I'm creating a BMI calculator for my visual basic class. I'm nearly done.
' Jessica Chen (jzc22)
' This program will calculate the BMI (Body Mass Index) given the inputed
' user weight and height.

' uses:
' lblWeight - Label - prompt for where user should enter weight, in kilograms
' lblHeight - Label - prompt for where user should enter height, in meters
' weightTB - TextBox - used to get user input for weight
' heightTB - TextBox - used to get user input for height
' calcBtn - Button - used by user to start task of calulating the BMI
' outputTB - TextBox - ReadOnly(True),Multiline(True),VerticalScroll - used to show user
' result of calculation and any warnings

Option Strict On
Option Explicit On
Option Infer Off
Option Compare Binary


Public Class bmiCalculator
Dim counter As Integer = 0
Private Sub calcBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calcBtn.Click
counter += 1
Dim weight As Integer
weight = CInt(weightTB.Text)
Dim height As Integer
height = CInt(heightTB.Text)
Dim result As Double
If weight > 0 And height > 0 Then
result = (weight / height ^ 2)
outputTB.AppendText("Your BMI is " & CDbl(result) & "." & vbCrLf)
If result < 15.0 Then
outputTB.AppendText("Warning: Dangerous underweight condition!" & vbCrLf)
ElseIf result > 40.0 Then
outputTB.AppendText("Warning: Dangerous overweight condition!" & vbCrLf)
End If
If result >= 15.0 And result < 16.0 Then
outputTB.AppendText("Severely underweight" & vbCrLf)
End If
If result >= 16.0 And result < 18.5 Then
outputTB.AppendText("Underweight" & vbCrLf)
End If
If result >= 18.5 And result < 25.0 Then
outputTB.AppendText("Normal" & vbCrLf)
End If
If result >= 25.0 And result < 30.0 Then
outputTB.AppendText("Overweight" & vbCrLf)
End If
If result >= 30.0 And result < 35.0 Then
outputTB.AppendText("Moderately obese" & vbCrLf)
End If
If result >= 35.0 And result < 40.0 Then
outputTB.AppendText("Severely obese" & vbCrLf)
End If
outputTB.AppendText("Number of BMI Calculations: " & CStr(CDbl(counter)) & vbCrLf)
Else
If weight < 0 And height < 0 Then
outputTB.AppendText("Inputed height or weight not valid!" & vbCrLf)
End If
End If
End Sub
End Class

All I need help on is how to get the output text to replace the current text if I put in new values. In other words, if I put in 65 for weight and 1.2 for height and get a result, how do I get it to clear when I put in new values?

ethanf93
February 10th, 2013, 01:40 PM
Add
outputTB.Text = ""
to where you want the text box cleared.

Jess
February 10th, 2013, 02:02 PM
Thank you! That works.

I'm pretty much done here, but is there any way I could improve my code (ex, did I put in too many If Then statements?)? Or is it fine?

final code:

Public Class bmiCalculator
Dim counter As Integer = 0 ' count the number of times BMI was calculated
Private Sub calcBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles calcBtn.Click
counter += 1
Dim weight As Integer
weight = CInt(weightTB.Text)
Dim height As Integer
height = CInt(heightTB.Text)
Dim result As Double
If weight > 0 And height > 0 Then
result = (weight / height ^ 2) ' the calculation (formula for getting BMI)
If result < 15.0 Then
outputTB.Text = ""
outputTB.AppendText("Your BMI is " & CDbl(result) & vbCrLf & "Warning: Dangerous underweight condition!" & vbCrLf)
ElseIf result > 40.0 Then
outputTB.Text = ""
outputTB.AppendText("Your BMI is " & CDbl(result) & vbCrLf & "Warning: Dangerous overweight condition!" & vbCrLf)
End If
If result >= 15.0 And result < 16.0 Then
outputTB.Text = ""
outputTB.AppendText("Your BMI is " & CDbl(result) & vbCrLf & "Severely underweight" & vbCrLf)
End If
If result >= 16.0 And result < 18.5 Then
outputTB.Text = ""
outputTB.AppendText("Your BMI is " & CDbl(result) & vbCrLf & "Underweight" & vbCrLf)
End If
If result >= 18.5 And result < 25.0 Then
outputTB.Text = ""
outputTB.AppendText("Your BMI is " & CDbl(result) & vbCrLf & "Normal" & vbCrLf)
End If
If result >= 25.0 And result < 30.0 Then
outputTB.Text = ""
outputTB.AppendText("Your BMI is " & CDbl(result) & vbCrLf & "Overweight" & vbCrLf)
End If
If result >= 30.0 And result < 35.0 Then
outputTB.Text = ""
outputTB.AppendText("Your BMI is " & CDbl(result) & vbCrLf & "Moderately obese" & vbCrLf)
End If
If result >= 35.0 And result < 40.0 Then
outputTB.Text = ""
outputTB.AppendText("Your BMI is " & CDbl(result) & vbCrLf & "Severely obese" & vbCrLf)
End If
outputTB.AppendText("Number of BMI Calculations: " & CStr(CDbl(counter)) & vbCrLf) ' reveals the number of calculations so far
Else
If weight < 0 Or height < 0 Then
outputTB.Text = ""
outputTB.AppendText("Inputed height or weight not valid!" & vbCrLf) ' if inputed height or weight is under 0 then this will appear
End If
End If
End Sub
End Class

ethanf93
February 10th, 2013, 05:36 PM
This stuff is more about style- of you have something working you should make a copy of that before you look at this. (It's better to have a program with style issues that works than one with perfect style that doesn't work)

I would put outputTB.Text = "" under the line result = (weight / height ^ 2) ' the calculation (formula for getting BMI)and then delete the other outputTB.Text = ""s.

The reason for this is that if this is something that happens for every case, you don't need to have it in every case. If/else if blocks are for conditional code, but this code isn't conditional. This also helps since you don't have to copy paste things around so much. I would also move the code that outputs the BMI number to there (since again that doesn't really depend on what the BMI is.)

I'm not particularly versed in VB syntax but in the If weight and height are valid block, I would use Else Ifs rather than If/End If/If... The BMI can't be both more than 40 and between 15 and 16.

Gigablue
February 10th, 2013, 05:43 PM
You can use ElseIfs instead of separate if statements. Other than that, the code is fine.

Jess
February 10th, 2013, 05:45 PM
Sigh, I tried Elseifs but it got really confusing. And I already turned in my assignment...oh well. At least I won't totally fail. might lose a few points. Thanks anyways :)