'I have made a lua calculator to learn the language but when i run the code it skips an io.read

function CALC()
    print("First number:")
    local input1 = io.read("*n")
    print("operator: ")
    local operator = io.read("*l")
    print("secondNumber:")
    local input2 = io.read("*n")
    local switchCalc = {
        ["+"] = function ()
            local result = input1 + input2
            print(result)
        end,
        ["-"] = function ()
            local result = input1 - input2
            print(result)
        end,
        ["*"] = function ()
            local result = input1 * input2
            print(result)
        end,
        ["/"] = function ()
            local result = input1 / input2
            print(result)
        end
    }
    local a = 1
    local f = switchCalc[a]
    if(f) then
        f()
    else
        print("Default")
    end
end

CALC()

My output is:
First number: 10
operator:
secondNumber:
*
Default
Hope somebody will help me with this cause I'm starring this from a couple hours and I really don't understand

lua


Solution 1:[1]

The problem is not "the code skipping an io.read" as you make it out to be - it is simply you not using the variables correctly; the reading is just fine.

The line local f = switchCalc[a] uses a global variable a which you haven't defined (and which certainly isn't the operator). It will thus be nil. switchCalc[nil] is again nil, so f will be nil. Now the if doesn't trigger, the else is entered and you get "Default" as the output. The fix is simple: Just make this switchCalc[operator].

Two notes:

  • You don't need parentheses around if-conditions if you use spacing;
  • io.read() is the same as io.read("*l") (you can omit "*l")

Solution 2:[2]

I suggest to use io.read() without any argument.
Also it looks more like a prompt if using io.write()
But converting it into whats asking for on the fly...

-- calc.lua (Tested with Lua 5.4)
local function calc()
io.write("First number: ") local input1 = tonumber(io.read()) -- number
io.write("Operator: ") local operator = io.read() -- Let a string be a string
io.write("Second number: ") local input2 = tonumber(io.read()) -- number
print('Equals to:', load('return '.. input1 .. operator .. input2)())
end

return calc

And than...

> calc = require('calc')
> calc()
First number: 3
Operator: *
Second number: 3
Equals to: 9

Solution 3:[3]

How much I know:

print("First number:") -- prints -> First number:
local input1 = io.read("*n") -- when you write 10 , buffer got "10(enter)"
-- now buffer is "(enter)"
print("operator: ") -- prints -> operator:
local operator = io.read("*l") -- buffer not empty then operator is "(enter)"
print("secondNumber:")
local input2 = io.read("*n") -- try to read "*"

solution is add io.read("*l") before print("operator: ") or use io.read() not io.read("*n") / io.read("*l")

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 LMD
Solution 2
Solution 3 Reinisdm