Results 1 to 16 of 16

Thread: BMI Program

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    25

    BMI Program

    I'm making a program which calculates a user's BMI (Body Mass Index). I am fairly new to VB.NET so please bare with me.

    This is what I have so far...

    Code:
    Public Class BMI
        Inherits System.Windows.Forms.Form
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            'an integer is one or a group of characters has a value ranging from 0 to 255
            Dim number1 As Decimal 'user enters height
            Dim number2 As Integer 'user enters weight
            Dim answer As Decimal 'signals that the answer must be a decimal
    
            'Val returns the numbers contained in a string as a numeric value of appropriate type
            number1 = Val(TextBox1.Text) 'number 1 equals TextBox1
            number2 = Val(TextBox2.Text) 'number 2 equals TextBox2
    
            answer = number2 / (number1 * 2) 'this shows what the calculation is and both number1 and number2 will be added together
    
            MsgBox(answer) 'a message box will appear with the user's BMI
    
        End Sub
    
    End Class
    The concept works absolutely fine, however, I want to extend it a bit.

    I'd like to include some sort of validation, so the user is permitted only to use numbers and not letters. So if they did enter a letter or having said that, if they enter anything in the wrong format, an error message will appear.

    I'd also like the pop-up box with the user's BMI to say whether they are under, over, or a healthy weight.

    Could anybody help me with this?

  2. #2
    Junior Member
    Join Date
    Mar 2009
    Location
    Argentina
    Posts
    25

    Re: BMI Program

    You could use a MaskedTextBox, setting the mask to the format you need.

    By the way, the body mass index is calculated as Weight / (Height * Height)
    http://en.wikipedia.org/wiki/Body_mass_index

  3. #3
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: BMI Program

    'an integer is one or a group of characters has a value ranging from 0 to 255
    You may want to work on your definition of an integer.

  4. #4
    Master Of Orion ForumAccount's Avatar
    Join Date
    Jan 2009
    Location
    Canada
    Posts
    2,802

    Re: BMI Program

    Quote Originally Posted by keystone_paul View Post
    You may want to work on your definition of an integer.
    Well he's not exactly wrong... just unaware. An 8bit unsigned integer has a range of 0-255. In VB.net however its a 32 bit signed integer so it's range is −2,147,483,648 to +2,147,483,647...

  5. #5
    PowerPoster keystone_paul's Avatar
    Join Date
    Nov 2008
    Location
    UK
    Posts
    3,327

    Re: BMI Program

    An 8bit unsigned integer has a range of 0-255. In VB.net however its a 32 bit signed integer so it's range is −2,147,483,648 to +2,147,483,647...
    Indeed, however as its a VB.net program and preceding the declaration of a variable of type integer, it is at best misleading.

    In my view the only thing worse than a program without comments is one with misleading comments.

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    25

    Re: BMI Program

    Right, I've used masked text boxes for height and weight as KaDMiO said to do.

    Now, I need to know how I can adjust the BMI decimal answer so that there is only one number after the decimal point rather than something like 18.4444444444444444444444.

    And, how do I go about doing validation, so the user is told whether they are over, under, or a healthy weight?

  7. #7
    Junior Member
    Join Date
    Mar 2009
    Location
    Argentina
    Posts
    25

    Re: BMI Program

    To do that, use the Math.Round function.

    For the over, under or healthy weight, If clauses.

  8. #8
    Member Jericho's Avatar
    Join Date
    Mar 2009
    Posts
    36

    Re: BMI Program

    If your having further problems theres plenty examples of BMI calculators on Youtube and Google.

  9. #9

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    25

    Re: BMI Program

    Quote Originally Posted by Jericho View Post
    If your having further problems theres plenty examples of BMI calculators on Youtube and Google.
    That's very helpful. Funnily enough, I have looked around on the internet but couldn't find anything which helped me.

    Could anyone show me how to implement the Math.Round function into the code I've given? Because at the moment I'm having trouble doing it despite looking on Google for ages. I thought it would be easy to do.

  10. #10
    Frenzied Member
    Join Date
    Mar 2005
    Location
    Sector 001
    Posts
    1,577

    Re: BMI Program

    The second parameter sets how many decimal places.
    Code:
    Dim tempDec As Decimal
    tempDec = Math.Round(18.4444444444444444444444, 1)
    VB 2005, Win Xp Pro sp2

  11. #11
    Member Jericho's Avatar
    Join Date
    Mar 2009
    Posts
    36

    Re: BMI Program

    Quote Originally Posted by ACM1989 View Post
    That's very helpful. Funnily enough, I have looked around on the internet but couldn't find anything which helped me.

    Could anyone show me how to implement the Math.Round function into the code I've given? Because at the moment I'm having trouble doing it despite looking on Google for ages. I thought it would be easy to do.

    Really? Cause I just looked then and look what I found!

    http://www.vbtutor.net/vb2008/vb2008_lesson7.html

  12. #12

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    25

    Re: BMI Program

    No help mate. It doesn't answer the question I asked.

  13. #13
    Junior Member
    Join Date
    Mar 2009
    Location
    Argentina
    Posts
    25

    Re: BMI Program

    answer = Math.Round(number2 / (number1 * number1),1)

    That should work.

  14. #14
    Hyperactive Member Runesmith's Avatar
    Join Date
    Oct 2008
    Posts
    399

    Re: BMI Program

    If you are rounding just because you want to limit the numbers displayed after decimal point, you can also use the Format function to do that, and still retain the multiple decimals for accuracy in other calcs.

    Eg.
    Code:
    answer = WeightInKg / ((HeightInCm /100)^2)
    MsgBox("BMI: " & Format(answer, "#,##0.00") & " kg/m2", MsgBoxStyle.OkOnly, "BMI Result")
    Runesmith

    The key to getting the right answer is asking the right question

    I just realized: good health is merely the slowest possible rate at which one can die

  15. #15

    Thread Starter
    Junior Member
    Join Date
    Mar 2009
    Posts
    25

    Re: BMI Program

    Thanks guys, I used KaDMio's and it works just fine.

    I'm immensely stuggling though to do IF statements for a message box. I need the message box to not only display the user's BMI but also whether they are healthy or over or underweight.

  16. #16
    New Member
    Join Date
    Jun 2008
    Location
    Blackpool, England
    Posts
    4

    Re: BMI Program

    Something like this will probably work..

    Code:
    dim LOWBMI as Integer = 20
    dim HIGHBMI as Integer = 30
    
    if answer < LOWBMI then 
    MsgBox("Start eating more pies, your BMI is " & answer)
    elseif answer > HIGHBMI then 
    MsgBox("You're too fat: your BMI is " & answer)
    else
    MsgBox("You're as normal as it gets: your BMI is " & answer)
    endif

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width