Results 1 to 9 of 9

Thread: [RESOLVED] Help with BMI program

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2014
    Location
    Tampa
    Posts
    5

    Resolved [RESOLVED] Help with BMI program

    Hi, I am running into a problem with my code, but I am not sure where.

    I have tried putting the formula differently and even splitting it up.

    Please help, lol.

    Code:
    Module Module1
    
        Sub Main()
            'Declarations
            Dim intWeight As Integer = 0
            Dim intHeight As Integer = 0
            Dim dblBMI As Double = 0.0
            Dim strEval As String = ""
    
            'Get Height and Weight
            getNumbers(intHeight, intWeight)
    
            'Calculate the BMI
            calculateBMI(intWeight, intHeight, dblBMI)
            bmiEval(dblBMI, strEval)
    
            'Display BMI
            displayBMI(strEval, dblBMI)
            terminateProgram()
    
        End Sub
    
        Private Sub getNumbers(ByRef intHeight As Integer, ByRef intWeight As Integer)
    
            Console.Write("What is your height in inches? ")
            intHeight = CInt(Console.ReadLine())
            Console.Write("What is your weight in pounds? ")
            intWeight = CInt(Console.ReadLine())
    
        End Sub
    
        Private Sub calculateBMI(ByVal intHeight As Integer, ByVal intWeight As Integer, ByRef dblBMI As Double)
    
            dblBMI = (intWeight * 703) / (intHeight * intHeight)
    
        End Sub
    
        Private Sub bmiEval(ByVal dblBMI As Double, ByRef strEval As String)
    
            If dblBMI >= 30.0 Then
                strEval = "Obese"
            ElseIf dblBMI >= 25.0 Then
                strEval = "Overweight"
            ElseIf dblBMI >= 18.5 Then
                strEval = "Normal Weight"
            ElseIf dblBMI >= 0 Then
                strEval = "Underweight"
            Else
                strEval = "Cannot find BMI. Please try again."
            End If
    
        End Sub
    
        Private Sub displayBMI(ByVal strEval As String, ByVal dblBMI As Double)
    
            Console.WriteLine()
            Console.WriteLine("Your BMI is " & dblBMI)
            Console.WriteLine("Evaluation: " & strEval)
    
        End Sub
    
        Private Sub terminateProgram()
    
            Console.WriteLine()
            Console.Write("Press the enter key to terminate the program.")
            Console.Read()
    
        End Sub
    
    End Module

  2. #2
    Frenzied Member KGComputers's Avatar
    Join Date
    Dec 2005
    Location
    Cebu, PH
    Posts
    2,020

    Re: Help with BMI program

    Hi, I am running into a problem with my code, but I am not sure where
    Can you describe the problem/issue properly?
    CodeBank: VB.NET & C#.NET | ASP.NET
    Programming: C# | VB.NET
    Blogs: Personal | Programming
    Projects: GitHub | jsFiddle
    ___________________________________________________________________________________

    Rating someone's post is a way of saying Thanks...

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,344

    Re: Help with BMI program

    Quote Originally Posted by KGComputers View Post
    Can you describe the problem/issue properly?
    Hear, hear. What do you expect to happen and what actually happens? If an exception is thrown, what's the error message?

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2014
    Location
    Tampa
    Posts
    5

    Re: Help with BMI program

    It keeps on calculating wrong, but no error messages though. I expect the answer to be 19.59 or somewhere around that spot, but idk where its going wrong at. The formula should work...

    Name:  wrong.jpg
Views: 311
Size:  17.1 KB
    Last edited by koomie; Apr 4th, 2014 at 12:07 PM.

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2014
    Location
    Tampa
    Posts
    5

    Re: Help with BMI program

    Any help or clues/hints would be greatly appreciated.

    This is due on monday

  6. #6
    Addicted Member
    Join Date
    Oct 2013
    Posts
    212

    Re: Help with BMI program

    This is the line that causes the problem:

    Code:
     getNumbers(intHeight, intWeight)
    change it like this:

    Code:
     getNumbers( intWeight,intHeight)
    Name:  BMI.jpg
Views: 362
Size:  19.1 KB
    Last edited by Atenk; Apr 4th, 2014 at 06:39 PM.

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2014
    Location
    Tampa
    Posts
    5

    Re: Help with BMI program

    Why would the positioning matter? (First time programmer ^^)

    Edit: Also thank you! It worked like you said
    Last edited by koomie; Apr 4th, 2014 at 07:49 PM.

  8. #8
    Addicted Member
    Join Date
    Oct 2013
    Posts
    212

    Re: [RESOLVED] Help with BMI program

    I wanted to get by calculation, and not by code, the first wrong result = 2.7856185536973369850241827399336, so I changed the formula
    like this: (68 * 703) / (131 * 131); and I got it.
    Then I was convinced that one argument in one of the sub procedures was inverted.

    In fact the argument to change, wasn't that In getNumbers, even if that gives the correct result.

    The argument that should be inverted is that in calculateBMI:

    Code:
         Private Sub calculateBMI(ByVal intHeight As Integer, ByVal intWeight As Integer, ByRef dblBMI As Double)
    
            dblBMI = (intWeight * 703) / (intHeight * intHeight)
    
        End Sub
    The right code is therefore:

    Code:
        Private Sub calculateBMI( ByVal intWeight As Integer, ByVal intHeight As Integer, ByRef dblBMI As Double)
    If arguments must figure in a sub procedure, two things must be respected.

    1-When the arguments are passed, they must respect the same number of arguments, with the same datatypes
    2-the order of those arguments must also be respected.
    Last edited by Atenk; Apr 4th, 2014 at 08:49 PM.

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2014
    Location
    Tampa
    Posts
    5

    Re: [RESOLVED] Help with BMI program

    Okay, I am pretty sure I fully understand this problem now.

    I will have to write these down in my notes so that I will remember these two rules and not make the same mistake again

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