[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Passing a string equation to become an integer

Posted on 2009-02-16
9
Medium Priority
?
402 Views
Last Modified: 2012-05-06
ok I have two text boxes. The user enters a equation to be calculated in one of the boxes and the other, after clicking a button, provides the answer. The problem occurs when passing the string to become an integer at the StrToInt(sEquation). Error is "Not a valid integer"

for example this equation (975 - 369) x (943 x 384 + 100)



procedure TForm1.Button1Click(Sender: TObject);
var
  iEquation: Integer;
  sEquation: String;

begin

  sEquation := edtEquation.Text;
  {changes character x to * to satisfy the compilers need to recognize the symbol used to multiply with}
  while Pos('x', sEquation) > 0 do
    sEquation[Pos('x', sEquation)] := '*';
  edtEquation.Text :=  sEquation;
  {error happens on the following line}
  iEquation := StrToInt(sEquation);
  edtAnswer.Text := IntToStr(iEquation);
  edtAnswer.SetFocus;

end;
0
Comment
Question by:CrazyOne
  • 4
  • 4
9 Comments
 
LVL 13

Expert Comment

by:rfwoolf
ID: 23654863
I guess intead of using an integer (which won't allow decimals) use a double or float. (strtofloat etc)
0
 
LVL 44

Author Comment

by:CrazyOne
ID: 23654876
I did it doesnt matter what type i use the error still occurs
0
 
LVL 14

Expert Comment

by:SteveBay
ID: 23654891
Changing : '(975 - 369) x (943 x 384 + 100)'
to: '(975 - 369) * (943 * 384 + 100)'
does not make it an expression that can be recognized as an integer.
To find the solution to this equation you will need parse for all numbers and operators separately and build the expression in code.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 14

Expert Comment

by:SteveBay
ID: 23654929
for example
StrToInt('100') = 100
StrToInt('10 * 10') = Error! not a valid integer.
StrToInt('10') * StrToInt('10') = 100

0
 
LVL 44

Author Comment

by:CrazyOne
ID: 23654944
Yeah I can understand that but when i do something like the following it works so why the difference

   iEquation := (975 - 369) * (943 * 384 + 100);

  {and then}

  edtAnswer.Text := IntToStr(iEquation);
  edtAnswer.SetFocus;
0
 
LVL 44

Author Comment

by:CrazyOne
ID: 23654955
Oh ok I see what u are driving at. What a pain :>)
0
 
LVL 14

Expert Comment

by:SteveBay
ID: 23654984
There may be a component somewhere out htere that can do what you are trying to do. I've never looked for one.
0
 
LVL 14

Accepted Solution

by:
SteveBay earned 2000 total points
ID: 23654999
0
 
LVL 44

Author Closing Comment

by:CrazyOne
ID: 31547569
That link gives access to code that did the job for me thanks
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month18 days, 18 hours left to enroll

834 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question