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

on
Medium Priority
411 Views
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);

end;
Comment
Watch Question

## View Solution Only

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

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

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}

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.

Commented:
##### Thanks for using Experts Exchange.

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