This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Yacomus's profile picture

Calculator program help?

Started by Yacomus, 31 March 2018 - 01:43 PM
Yacomus #1
Posted 31 March 2018 - 03:43 PM
So I've made a simple calculator for my terminal, get to it by typing calc and it asks enough information for simple addition, subtraction, multiplication, and division. However, if I want to do more equations I have to type calc after each one is done to do another, and I don't want to do that. I'd like to be able to go into the program so it asks what I want to do, and when it's done with that equation it asks for the next right away unless I tell it I want to exit the program, but I have no clue how to do that. The code I have already is below, any tips or advice?


local sprint = textutils.slowPrint

term.write("What process?: ")
eq = read()

term.write("Enter first number: ")
num1 = tonumber(read())

term.write("Enter second number: ")
num2 = tonumber(read())

print(Calculating)
sprint("...", 3)

if eq == "+" then
  result = num1+num2
print(result)
end

if eq == "-" then
  result = num1-num2
print(result)
end

if eq == "/" then
  result = num1/num2
print(result)
end

if eq == "*" then
  result = num1*num2
print(result)
end
KingofGamesYami #2
Posted 31 March 2018 - 09:20 PM
What you need is a while loop.

https://www.lua.org/pil/4.3.2.html