I'm trying to make a blood pressure category checker in Delphi 7, and I just started learning the Delphi Programming Language a few weeks ago.
The problem is every time I put numbers above than 120, the label caption always displaying normal.
Here is my code:
procedure TForm1.Button1Click(Sender: TObject);
var a,b:real;
begin
a:=strtofloat(edit1.Text);
if (a<120) then label1.caption:='optimal'
else if (a>120) then label1.caption:='normal'
else if (a<130) then label1.caption:='normal'
else if (a>130) then label1.caption:='normal high'
else if (a<140) then label1.caption:='normal high'
else if (a>140) then label1.caption:='grade 1 hypertension'
else if (a<160) then label1.caption:='grade 1 hypertension'
else if (a>160) then label1.caption:='grade 2 hypertension'
else if (a<180) then label1.caption:='grade 2 hypertension'
else if (a>181) then label1.caption:='grade 3 hypertension'
end;
end.
It may be some common mistake but I still can't figure it out myself, any kind of help would help a lot, thank you.
Your code is incorrect. It only checks two values, which are < 120
and > 120
. Nothing else is ever tested.
When looking for a value within a range, you need to test for both ends of the range, like this:
procedure TForm1.Button1Click(Sender: TObject);
var
a: real;
begin
a:=strtofloat(edit1.Text);
if (a < 120) then
Label1.Caption := 'Optimal'
else if (a >= 120) and (a < 130) then
Label1.Caption := 'Normal'
else if (a >= 130) and (a < 150) then
Label1.Caption := 'Normal high'
else if (a >= 150) and (a < 160) then
Label1.Caption := 'Grade 1 hypertension'
else if (a >= 160) and (a < 170) then
Label1.Caption := 'Grade 2 hypertension'
else if (a >= 170) and (a < 180) then
Label1.Caption := 'Grade 3 hypertension'
else
Label1.Caption := 'Heart exploded from pressure';
end;
(Your ranges are all really confused. You'll need to adjust my code to meet your actual range requirements, but what I've posted should get you started.)
As it's unlikely that someone is going to record a blood pressure as a floating point value (not likely your BP is going to be 121.6/97.2), you probably want to use an integer instead, which would make the code easier.
procedure TForm1.Button1Click(Sender: TObject);
var
a: Integer;
begin
a := StrToInt(Edit1.Text);
case a of
0..119: Label1.Caption := 'Optimal'; // Probably want to test for too low
120..129: Label1.Caption := 'Normal';
130..149: Label1.Caption := 'Normal high';
150..159: Label1.Caption := 'Grade 1 hypertension';
160..169: Label1.Caption := 'Grade 2 hypertension';
else
Label1.Caption := 'Over 170! Danger!'
end;
end;