Page 1 of 1

Error Trap

Posted: Mon Jan 28, 2019 12:07 pm
by MechWithoutTech
2nd Post (Yay)
I'm making a BMI calculation app and I'm trying to trap a possible error where the user does not enter anything into the input fields, and when the height is zero(as BMI is weight divided by height squared and you can't divide by zero). The following code is for the calculation button where I'm trying to trap the errors spoken above where "bmiField" is the final output field. For some reason, always get an error at the duo of "put" lines. What's wrong with it?

global x
on mouseUp

if text of field "heightField" is not "0" or field "heightField" is not empty or field "weightField" is not empty then
put field("heightField") * field("heightField") into x
put field("weightField") / x into field "bmiField"

else
put("Enter proper height please.") into field "bmiField"
end if
end mouseUp

Re: Error Trap

Posted: Mon Jan 28, 2019 12:39 pm
by jmburnod
HI MechWithoutTech,
You can get the field content directly. Parenthesis seems the problem here.
You probably have to check if an entry is a number

Code: Select all

global x
on mouseUp
   --      if text of field "heightField" is not "0" or field "heightField" is not empty or field "weightField" is not empty then
   --   if field "heightField" <> "0" or field "heightField" <> empty or fld "weightField" <> empty  then -- also works
   if field "heightField" * fld "weightField" > 0 then -- also works
      put field "heightField" * field "heightField" into x
      put field "weightField" / x into field "bmiField"
   else
      put "Enter proper height please." into field "bmiField"
   end if
end mouseUp
best regards
Jean-Marc

Re: Error Trap

Posted: Mon Jan 28, 2019 1:07 pm
by jmburnod
With a number check and message for use when there is a entry mistake

Code: Select all

global x
on mouseUp
   put fld "heightField" into tH
   put fld "weightField" into tW
   put  (tH is a number) into tBoolNumH
   put  (tW is a number) into tBoolNumW
   if tBoolNumH and tBoolNumW then
      put tH * tW into x
      put tW  / x into field "bmiField"
   else
      if not tBoolNumH  and not tBoolNumW then put "Enter proper height and weight please." into field "bmiField"
      else
         if not tBoolNumH then  put "Enter proper height please." into field "bmiField"
         else put "Enter proper weight please." into field "bmiField"
      end if
   end if
end mouseUp

Re: Error Trap

Posted: Mon Jan 28, 2019 2:28 pm
by bogs
The only other possible issue I see is that you are using "or" instead of "and", when you want to make sure that all fields have information.

Using "or" in the statements you list means that if any of those fields are valid, proceed. Using "and" instead would mean field 1 and 2 and 3 have to have information.

The parenthesis as Jean-Marc pointed out have to include both parts of the control, like

Code: Select all

put (field "heightField") * (field "weightField") into x
 /* I assume the second variable above was supposed to be weight, 
 not height times height, but you get the idea... */
...or you could have left them out entirely if you want the math to proceed as it is written. If you need a line to evaluate in a specific order of operations, that is when you use parenthesis to tell that to the compiler.

Jean-Marc's solution is the way to go though.