1522 posts
Location
The Netherlands
Posted 09 March 2013 - 08:54 AM
Hello,
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/EN8Y2ChZThanks in advance as always,
Engineer
620 posts
Location
Holland
Posted 09 March 2013 - 09:00 AM
One thing I know is that loadstring returns a function not a value
1522 posts
Location
The Netherlands
Posted 09 March 2013 - 09:05 AM
One thing I know is that loadstring returns a function not a value
Hoe zou ik dat moeten oplossen?
English: How should I solve that?
620 posts
Location
Holland
Posted 09 March 2013 - 09:11 AM
Makkelijk vervang het met dit
result = loadstring('return ' .. displayInput)
result = result()
Dan zou het moeten werken.
English:
Easy replace it with this:
<code-snippet>
Then it should work
404 posts
Location
St. Petersburg
Posted 09 March 2013 - 09:17 AM
to make it simpler:
result = (loadstring("return " .. displayInput))()
620 posts
Location
Holland
Posted 09 March 2013 - 09:21 AM
to make it simpler:
result = (loadstring("return " .. displayInput))()
Thought of that but I thought This was easier to understand.
1522 posts
Location
The Netherlands
Posted 09 March 2013 - 09:22 AM
Makkelijk vervang het met dit
result = loadstring('return ' .. displayInput)
result = result()
Dan zou het moeten werken.
English:
Easy replace it with this:
<code-snippet>
Then it should work
to make it simpler:
result = (loadstring("return " .. displayInput))()
Sadly enough, those dont work for me :/
It just attempts to call nil with both
620 posts
Location
Holland
Posted 09 March 2013 - 09:24 AM
Makkelijk vervang het met dit
result = loadstring('return ' .. displayInput)
result = result()
Dan zou het moeten werken.
English:
Easy replace it with this:
<code-snippet>
Then it should work
to make it simpler:
result = (loadstring("return " .. displayInput))()
Sadly enough, those dont work for me :/
It just attempts to call nil with both
Are you sure displayInput is not nil
1522 posts
Location
The Netherlands
Posted 09 March 2013 - 09:31 AM
I am very sure that displayInput is not nil because I can write(displayInput)
1688 posts
Location
'MURICA
Posted 09 March 2013 - 10:39 AM
I think the problem is that, when you type "=", it makes a string like "return 5 + 5 =", because you're adding the equal sign to the string before doing calculations. Here's a revised snippet of lines 96 to 106:
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.
404 posts
Location
St. Petersburg
Posted 09 March 2013 - 10:45 AM
–snip–
the program should tell the user he or she is stupid and should hit him or her self with a brick.
please use that direct quote for your error handling :)/>
1522 posts
Location
The Netherlands
Posted 09 March 2013 - 11:12 AM
I think the problem is that, when you type "=", it makes a string like "return 5 + 5 =", because you're adding the equal sign to the string before doing calculations. Here's a revised snippet of lines 96 to 106:
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
2088 posts
Location
South Africa
Posted 09 March 2013 - 01:04 PM
–snip
Fixed versionOkay, 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
FALSEDo you understand that? :P/>/>/>
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 :P/>/>/>
1522 posts
Location
The Netherlands
Posted 09 March 2013 - 01:33 PM
Thank you very much! It is finally working :D/>
And to my knowledge 3 + 33 is 36 :P/>
I am just joking about that, thank you ;)/>
Engineer
2088 posts
Location
South Africa
Posted 09 March 2013 - 10:11 PM
Thank you very much! It is finally working :D/>
And to my knowledge 3 + 33 is 36 :P/>
I am just joking about that, thank you ;)/>
Engineer
Lol derp, thought i had it as 2 + 33 :D/>
1522 posts
Location
The Netherlands
Posted 09 March 2013 - 10:55 PM
This is the proper code:
http://pastebin.com/EN8Y2ChZThanks everyone for helping me!:D/>