WolfDood, on 11 November 2016 - 08:34 PM, said:
You could probably use loops and I'm probably doind it wrong can some one help me it goes like this:
args = {}
write("Calc > ")
ie = read()
for n in ie:gmatch("%w+") do
table.insert(args, n)
end
noa = #args
if args[1] == "add" then
i = 2
while i < noa do
res = args[i] + args[i + 1]
i = i + 1
end
conres = res + args[noa]
print(res)
elseif args[1] == "subtract" then
-- Work on it
elseif args[1] == "divide" then
-- Work on it
elseif args[1] == "multiply" then
-- Work on it
else
error("Incorrect Syntax: usage: <function> <no..>")
end
Thank you.
It's likely seeing the argument as a string instead of a number, in that case you would need to use tonumber(args[i]).
Additionally, you should change
args = {}
to
args = {...}
The first will output an empty table, the second will actually get the arguments passed on to the program.
The_Cat, on 11 November 2016 - 09:55 PM, said:
In the while loop you are adding args[i] to args[i+1] try adding just the args[i] to res:
res = res + args[i].
Don't forget to define "res" to 0. If res is not definied, it'll result to nil and nil + any number doesn't work.