Czarek
Czarek

Reputation: 647

Basic Lua Loop not working as it should

So basically I have this calculator where you can choose between division, multiply, plus and minus. And I just learned how to loop programs. But the loop is not working as it should.

print("Please choose the way to use the calculator")
print("[1] Plus [2] Minus [3] Division [4] Multiply")

restart = 1

x = tonumber(io.read())

while restart == 1 do

if x == 1 then
    print("Please write the first number to add up")
    n1 = tonumber(io.read())
    print("Please write the second number to add up")
    n2 = tonumber(io.read())
    print(n1 .. "+" .. n2 .. "=" .. n1+n2)
elseif x == 2 then
    print("Please write the first number to subtract")
    n1 = tonumber(io.read())
    print("Please write the second number to subtract")
    n2 = tonumber(io.read())
    print(n1 .. "-" .. n2 .. "=" .. n1-n2)
elseif x == 3 then
    restart = 0
    print("Please write the first number to divide")
    n1 = tonumber(io.read())
    print("Please write the second number to divide")
    n2 = tonumber(io.read())
    print(n1 .. "/" .. n2 .. "=" .. n1/n2)
elseif x == 4 then
    print("Please write the first number to multiply")
    n1 = tonumber(io.read())
    print("Please write the second number to multiply")
    n2 = tonumber(io.read())
    print(n1 .. "*" .. n2 .. "=" .. n1*n2)
end
end

So what happens is basically if you choose minus and then you put in lets say 10-2. It works as it should. But the thing is that only the minus portion keeps looping. It does not ask you to choose a way to multiply. How can I fix this?

E.g I would like this to do the equation for you, then loop back to the beginning.

Upvotes: 0

Views: 345

Answers (3)

catwell
catwell

Reputation: 7048

You can just replace this:

print("Please choose the way to use the calculator")
print("[1] Plus [2] Minus [3] Division [4] Multiply")

restart = 1

x = tonumber(io.read())

while restart == 1 do

by this:

restart = 1

while restart == 1 do

print("Please choose the way to use the calculator")
print("[1] Plus [2] Minus [3] Division [4] Multiply")

x = tonumber(io.read())

On a side note, since you are learning Lua, here is a way you could refactor this code (with proper use of locals, etc):

local get_operands = function(s)
  print("Please write the first number to " .. s)
  local n1 = io.read("*n")
  print("Please write the second number to " .. s)
  local n2 = io.read("*n")
  return n1, n2
end

while true do

  print("Please choose the way to use the calculator")
  print("[1] Plus [2] Minus [3] Division [4] Multiply")

  local x = io.read("*n")

  if x == 1 then
      local n1, n2 = get_operands("add up")
      print(n1 .. "+" .. n2 .. "=" .. n1+n2)
  elseif x == 2 then
      local n1, n2 = get_operands("subtract")
      print(n1 .. "-" .. n2 .. "=" .. n1-n2)
  elseif x == 3 then
      local n1, n2 = get_operands("divide")
      print(n1 .. "/" .. n2 .. "=" .. n1/n2)
      break
  elseif x == 4 then
      local n1, n2 = get_operands("multiply")
      print(n1 .. "*" .. n2 .. "=" .. n1*n2)
  end

end

Upvotes: 2

Oliver
Oliver

Reputation: 29463

You set x outside the loop so it never changes. Put your x = input inside the loop, this way it will be re-read every iteration.

Upvotes: 0

Truls Unstad
Truls Unstad

Reputation: 56

Don't you need to put the input inside your loop?

x = tonumber(io.read())

while restart == 1 do

Should rather be

while restart == 1 do

x = tonumber(io.read())

Upvotes: 1

Related Questions