I.T. Discussion Community!
-Collapse +Expand
Delphi
Search Delphi Group:

Advanced
-Collapse +Expand Delphi To/From
To/FromCODEGuides
-Collapse +Expand Delphi Store
PRESTWOODSTORE

Prestwood eMagazine

December Edition
Subscribe now! It's Free!
Enter your email:

   ► MB LobbyPascal and Delphi Coding BoardDelphi Single User Apps Topic   Print This     

Calculations with blank Edit Boxes

Calculations with blank Edit Boxes in Delphi Single User Apps topic (part of our Pascal and Delphi Coding group).

Quick Search: Calculations   Calculations blank   Calculations blank Edit  
Creeden
 (Inactive)
Paris, Ontario,Canada
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.


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..
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..
Old UBB Archived Link: [URL=http://members.rogers.com/bobsdelphi/]http://members.rogers.com/bobsdelphi/[/URL]

on the New Projects link.
Thanks,
Bob. Confused
 Posted 17 years ago (Thread Starter)
Comment Quote
About Creeden -Collapse +Expand
Visit Profile
Inactive member.
Member does not subscribe to this thread.
Email Not Verified!
Once email is verified, we will review and approve the account.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #6291, 7 replies
Thread Started 5/20/2002 10:08:00 AM
View Counter=13736
Last Reply Posted 6/11/2002 5:49:00 AM)
Location=Paris, Ontario,Canada  
Joined=17 years ago   MB Posts=6  
Angel
 (Inactive)
England
Your error is appearing because your calculation is taking a blank edit box and trying to use its value in the calculation. To safeguard against this, you will need to check each editbox to see if it contains a value or not before assigning it to your variables.

if Edit1.Text = '' then variable := 0 else variable := Edit1.Text;

Something like this should work where the edit box is empty, it will return a value of zero.
 Posted 17 years ago
Comment Quote
About Angel -Collapse +Expand
Visit Profile
Inactive member.
Member does not subscribe to this thread.
Email Not Verified!
Once email is verified, we will review and approve the account.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #6300 (Level 1.1)  Reply to 6291
Thread Started 5/21/2002 12:58:00 AM
View Counter=2
Location=England  
Joined=18 years ago   MB Posts=199  
Angel
 (Inactive)
England
In that case, perform a check in your calculation procedure before calculating the totals.

if (Edit1.Text = '') or (Edit2.Text = '') or (Edit3.Text = '') or (Edit4.Text = '') or (Edit5.Text = '') then
ShowMessage('Please enter all field values')
else
begin
Your calculation procedure!
end;
 Posted 17 years ago
Comment Quote
About Angel -Collapse +Expand
Visit Profile
Inactive member.
Member does not subscribe to this thread.
Email Not Verified!
Once email is verified, we will review and approve the account.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #6302 (Level 1.2)  Reply to 6291
Reply Posted 5/21/2002 5:11:00 AM
Location=England  
Joined=18 years ago   MB Posts=199  
Creeden
 (Inactive)
Paris, Ontario,Canada
Hi Angel.
Been there done that.
Can't do it that way or I get a "Divide by Zero" error. Gotta find another way. Big Grin!
That works fine in the series side as it is
only addition.
 Posted 17 years ago (Thread Starter)
Comment Quote
About Creeden -Collapse +Expand
Visit Profile
Inactive member.
Member does not subscribe to this thread.
Email Not Verified!
Once email is verified, we will review and approve the account.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #6301 (Level 1.3)  Reply to 6291
Reply Posted 5/21/2002 4:22:00 AM
Location=Paris, Ontario,Canada  
Joined=17 years ago   MB Posts=6  
Creeden
 (Inactive)
Paris, Ontario,Canada
Hello again Angel.
That still is not what I need.
A user may only have 3 out of the 5 Edit boxes to fill. (inductor components).
I used this code for now.

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;


If you want to play with it I will email you the source.
Thanks
Bob
 Posted 17 years ago (Thread Starter)
Comment Quote
About Creeden -Collapse +Expand
Visit Profile
Inactive member.
Member does not subscribe to this thread.
Email Not Verified!
Once email is verified, we will review and approve the account.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #6305 (Level 1.4)  Reply to 6291
Reply Posted 5/21/2002 10:44:00 AM
Location=Paris, Ontario,Canada  
Joined=17 years ago   MB Posts=6  
Most Recent Post
Creeden
 (Inactive)
Paris, Ontario,Canada
Hello Scott and Raymond.
I have that Project all finished now.
Here is part of what I did.
 procedure TForm1.CalcpClick(Sender: TObject);
begin
seventh:=convInput(Edit7);
eighth:=convInput(Edit8);
ninth:=convInput(Edit9);
tenth:=convInput(Edit10);
eleventh:=convInput(Edit11);
try
Edit12.Font.Color := clBlue;
Edit12.Text:=FloattoStr(1/(invertion(seventh)+
invertion(eighth)+
invertion(ninth)+
invertion(tenth)+
invertion(eleventh)));
except
Edit12.Font.Color := clRed;
Edit12.Text := '#Error!#';
showmessage('Error!, check the values!');
end;
end;
Then the Invertion and ConvInput functions look after the Division by Zero problem.
I used this code for Numbers only.
begin
if not (Key in [#8, '0'..'9', DecimalSeparator]) then begin
ShowMessage('Invalid key: ' + Key);
Key := #0;
end
else if (Key = DecimalSeparator) and
(Pos(Key, Edit1.Text) > 0) then begin
ShowMessage('Invalid Key: twice ' + Key);
Key := #0;
end;
Thank you all for your input into this problem.
Bob.
 Posted 17 years ago (Thread Starter)
Comment Quote
About Creeden -Collapse +Expand
Visit Profile
Inactive member.
Member does not subscribe to this thread.
Email Not Verified!
Once email is verified, we will review and approve the account.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #6621 (Level 1.5)  Reply to 6291
Reply Posted 6/11/2002 5:49:00 AM
Location=Paris, Ontario,Canada  
Joined=17 years ago   MB Posts=6  
Raymond
Maui
I created a similar form to calculate water fixture values to size water meters. I assigned to zero to all edit boxes when the form opens. Whenever the cursor leaves an edit box, test if it's blank and assign zero if it is. This prevents a user from blanking out or deleting the value from an edit box. This prevented all my calculation errors.
 Posted 17 years ago
Comment Quote
About Raymond -Collapse +Expand
Visit Profile
Approved member.
Member subscribes to this thread with a verified email.
Old Account!
If this is your account, sign in to activate web presence data (sign in quarterly to keep active). Alternatively, you can subscribe to our monthly eMag with a valid email address.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #6617 (Level 1.6)  Reply to 6291
Reply Posted 6/10/2002 2:39:00 PM
Location=Maui  
Joined=17 years ago   MB Posts=4  
Scott Wehrly
Prestwood IT
 (Inactive)
Las Vegas, NV USA
It appears (based on the code in your May 21 post) that you need to have non-zero values in seventh, eighth, and ninth, since your first "if = 0" test is on ninth.



So, for this to work, after your StrToFloat conversions, you should have code that ripples the non-zero values up towards seventh, eighth, and ninth:

if (tenth = 0) then
begin
tenth := eleventh;
eleventh := 0;
end;
if (ninth = 0) then
begin
ninth := tenth;
tenth := 0;
end;
if (eighth = 0) then
begin
eighth := ninth;
ninth := 0;
end;


You don't need to go beyond that, because if eighth is zero, your parallel calculation won't be computed. That portion of code could be supplemented by adding the following to the beginning:

if (eighth = 0) then
Edit12.Text := '0.0'
else
if (ninth = 0) then
...


One other thing I noticed is that you don't seem to accomodate for invalid character input by the user. Common practice for StrToFloat and StrToInt functions when working with TEdit controls is to wrap the function call in a "try..except" block to prevent invalid character errors:

try
eighth := StrToFloat(Edit8.Text);
except
eighth := 0;
end;


Invalid character errors will be trapped by the application without throwing up a dialog box. (of course, you could handle the error when this happensd and give the user a kind warning too...)
 Posted 17 years ago
Comment Quote
About Scott Wehrly -Collapse +Expand
Visit Profile
Inactive member.
Member does not subscribe to this thread.
Old Account!
If this is your account, sign in to activate web presence data (sign in quarterly to keep active). Alternatively, you can subscribe to our monthly eMag with a valid email address.
Web Presence Hidden.
Once above is taken care of, full Profile content will display including back links, about me, my message, custom Profile html, social networking links, message board signature, company profile, etc.

Post ID #6618 (Level 1.7)  Reply to 6291
Reply Posted 6/10/2002 4:14:00 PM
Location=Las Vegas, NV USA 
Joined=17 years ago   MB Posts=442   KB Posts=19  

Revive Thread!

Add a comment to revive this old thread and make this archived thread more useful.

Write a Comment...
Full Editor
...
Sign in...

If you are a member, Sign In. Or, you can Create a Free account now.


Anonymous Post (text-only, no HTML):

Enter your name and security key.

Your Name:
Security key = P1103A1
Enter key:
Icon: A Post    Thread    Idea    Important!    Cool    Sad    No    Yes    Includes a Link...   
Thread #6291 Counter
13736
Since 4/2/2008

Follow PrestwoodBoards on: 


©1995-2019 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech