Jump to content




Help on Calculator Program


  • You cannot reply to this topic
1 reply to this topic

#1 moo3oo3oo3

  • New Members
  • 1 posts

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

#2 CometWolf

  • Members
  • 1,283 posts

Posted 01 March 2014 - 09:17 AM

This is why you should use indentation... The main code is part of the compare function because your ends are missplaced. Also, use code tags please!





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users