Posted 01 March 2014 - 02:11 AM
I created a calculator program and can't seem to find the problem. Every time I run the program, nothing happens. Instead it just goes to the next line waiting for a command.
function add()
result = num1 + num2
print("Answer: " .. result)
end
function subtract()
result = num1 - num2
print("Answer: " .. result)
end
function multiply()
result = num1 * num2
print("Answer: " .. result)
end
function divide()
result = num1 / num2
print("Answer: " .. result)
end
function compare()
if num1 > num2 then
print(num1 .. " is greater than " .. num2)
elseif num1 < num2 then
print(num1 .. " is less than " .. num2)
else
print(num1 .. " is equal to " .. num2)
end
a = 1
while a == 1 do
print("")
print("Would you like to +, -, /, *, compare, or cancel")
print("")
op = read()
print("")
if op == "cancel" then
print("Calculator Canceled")
sleep(2)
term.setCursorPos(1, 1)
term. clear()
end
print("First Number?")
num1 = tonumber(read())
print("")
print("Second Number?")
num2 = tonumber(read())
print("")
if op == "+" then
add()
end
if op == "-" then
subtract()
end
if op == "/" then
divide()
end
if op == "*" then
multiply()
end
if op == "compare" then
compare()
end
end
end
function add()
result = num1 + num2
print("Answer: " .. result)
end
function subtract()
result = num1 - num2
print("Answer: " .. result)
end
function multiply()
result = num1 * num2
print("Answer: " .. result)
end
function divide()
result = num1 / num2
print("Answer: " .. result)
end
function compare()
if num1 > num2 then
print(num1 .. " is greater than " .. num2)
elseif num1 < num2 then
print(num1 .. " is less than " .. num2)
else
print(num1 .. " is equal to " .. num2)
end
a = 1
while a == 1 do
print("")
print("Would you like to +, -, /, *, compare, or cancel")
print("")
op = read()
print("")
if op == "cancel" then
print("Calculator Canceled")
sleep(2)
term.setCursorPos(1, 1)
term. clear()
end
print("First Number?")
num1 = tonumber(read())
print("")
print("Second Number?")
num2 = tonumber(read())
print("")
if op == "+" then
add()
end
if op == "-" then
subtract()
end
if op == "/" then
divide()
end
if op == "*" then
multiply()
end
if op == "compare" then
compare()
end
end
end