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

Need Help With Code

Started by iPortCrazy, 21 August 2013 - 03:07 AM
iPortCrazy #1
Posted 21 August 2013 - 05:07 AM
I was trying to create a program where you select an option using a number e.g [1] do something
[2] do something else
but when i created the program it won't detect what number you type so it skips the tests for input

term.clear()
term.setCursorPos(1,1)
turtle.refuel()
local a = 0
local b = 0
print ("[1] Build a house")
print ("[2] Build a bridge")
print ("[3] Unlock door")
print ("[4] Type name")
local input = read()
if input == 1 then
term.clear()
term.setCursorPos(1,1)
print ("Width:")
local width = read()
term.clear()
term.setCursorPos(1,1)
print ("Length:")
local length = read()
term.clear()
term.setCursorPos(1,1)
print ("height:")
local height = read()
term.clear()
term.setCursorPos(1,1)
turtle.forward()
turtle.forward()
turtle.forward()
turtle.forward()
turtle.turnLeft()
turtle.up()
turtle.forward()
turtle.select(2)
repeat
  repeat
   turtle.placeDown()
   turtle.forward()
   length = length - 1
   a = a + 1
  until length == 0
  turtle.turnRight()
  turtle.forward()
  repeat
   turtle.placeDown()
   turtle.forward()
   width = width - 1
   b = b + 1
  until width == 0
  turtle.turnRight()
  turtle.forward()
  repeat
   turtle.placeDown()
   turtle.forward()
   a = a - 1
  until a == 0
  turtle.turnRight()
  turtle.forward()
  repeat
   turtle.placeDown()
   turtle.forward()
   b = b - 1
  until b == 0
  height = height - 1
until height == 0
end
print ("End of program")

i added the print ("End of program") so i knew when the program finished
my problem is if you type 1 for example the "if input == 1 then" test doesn't pick up the 1 and just skips to the end
Lyqyd #2
Posted 21 August 2013 - 10:54 AM
Split into new topic.

The read function returns a string, never a number. Use `if input == "1" then` instead to compare the string.
Zudo #3
Posted 21 August 2013 - 11:02 AM
or use tonumber(read())
Last1Here #4
Posted 21 August 2013 - 02:20 PM
i would do something like this


function getInput()
  input = read()
  if input == "1" then
    buildHouse()
  elseif input == "2" then
    buildBridge()
  elseif input == "3" then
    openDoor()  
  elseif input == "4" then
    print(os.getComputerLabel())
  else 
      print("Please enter number 1-4:")
    getInput()
  end
end

function buildHouse()
  print("build house code here")
end  

function buildBridge()
  print("build bridge code here")
end  

function openDoor()
  print("open door code here")
end

shell.run('clear')
turtle.refuel()
print("[1] build house")
print("[2] build bridge")
print("[3] open door")
print("[4] print name")
print("Enter your choice: [1-4]")
getInput()
iPortCrazy #5
Posted 21 August 2013 - 05:29 PM
Thanks that helped
H4X0RZ #6
Posted 21 August 2013 - 07:25 PM
i would do something like this


function getInput()
  input = read()
  if input == "1" then
    buildHouse()
  elseif input == "2" then
    buildBridge()
  elseif input == "3" then
    openDoor()  
  elseif input == "4" then
    print(os.getComputerLabel())
  else 
      print("Please enter number 1-4:")
    getInput()
  end
end

function buildHouse()
  print("build house code here")
end  

function buildBridge()
  print("build bridge code here")
end  

function openDoor()
  print("open door code here")
end

shell.run('clear')
turtle.refuel()
print("[1] build house")
print("[2] build bridge")
print("[3] open door")
print("[4] print name")
print("Enter your choice: [1-4]")
getInput()
What you are doing in the getInput() function is recursivevfunction calling. If you enter 256(I think) times the wrong number/text it will crash. At this example it's not a problem because I don't think you will enter 256(I think)ntimes the wrong number/text :P/>
Last1Here #7
Posted 21 August 2013 - 08:39 PM
i would do something like this


function getInput()
  input = read()
  if input == "1" then
    buildHouse()
  elseif input == "2" then
    buildBridge()
  elseif input == "3" then
    openDoor()  
  elseif input == "4" then
    print(os.getComputerLabel())
  else 
      print("Please enter number 1-4:")
    getInput()
  end
end

function buildHouse()
  print("build house code here")
end  

function buildBridge()
  print("build bridge code here")
end  

function openDoor()
  print("open door code here")
end

shell.run('clear')
turtle.refuel()
print("[1] build house")
print("[2] build bridge")
print("[3] open door")
print("[4] print name")
print("Enter your choice: [1-4]")
getInput()
What you are doing in the getInput() function is recursivevfunction calling. If you enter 256(I think) times the wrong number/text it will crash. At this example it's not a problem because I don't think you will enter 256(I think)ntimes the wrong number/text :P/>/>

I know I went on the assumption that people read :P/>
Zudo #8
Posted 22 August 2013 - 01:24 AM
Or use os.pullEvent, which I prefer.