Recent

Author Topic: BMI index RadioGroup  (Read 2971 times)

godik

  • New Member
  • *
  • Posts: 23
BMI index RadioGroup
« on: October 26, 2018, 09:37:27 pm »
Hello I have problem my bmi index is kinda broken i dont know why :) And i dont really understant this command
RadioGroup1.Caption:='BMI = '+floattostr(bmi);





procedure TForm1.Button1Click(Sender: TObject);
var heightm,weightkg,bmi:real;
begin
  heightm:=StrToFloat(edit1.text);
  weightkg:=StrToFloat(edit2.text);
  bmi:=weightkg/heightm;
  RadioGroup1.Caption:='BMI = '+floattostr(bmi);
  if bmi<=19.9 then RadioGroup1.ItemIndex:=0;
  if bmi<=24.9 then RadioGroup1.ItemIndex:=1;
  if bmi<=29.9 then RadioGroup1.ItemIndex:=2;
  if bmi<=39.9 then RadioGroup1.ItemIndex:=3;
   if bmi >40 then RadioGroup1.ItemIndex:=4;   

Thanks for all answers

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: BMI index RadioGroup
« Reply #1 on: October 26, 2018, 09:52:44 pm »
numbers are not stored in memory like they are on paper when  you look at them.

 A REAL is a floating point number that is stored in memory in a non human readable format. The FloatToStr creates a
human readable list of letters (characters) so that you can better understand the value when looking at it.

The "Caption" is a string which shows at the top of the group box and what is happening is you are directly telling it
to build a string (a series of letters) of what is inside the '?????' and adding the converted results of the FloatToStr to
create a final single string which will be stored in the "Caption"

 the FloatTostr function could be creating a number with a power at the end "-10" for example. This can be fixed by using a
different formatting function or use the "Str" function which will generate a string with a specified format..

  Str(Hmi, 10:2, OutPutString);
 
 That will create a number string 10 characters long with 2 places to the right after the decimal point.

 If you need more info feel free to come back.
The only true wisdom is knowing you know nothing

Blaazen

  • Hero Member
  • *****
  • Posts: 3237
  • POKE 54296,15
    • Eye-Candy Controls
Re: BMI index RadioGroup
« Reply #2 on: October 26, 2018, 10:08:35 pm »
The height in formula must be squared:
Code: Pascal  [Select][+][-]
  1.   bmi:=weightkg/(heightm*heightm);
or
Code: Pascal  [Select][+][-]
  1.   bmi:=weightkg/power(heightm, 2);

See: https://en.wikipedia.org/wiki/Body_mass_index
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

godik

  • New Member
  • *
  • Posts: 23
Re: BMI index RadioGroup
« Reply #3 on: October 27, 2018, 10:22:05 am »
Thank you guys appreciate it  :)

BrunoK

  • Sr. Member
  • ****
  • Posts: 452
  • Retired programmer
Re: BMI index RadioGroup
« Reply #4 on: October 27, 2018, 10:36:53 am »
Except for the BMI index where Blaazen's indication seems right, the main problem that godik has in his code logic is that the index can be only 3 or 4.

Conditions should be
Code: Pascal  [Select][+][-]
  1. if bmi<=19.9 then RadioGroup1.ItemIndex:=0
  2. else  if bmi<=24.9 then RadioGroup1.ItemIndex:=1 //  <- else for other cases
  3. else etc...
  4. else
  5.   RadioGroup1.ItemIndex:=4;  
  6.  

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: BMI index RadioGroup
« Reply #5 on: October 27, 2018, 03:42:16 pm »
Looks like he messes the number 40 on the last check...

it should be >= 40;

currently it's > 40;
and the one before is  <= 39.9

 that is what I observed anyways.

The only true wisdom is knowing you know nothing

Kays

  • Hero Member
  • *****
  • Posts: 575
  • Whasup!?
    • KaiBurghardt.de
Re: BMI index RadioGroup
« Reply #6 on: October 27, 2018, 04:43:01 pm »
[…] bmi index […]
[…] BMI index […]
Argh! “BMI Index” Dudes, the “I” already stands for “index.”

I just wanna mention, don't forget to implement a plausibility check. The last time I've seen the GP, we measured size and weight, and their program accepted values in the wrong fields, suggesting a BMI of several thousands.
Yours Sincerely
Kai Burghardt

jamie

  • Hero Member
  • *****
  • Posts: 6130
Re: BMI index RadioGroup
« Reply #7 on: October 27, 2018, 08:43:17 pm »
We can always find more stuff wrong! :D

 With that type of code frag, its highly possible there is things like 39.888889 etc taking place
or 20.0000000556 etc... if you know what I mean...
 working with floats are fun since they can't represent the exact number and give you something just a little different..

  Which is why its best to use a Currency or integer multiplied  X 10 for your one fractional digit to the right, that way you
get exactly what you want.
The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018