We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Passing a string equation to become an integer

CrazyOne
CrazyOne asked
on
Medium Priority
411 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;
Comment
Watch Question

Commented:
I guess intead of using an integer (which won't allow decimals) use a double or float. (strtofloat etc)

Author

Commented:
I did it doesnt matter what type i use the error still occurs
CERTIFIED EXPERT

Commented:
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.
CERTIFIED EXPERT

Commented:
for example
StrToInt('100') = 100
StrToInt('10 * 10') = Error! not a valid integer.
StrToInt('10') * StrToInt('10') = 100

Author

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;

Author

Commented:
Oh ok I see what u are driving at. What a pain :>)
CERTIFIED EXPERT

Commented:
There may be a component somewhere out htere that can do what you are trying to do. I've never looked for one.
CERTIFIED EXPERT
Commented:

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
That link gives access to code that did the job for me thanks
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.