0
votes

I am trying to learn how to program Computercraft Mining Turtles and I want to write a program that prompts the user for the side length dimensions for a cube which is then built. I use io.read() to get the user to input the side lengths and if he wants the cube to be solid or hollow via Y/N inputs. However when I check the answer to that last question I get a error that I have a table and need a string. Error code: Cube:17: bad argument: string expected, got table

Here's my code

--"Cube constructor"
--"Powered by UglySoft"

function get_dimensions()
  --"prompts for cube dimensions and Solid"

  print("Please enter the Cube dimensions.\n")

  io.write("Cube side length ")
  side_length = tonumber(io.read())

  io.write("Solid Cube? (Y/N) ")
  solid = io.read()


  solid = string:lower()

  return side_length, solid
end

function build_solid(side_length)
    print("Building solid Cube with side length ", side_length)
end

function build_hollow(side_length)
    print("Building hollow Cube with side length ", side_length)
end


function main()
    --"main part of the program"
    term.clear()
    print("Welcome to Cube Builder")
    print("powered by UglySoft \n")


    get_dimensions()

    if solid == "y" then
        build_solid()
    else
        build_hollow()
    end

end


main()

I am very new to lua and I am not even sure if I should use io.read() in this situation. Any help is greatly appreciated and feel free to ask questions.

2

2 Answers

1
votes

Your problem is string:lower() which de-sugars to string.lower(string) which tries to operate on the string table. That's not going to work.

You want solid:lower() or string.lower(solid).

0
votes

In ComputerCraft you should generally use a built-in function

read()

Instead ofio.read since io.read is made for reading files instead of user input.

Instead of io.write you can also use write or print.