Jump to content

Need help with BMI Calc


Recommended Posts

Hello! My first time on this forum. I'm a newbie when it comes to autoit.

I've been trying to figure out a BMI Calculator. So I have 2 questions.

*First: How do I get a number from GUICtrlCreateInput to another GUICtrlCreateInput?

*Second: How do I calculate this right? (It just says "Result" when I press "Calculate.")

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <file.au3>
#include <IE.au3>

GUICreate("BMI Calculator", 300, 138)
GUICtrlCreateLabel("Length in meter", 8, 8, 100, 20)
GUICtrlCreateLabel("Weight in kilogram", 8, 44, 100, 20)
GUICtrlCreateLabel("Underweight  Ideal       Overweight        Obesity", 8, 72, 300, 20)
GUICtrlCreateLabel("< 19     |   19-25   |   26-30   |   > 30", 8, 88, 300, 20)
GUICtrlCreateLabel("Result", 8, 110, 100, 20)
$insertLength = GUICtrlCreateInput("", 108, 8, 35, 20)
$insertWeight = GUICtrlCreateInput("", 108, 44, 35, 20)
$result = GUICtrlCreateInput("", 108, 110, 35, 20)
$calcBMI = GUICtrlCreateButton("Calculate", 192, 8, 100, 20)
GUISetState(@SW_SHOW)

$math = $insertLength * $insertLength / $insertWeight

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

        Case $calcBMI

            MsgBox(0, "Result", GUICtrlRead($math))

    EndSwitch
WEnd
Link to comment
Share on other sites

1. GUICtrlRead($Input1) then GUICtrlSetData($Input2)

2. $math is not a control (that's why you can't get anything read from there) - use: MsgBox(0, "Result", $math)

Not on your dot-point but VITAL for your script: you need to use GUICtrlRead($insertLength) ... on your $math calculus.

Something to NEVER forget: if it is a control handle (e.g. $insertLength = identifier of an Inputbox) you need to use GUICtrlRead if you want to get its value.

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

1. GUICtrlRead($Input1) then GUICtrlSetData($Input2)

2. $math is not a control (that's why you can't get anything read from there) - use: MsgBox(0, "Result", $math)

Not on your dot-point but VITAL for your script: you need to use GUICtrlRead($insertLength) ... on your $math calculus.

Something to NEVER forget: if it is a control handle (e.g. $insertLength = identifier of an Inputbox) you need to use GUICtrlRead if you want to get its value.

I'm sorry but nothing of those answeres are working (sadly) :)

When I try the GuiCtrlRead($insert1) then GuiCtrlSetData($insert2) it says "ERROR: GUICtrlSetData() [built-in] called with wrong number of args."

When I try the second answere it give me the result of anything and says: "-1.#IND"

Link to comment
Share on other sites

I'm sorry but nothing of those answeres are working (sadly) :)

When I try the GuiCtrlRead($insert1) then GuiCtrlSetData($insert2) it says "ERROR: GUICtrlSetData() [built-in] called with wrong number of args."

When I try the second answere it give me the result of anything and says: "-1.#IND"

You have to assign the GuiCtrlRead($insert1) to a variable and then use the variable in the GuiCtrlSetData($insert2) like so:

$var = GuiCtrlRead($insert1)
GuiCtrlSetData($insert2, $var)

Dating a girl is just like writing software. Everything's going to work just fine in the testing lab (dating), but as soon as you have contract with a customer (marriage), then your program (life) is going to be facing new situations you never expected. You'll be forced to patch the code (admit you're wrong) and then the code (wife) will just end up all bloated and unmaintainable in the end.

Link to comment
Share on other sites

You have to assign the GuiCtrlRead($insert1) to a variable and then use the variable in the GuiCtrlSetData($insert2) like so:

$var = GuiCtrlRead($insert1)
GuiCtrlSetData($insert2, $var)

It works fine when I try out your code. The insert1 goes to insert2.

But when I change it to:

$var = GuiCtrlRead($math)
GuiCtrlSetData($insert2, $var)

Then in insert2 the text "Result" comes. I have no idea what this is :S

I have tried this:

GuiCtrlRead($math)
GuiCtrlSetData(-1, $result)

But then it gives me the result of "11" and not the result I want from:

$math = GuiCtrlRead($insert1)*GuiCtrlRead($insert1)/GuiCtrlRead($insert2)
Link to comment
Share on other sites

You are trying - again - to "GUICtrlRead" a variable and I told you that it doesn't work. (GuiCtrlRead($math))

This line is wrong:

$math = GuiCtrlRead($insert1)*GuiCtrlRead($insert1)/GuiCtrlRead($insert2)
It should be:
$math = GuiCtrlRead($insert2)/(GuiCtrlRead($insert1)*GuiCtrlRead($insert1))

SNMP_UDF ... for SNMPv1 and v2c so far, GetBulk and a new example script

wannabe "Unbeatable" Tic-Tac-Toe

Paper-Scissor-Rock ... try to beat it anyway :)

Link to comment
Share on other sites

*First: How do I get a number from GUICtrlCreateInput to another GUICtrlCreateInput?

*Second: How do I calculate this right? (It just says "Result" when I press "Calculate.")

This works for me!

#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <file.au3>
#include <IE.au3>
GUICreate("BMI Calculator", 300, 138)
GUICtrlCreateLabel("Length in meter", 8, 8, 100, 20)
GUICtrlCreateLabel("Weight in kilogram", 8, 44, 100, 20)
GUICtrlCreateLabel("Underweight  Ideal       Overweight        Obesity", 8, 72, 300, 20)
GUICtrlCreateLabel("< 19              |  19-25  |      26-30     |     > 30", 8, 88, 300, 20)
GUICtrlCreateLabel("Result", 8, 110, 100, 20)
$insertLength = GUICtrlCreateInput("", 108, 8, 35, 20)
$insertWeight = GUICtrlCreateInput("", 108, 44, 35, 20)
$result = GUICtrlCreateInput("", 108, 110, 35, 20)
$result2 = GUICtrlCreateLabel("Waiting ..", 155, 110, 50, 20)
$calcBMI = GUICtrlCreateButton("Calculate", 192, 8, 100, 20)
GUISetState(@SW_SHOW)
$math = $insertLength * $insertLength / $insertWeight
While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $calcBMI
            $math = Round(GUICtrlRead($insertWeight) / (GUICtrlRead($insertLength) * GUICtrlRead($insertLength)), 2)
            GUICtrlSetData($result, $math)
            Switch $math
                Case 1 To 17.5
                    $msg = "Anorexia"
                Case 17.6 To 19.1
                    $msg = "Underweight"
                Case 19.2 To 25.8
                    $msg = "Normal"
                Case 25.9 To 27.3
                    $msg = "Marginally overweight"
                Case 27.4 To 32.3
                    $msg = "Overweight"
                Case 32.3 To 60
                    $msg = "Obese !"
                Case Else
                    $msg = "I give up!"
            EndSwitch
            GUICtrlSetData($result2, $msg)
    EndSwitch
WEnd
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...