Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I am making a Inductor Calculator t

Status
Not open for further replies.

Creeden

Programmer
May 9, 2002
16
0
0
CA
I am making a Inductor Calculator that contains 12 Edit boxes and 5 Buttons. 5 for series calculation and an answer box. 5 for Parallel calculations and an answer box. The problem is if I use only say 3 of the 5 inputs I get an error that "" is not a valid floating point value. My code is like this for the series side.

Code:
var  
Form1: TForm1;  
first: Real;  
second: Real;  
third: Real;  
fourth: Real;  
fifth: Real;  
seventh: Real;  
eighth: Real;  
ninth: Real;  
tenth: Real;  
eleventh: Real;  
answer: Real;
procedure TForm1.CalcsClick(Sender: TObject);
begin  
first:=StrtoFloat(Edit1.Text);  
second:=StrtoFloat(Edit2.Text);  
third:=StrtoFloat(Edit3.Text);  
fourth:=StrtoFloat(Edit4.Text);  
fifth:=StrtoFloat(Edit5.Text);  
Edit6.Text:=FloattoStr(first+second+third+fourth+fifth);
end;
and this for the parallel side..
Code:
procedure TForm1.CalcpClick(Sender: TObject);
begin  
seventh:=StrtoFloat(Edit7.Text);  
eighth:=StrtoFloat(Edit8.Text);  
ninth:=StrtoFloat(Edit9.Text);  
tenth:=StrtoFloat(Edit10.Text);  
eleventh:=StrtoFloat(Edit11.Text);  
Edit12.Text:=FloattoStr(1/(1/seventh+1/eighth+1/ninth+1/tenth+1/eleventh));
end;
The program works perfect if all the boxes are filled. Any suggestions are appreciated.
It can be seen at.. on the New Projects link.
Thanks,
Bob. [pipe]
 
hi,

of course they will work when they are filled, if the edit boxes aren't filled they have a nil value. The StrToFloat or StrToInt will see them as '' and thats not a valid integer or float.
The trick to avoid this problem you have 2 posibilties.
The first is to put default 0 in all the edit boxes. this can be done with edit1.text := 0
The other approach is to check if a variable has a value.
if edit1.text <> '' then
first := StrToFloat(edit11.text)
else
first := 0;

Make sure that you can only put numbers in the editboxes. If a ussr puts A in an edit box StrToFloat will try to convert them also.

Steph [bigglasses]
 
Hello Svanhooft.
Been there, done that.
I can't put zero's in the unused Editboxes or I get a divide by Zero error.
Code:
procedure TForm1.CalcpClick(Sender: TObject);
begin  
seventh:=StrtoFloat(Edit7.Text);  
eighth:=StrtoFloat(Edit8.Text);  
ninth:=StrtoFloat(Edit9.Text);  
tenth:=StrtoFloat(Edit10.Text);  
eleventh:=StrtoFloat(Edit11.Text);  
Edit12.Text:=FloattoStr(1/(1/seventh+1/eighth+1/ninth+1/tenth+1/eleventh));<---Here.
end;
Gotta find another way
Thanks
Bob ;-)
 
What do you want to happen if the user doesn't enter a valid floating point value into one of the edit boxes? An error message? Substitution of another value?
 
Do a check on the content of the TEdits.

Firstly, default them to 0 (so length > 0). You could use the tag property of each TEdit.

if (length(edit2.caption) > 0) and
(floattostr(edit2.caption)) <> 0 then
edit2.tag := 1
else
edit2.tag := 0;

Later
DivValue := 0;
if edit1.tag = 1 then
divValue := DivValue + 1/floattostr(edit1.text);
if edit2.tag = 1 then
divValue := DivValue + 1/floattostr(edit2.text);
etc
if DivValue <> 0 then
Edit12.Text:=FloattoStr(1/DivValue)
else Edit12.Text := 'no value';


Would this work for you?

lou

[penguin]
 
BTW, you could combine the if's if you want, instead of using the tag property

DivValue := 0;
if (length(edit2.caption) > 0) and
(floattostr(edit2.caption)) <> 0 then
divValue := DivValue + 1/floattostr(edit1.text);
 
If you just want to trap a conversion error, enclose the block in a try...except:
Code:
procedure TForm1.CalcpClick(Sender: TObject);
begin  
	try
		seventh:=StrtoFloat(Edit7.Text);  
		eighth:=StrtoFloat(Edit8.Text);  
		ninth:=StrtoFloat(Edit9.Text);  
		tenth:=StrtoFloat(Edit10.Text);  
		eleventh:=StrtoFloat(Edit11.Text);  
		Edit12.Text:=FloattoStr(1/
                       (1/seventh+1/eighth+1/
                       ninth+1/tenth+1/eleventh));
	except
		//error message
	end;
end;
 
Change ifs to:-

DivValue := 0;

if (length(edit2.text) > 0) then
if (StrToFloat(edit2.text)) <> 0 then
divValue := DivValue + 1/StrToFloat(edit2.text);

if (length(edit3.text) > 0) then
if (StrToFloat(edit3.text)) <> 0 then
divValue := DivValue + 1/StrToFloat(edit3.text);
:
etc

With this you don't have to default the boxes to 0.

p.s. should of fixed all the typos I made too.
 
Svanhooft, mikeEd, weez.
Thanks for all the Quick replies.
I still got the '' is not a valid floating point value.
I ended up doing this...

Code:
procedure TForm1.CalcpClick(Sender: TObject);
begin
  seventh:=StrtoFloat(Edit7.Text);
  eighth:=StrtoFloat(Edit8.Text);
  ninth:=StrtoFloat(Edit9.Text);
  tenth:=StrtoFloat(Edit10.Text);
  eleventh:=StrtoFloat(Edit11.Text);
    if edit9.Text = '0' then
   Edit12.Text:=FloattoStr(1/(1/seventh+1/eighth))
 else
  if edit10.text = '0' then
   Edit12.Text:=FloattoStr(1/(1/seventh+1/eighth+1/ninth))
 else
  if edit11.text = '0' then
   Edit12.Text:=FloattoStr(1/(1/seventh+1/eighth+1/ninth+1/tenth))
 else
   Edit12.Text:=FloattoStr(1/(1/seventh+1/eighth+1/ninth+1/tenth+1/eleventh));
 end;

I can live with this solution.
Thanks for all your help. If any of you want to play
with it I can put a zip file of the project on my site.
Thanks again.
Bob
 
To ensure to enter only positive values in the edit box:

event OnKeyPress

begin
if ((Key < '0') or (Key > '9')) and (Key <> #13)
then Key = #0; //backspace not numeric character
end;


Steven van Els
SAvanEls@cq-link.sr
 
In addition to Steven's last post:

try
seventh:=StrtoFloat(Edit7.Text); // or strtoint(...)?
except
seventh := 1; // or 0 if adding?
end;

This has to be done for all values ofcourse, as I have users sitting at the other side of the monitor, so I know for sure they (and even myself ;-) ) are to add some funny stuff into the editboxes...
The first answer that offered the try...except method is sure to fail, as it wil only catch the first bad creep, and all other values wil be unknown!

HTH, TonHu
 
Thanks all.
I have it working good now.
Bob
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top