22 posts
Location
java.exception:3753:location not found
Posted 11 November 2016 - 08:34 PM
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.
121 posts
Posted 11 November 2016 - 09:55 PM
In the while loop you are adding args to args[i+1] try adding just the args to res:
res = res + args.
22 posts
Location
java.exception:3753:location not found
Posted 12 November 2016 - 09:16 PM
In the while loop you are adding args to args[i+1] try adding just the args to res:
res = res + args.
Will try that thanks :)/>
119 posts
Posted 12 November 2016 - 10:22 PM
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
).
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.
In the while loop you are adding args to args[i+1] try adding just the args to res:
res = res + args.
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.
22 posts
Location
java.exception:3753:location not found
Posted 12 November 2016 - 10:45 PM
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
).
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.
In the while loop you are adding args to args[i+1] try adding just the args to res:
res = res + args.
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.
if you put {…} wouldn't that read all the input after the program name when you run it? I'm trying to make a calc where it get multiple input inside the program. anyway thanks
463 posts
Location
Star Wars
Posted 13 November 2016 - 12:27 AM
I would use lua for calculate a line, because i am lazy ^^:
local res, err = load('return ' .. read(), 'Calculator', 't', {})
if not res then printError(err) end
local ok, res = pcall(res)
if not ok then printError(res) end
Otherwise i would use gmatch.
Also, i woudl give a second arhument for error: 2. So if the calculator errors, the user knows in which line he has done a mistake.
Edited on 12 November 2016 - 11:30 PM
119 posts
Posted 13 November 2016 - 04:01 AM
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
).
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.
In the while loop you are adding args to args[i+1] try adding just the args to res:
res = res + args.
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.
if you put {…} wouldn't that read all the input after the program name when you run it? I'm trying to make a calc where it get multiple input inside the program. anyway thanks
Sorry, I didn't realize that you were trying to do that. xP That is correct then, however it should have tonumber() still.
797 posts
Posted 13 November 2016 - 08:58 AM
Using tonumber isn't entirely necessary in this case. Lua treats valid number strings as numbers with arithmetic operators:
print( "5" + "2" )
print( type( "5" + "2" ) )
That will print 7 and number:
Spoiler
It's probably better practise to convert it explicitly, but if you were wondering why it wasn't breaking, that's why.
96 posts
Posted 13 November 2016 - 01:56 PM
You could probably use loops and I'm probably doind it wrong can some one help me it goes like this:
-snip-
You could try using loadstring, here is a working example calculator
print("Welcome to Calculator")
write("Input: ")
local input = read()
local env = {
["math"] = math,
}
local func = loadstring("return "..input)
setfenv(func,env)
write("Output: ")
print(func())