Passing a string equation to become an integer

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;
LVL 44
CrazyOneAsked:
Who is Participating?
 
rfwoolfCommented:
I guess intead of using an integer (which won't allow decimals) use a double or float. (strtofloat etc)
0
 
CrazyOneAuthor Commented:
I did it doesnt matter what type i use the error still occurs
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
SteveBayCommented:
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
 
SteveBayCommented:
for example
StrToInt('100') = 100
StrToInt('10 * 10') = Error! not a valid integer.
StrToInt('10') * StrToInt('10') = 100

0
 
CrazyOneAuthor Commented:
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
 
CrazyOneAuthor Commented:
Oh ok I see what u are driving at. What a pain :>)
0
 
SteveBayCommented:
There may be a component somewhere out htere that can do what you are trying to do. I've never looked for one.
0
 
CrazyOneAuthor Commented:
That link gives access to code that did the job for me thanks
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.