# Auto math question generator

So i made a math question generator, but the problem is, it doesnt get the right answers and i think this is because its not doing the order of operations. Could someone help me, because i really dont nkow how to get it to do them, to get it to work? Thanks
[lua]
for I=1,10 do
Ran = math.random(3,8)
Num = math.random(1,50)
Str = tostring(Num)
for Iz=1,Ran do
R = math.random(1,4)
R2 = math.random(1,50)
if(Operators[R] == â€ś+â€ť) then
Num = Num + R2
Str = Strâ€¦"+"â€¦R2
end
if(Operators[R] == â€ś-â€ť) then
Num = Num - R2
Str = Strâ€¦"-"â€¦R2
end
if(Operators[R] == â€ś") then
Num = Num * R2
Str = Strâ€¦"
â€ťâ€¦R2
end
if(Operators[R] == â€ś/â€ť) then
Num = Num / R2
Str = Strâ€¦"/"â€¦R2
end
end
end
[/lua]

You will need to give us all of your code.

Why? Thats the only relavent part to my question.

Because we donâ€™t know what your code is doing. Because itâ€™s a mess.

i think you are trying to make it alot more complicated than it has to be. I would do something like below except not so crude.
[lua]
local random_num1 = math.random(1,50)
local random_num2 = math.random(1,50)
local random_op = math.random(1,4)

if random_op == 1 then
Msg("The question is: " â€¦ random_num1 â€¦ â€ś+â€ť â€¦ random_num2)
elseif random_op == 2 then
Msg("The question is: " â€¦ random_num1 â€¦ â€ś-â€ť â€¦ random_num2)
elseif random_op == 3 then
Msg("The question is: " â€¦ random_num1 â€¦ â€ś*â€ť â€¦ random_num2)
else
Msg("The question is: " â€¦ random_num1 â€¦ â€ś/â€ť â€¦ random_num2)
end
end

Msg(â€śThat is Correct!â€ť)
else
Msg("Sorry that is not Correct. :â€™( The Correct answer is " â€¦ answer)
end
end
[/lua]

Oh, i see what the problem is. I want it to make a question, such as 10+5/10*2-45/10 and solve it using order of operations. at the moment that would look like: ((((10+5)/10)*2)-45)/10 but i want it to do with the order of ops first. Sorry for not clarifying that

That is a non-trivial problem. Itâ€™s all about language parsing and associativity. This is the kind of problem they might throw at Computer Science students as coursework and would be worth many marks.

Hmmâ€¦ Darn it.

If you knew what you wanted to group, you could put parentheses the groups and append "local a = " to the beginning, then RunString() it.

If you are going to be using RunString, I advise strongly that you do not, under any circumstances, allow string input of any kind into the runstring. There is nothing worse than leaving a gigantic security hole in your system.

A math parser I made a while back.

Usage:
[lua]
local question = â€ś10+5/10*2-45/10â€ť
print(questionâ€¦" = "â€¦math.calc(question))
[/lua]

Enjoy!

You canâ€™t use locals in RunString :eng101:

You can, but you canâ€™t access it out of RunString