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

Elseif Issues

Started by cryptonat, 16 May 2015 - 01:34 AM
cryptonat #1
Posted 16 May 2015 - 03:34 AM
Sorry for the most basic of questions, but maybe one of you can give me a direction.

The code below is a basic password lock system. After receiving the correct password, the terminal gives the option of which door to open by input from the user. The problem is, the program accepts the first input in the function "input", but it doesn't accept the second or third input from the elseif statement. The code works perfectly otherwise. What am I missing?

Thank you!




oldPull = os.pullEvent
os.pullEvent = os.pullEventRaw

function access()
	    term.clear()
	    term.setCursorPos(1,1)
	    print("Access Reactor or Security?")
	    write("Access: ")
    input()
    os.reboot()
end
	
function input()		 
	    if read() == "reactor" then
	    redstone.setOutput("right", true)
	    sleep(4)
	    redstone.setOutput("right", false)
	  sleep(1)
  
    elseif read() == "security" then
	    redstone.setOutput("left", true)
	    sleep(4)
	    redstone.setOutput("left", false)
	  sleep(1)
  
    else
	    print("Incorrect Input")
			    print("Try Again")
    end
end

function open()
	    redstone.setOutput("left", true)
	    sleep(3)
	    redstone.setOutput("left", false)
end

while true do
  term.clear()
  term.setCursorPos(1,1)
  print("RESTRICTED ACCESS")
  write("Password: ")
  if read("*") == "lemon" then
    print("Access Granted.")
    --  open()
		   access()
		   os.reboot()
    else
	 print("Access Denied")
	 sleep(2)
	 os.reboot()
   end
end
flaghacker #2
Posted 16 May 2015 - 08:28 AM
You are calling read() multiple times, which will result in the computer trying to read multiple times.

Store read's return in a variable and use that in your if statements.


local value = read ()

if value == "foo" then
  --code
elseif value == "bar" then
  --code
else
  --code
end
cryptonat #3
Posted 18 May 2015 - 01:57 AM
That fixed my issue. Thank you.