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 :slight_smile:
[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
Addquest(tostring(Str),tostring(math.Round(Num)),Ran*20)
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)
local answer = 0

function ask_question()
if random_op == 1 then
Msg("The question is: " … random_num1 … “+” … random_num2)
answer = random_num1 + random_num2
elseif random_op == 2 then
Msg("The question is: " … random_num1 … “-” … random_num2)
answer = random_num1 - random_num2
elseif random_op == 3 then
Msg("The question is: " … random_num1 … “*” … random_num2)
answer = random_num1 * random_num2
else
Msg("The question is: " … random_num1 … “/” … random_num2)
answer = random_num1 / random_num2
end
end
concommand.Add(“ask_question”, ask_question)

function answer_question(ply,command,args)
if args[1] == answer then
Msg(“That is Correct!”)
else
Msg("Sorry that is not Correct. :’( The Correct answer is " … answer)
end
end
concommand.Add(“answer_question”, answer_question)
[/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