Posted 27 September 2012 - 09:45 AM
So yesterday I found out about string.match() and search patterns and to try and help familiarize myself with them I decided to make a little calculator:
So it runs but when I try to do some math I get an error "calc:34: bad argument : string expected, got nil"
Now I would assume that this means that in my sting.match() function I used a search argument in there, and I would think that this was the (%p?), in case you're not sure that is to find the +,-,*,/ to determine what the user wants to do.
Any help would be appreciated muchly :P/>/>
Spoiler
function add(a,:D/>/>
total = a + b
print(a.." + "..b.." = "..total)
end
function subtract(a,:)/>/>
total = a - b
print(a.." - "..b.." = "..total)
end
function multiply(a,B)/>/>
total = a * b
print(a.." * "..b.." = "..total)
end
function divide(a,B)/>/>
total = a / b
print(a.." / "..b.." = "..total)
end
local total
local run = 0
while run ~= 1 do
term.clear()
term.setCursorPos(1,1)
term.write("Please enter calculation: ")
input = read()
if input == "exit" then
run = 1
break
end
termOne, math, termTwo = string.match(userInput,"(%d+)%s*(%p?)%s*(%d+)")
termOne = tonumber(termOne)
termTwo = tonumber(termTwo)
if math == "+" then
add(termOne, termTwo)
elseif math == "-" then
subtract(termOne, termTwo)
elseif math == "*" then
multiply(termOne, termTwo)
elseif math == "/" then
divide(termOne, termTwo)
else
print("MATH ERROR.")
end
end
So it runs but when I try to do some math I get an error "calc:34: bad argument : string expected, got nil"
Now I would assume that this means that in my sting.match() function I used a search argument in there, and I would think that this was the (%p?), in case you're not sure that is to find the +,-,*,/ to determine what the user wants to do.
Any help would be appreciated muchly :P/>/>