loadstring help
#1
Posted 09 March 2013 - 08:54 AM
I am developing currently a calculator and I am stuck on the output, I cant get it to actually calculate.
It spits this error out: bios:156: bad argument: string expected, got nil
I think (actually im pretty sure) the problem is somewhere between the lines 8 - 43 and 95 - 107 > http://pastebin.com/EN8Y2ChZ
Thanks in advance as always,
Engineer
#2
Posted 09 March 2013 - 09:00 AM
#4
Posted 09 March 2013 - 09:11 AM
result = loadstring('return ' .. displayInput)
result = result()
Dan zou het moeten werken.
English:
Easy replace it with this:
<code-snippet>
Then it should work
#5
Posted 09 March 2013 - 09:17 AM
result = (loadstring("return " .. displayInput))()
#7
Posted 09 March 2013 - 09:22 AM
superaxander, on 09 March 2013 - 09:11 AM, said:
result = loadstring('return ' .. displayInput)
result = result()
Dan zou het moeten werken.
English:
Easy replace it with this:
<code-snippet>
Then it should work
tesla1889, on 09 March 2013 - 09:17 AM, said:
result = (loadstring("return " .. displayInput))()
Sadly enough, those dont work for me :/
It just attempts to call nil with both
#8
Posted 09 March 2013 - 09:24 AM
Engineer, on 09 March 2013 - 09:22 AM, said:
superaxander, on 09 March 2013 - 09:11 AM, said:
result = loadstring('return ' .. displayInput)
result = result()
Dan zou het moeten werken.
English:
Easy replace it with this:
<code-snippet>
Then it should work
tesla1889, on 09 March 2013 - 09:17 AM, said:
result = (loadstring("return " .. displayInput))()
Sadly enough, those dont work for me :/
It just attempts to call nil with both
#9
Posted 09 March 2013 - 09:31 AM
#10
Posted 09 March 2013 - 10:39 AM
if tButton[button][5] == "=" then
local answer = loadstring('return ' .. displayInput)
if answer then
result = answer()
else
result = 'invalid input'
end
displayInput = ""
resultBool = true
else
if displayInput == "" then
displayInput = displayInput .. tButton[button][5]
else
displayInput = displayInput .. " " .. tButton[button][5]
end
end
draw()
It only adds to the displayInput if the sign isn't the equal sign, and otherwise, it does calculations.The whole 'if answer then' bit up there is to make sure the user doesn't enter in something like "5 + - /", and if he does, the program should tell the user he or she is stupid and should hit him or her self with a brick.
#12
Posted 09 March 2013 - 11:12 AM
Kingdaro, on 09 March 2013 - 10:39 AM, said:
if tButton[button][5] == "=" then
local answer = loadstring('return ' .. displayInput)
if answer then
result = answer()
else
result = 'invalid input'
end
displayInput = ""
resultBool = true
else
if displayInput == "" then
displayInput = displayInput .. tButton[button][5]
else
displayInput = displayInput .. " " .. tButton[button][5]
end
end
draw()
It only adds to the displayInput if the sign isn't the equal sign, and otherwise, it does calculations.The whole 'if answer then' bit up there is to make sure the user doesn't enter in something like "5 + - /", and if he does, the program should tell the user he or she is stupid and should hit him or her self with a brick.
Thanks for correcting my code, now it doesnt attempt to call nill, bad argument etc.
But my only problem is now that it just doesnt write anything. I have uploaded the code I have use with your part included on pastebin I am starting to think there is something wrong with something else. Maybe the draw() function is totally wrong.
Hopefully we get there eventually and thanks to everyone who is trying to help me!
Engineer
#13
Posted 09 March 2013 - 01:04 PM
Engineer, on 09 March 2013 - 11:12 AM, said:
Fixed version
Okay, so the main reason for it not displaying the result is because when you defined the draw() function, resultBool is false so when ever you call that function, it's ALWAYS detected to be FALSE
Do you understand that?
Another problem was that you added a space after every digit/math operation. Now this will cause it to error because when it wants to return a value with a space between the numbers, like this - fixed
local a = '3 + 3 3'
local ans = loadstring('return ' .. a)
print( type( ans ) ) -- output will be nil
-- whereas this will work
local a = '3 + 33'
local ans = loadstring('return ' .. a)
print( ans() ) -- output will be 35
Another problem is that if you press anywhere on the screen that isn't a valid number / operation, it will error. This is because it doesn't verify that the 'button' variable is valid - fixed that for you too
#14
Posted 09 March 2013 - 01:33 PM
And to my knowledge 3 + 33 is 36
I am just joking about that, thank you
Engineer
#16
Posted 09 March 2013 - 10:55 PM
2 user(s) are reading this topic
0 members, 2 guests, 0 anonymous users











