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

if/elseif problems

Started by nlioc4, 27 August 2012 - 05:27 AM
nlioc4 #1
Posted 27 August 2012 - 07:27 AM
I'm having some problems with my re written refinery program. Once I get through the initial password whenever I press any key, it automatically displaces the "else" piece of code, and returns to the main menu, as designed, but it even does this when I do press the right key. Also, my second loop-breaking password doesn't work. Please help, this problem has me stumped :D/>/> Thanks :P/>/>
local clients = 31
local pass = "4808"
local breakpass = "Xieylu"
local modemside = "top"
local cableside = "left"
local oil = {}
oil[1] = "On "
local fuel = {}
fuel[1] = "On "
local refinery = {}
refinery[1] = "On "
local function screenWipe()
    term.clear()
    term.setCursorPos(1, 1)
end
	 --Thirty Spaces!  9
while true do
    screenWipe()
    term.setCursorPos(1, 1)
    print("********************************")
    print("*	 Referiny  Controller	 *")
    print("*	    1.Oil Pumps: "..oil[1].."	  *")
    print("*	   2.Fuel Pumps: "..fuel[1].."	  *")
    print("*  3.Refinery & Pump: "..refinery[1].."	  *")
    print("*							  *")
    print("*   Please Enter Password:	 *")
    print("*							  *")
    print("*							  *")
    print("*		  By  Nlioc4		  *")
    print("********************************")
    term.setCursorPos(5, 8)
    input = read("*")
    if input == pass then
	    term.setCursorPos(1, 7)
	    term.clearLine()
	    write("*Press number to change status *")
	    term.setCursorPos(1, 8)
	    term.clearLine()
	    write("*Press Backspace to exit	   *")
	    event, param1, param2 = os.pullEvent()
	    if event == "char" and param1 == "1" then
		    if oil[1] == "On " then
			    table.remove(oil)
			    table.insert(oil,"Off")
			    term.setCursorPos(1, 9)
			    term.clearLine()
			    write("*   Shutting Down Oil Pumps   *")
			    sleep(1)
			    term.setcursorPos(1, 9)
			    term.clearLine()
			    print("*							  *")
		    elseif oil[1] == "Off" then
			    table.remove(oil)
			    table.insert(oil,"On ")
			    term.setCursorPos(1, 9)
			    term.clearLine()
			    write("*    Enabling Oil Pumps	   *")
			    sleep(1)
			    term.setCursorPos(1, 9)
			    term.clearLine()
			    print("*							  *")
		    end
	    elseif event == "char" and param1 == "2" then
		    if fuel[1] == "On " then
			    table.remove(fuel)
			    table.insert(fuel,"Off")
			    term.setCursorPos(1, 9)
			    term.clearLine()
			    write("*   Shutting Down Fuel Pumps  *")
			    sleep(1)
			    term.setCursorPos()
			    term.clearLine()
		    elseif fuel[1] == "Off" then
			    table.remove(fuel)
			    table.insert(fuel,"On ")
			    term.setCursorPos(1, 9)
			    term.clearLine()
			    write("*    Enabling Fuel Pumps	  *")
			    sleep(1)
			    term.setCursorPos(1, 9)
			    term.clearLine()
			    print("*							  *")
		    end
	    elseif event == "char" and param1 == "3" then
		    if refinery[1] == "On " then
			    table.remove(refinery)
			    table.insert(refinery,"Off")
			    term.setCursorPos(1, 9)
			    term.clearLine()
			    write("*   Shutting Down Refinery    *")
			    sleep(1)
			    term.setCursorPos(1, 9)
			    term.clearLine()
			    print("*							  *")
		    elseif refinery[1] == "Off" then
			    table.remove(refinery)
			    table.insert(refinery,"On ")
			    term.setCursorPos(1, 9)
			    term.clearLine()
			    write("*    Enabling Refinery	    *")
			    sleep(1)
			    term.setCursorPos(1, 9)
			    term.clearLine()
			    print("*							  *")
		    end
	    elseif event == "key" and param1 == "14" then
	    return
	    else
		    term.setCursorPos(1, 9)
		    term.clearLine()
		    write("*    Unrecognized Keypress	 *")
		    sleep(3)
		    term.setCursorPos(1, 9)
		    term.clearLine()
	    end
    elseif pass == breakpass then
    break
    else
    term.setCursorPos(1, 9)
		    term.clearLine()
		    write("*    Unrecognized Password	 *")
		    sleep(3)
		    term.setCursorPos(1, 9)
		    term.clearLine()
    end
end
	   
   
   
    
ardera #2
Posted 27 August 2012 - 08:49 AM
One thing I could find (problem that you didn't find :D/>/>) is that you wrote

elseif event == "key" and param1 == "14" then
instead of

elseif event == "key" and param1 == 14 then


Second thing I could find (problem with breakpass) was that you wrote

elseif pass == breakpass then
instead of

elseif input == breakpass then

Third thing I could find (problem with keypress) is
os.pullEvent (or os.pullEventRaw or coroutine.yield)
"fires" every second an key event, with the param 1=0.
I don't know why but you could use

elseif param1~=0 then
instead of

else
nlioc4 #3
Posted 27 August 2012 - 09:07 PM
Thanks so much :P/>/> I changed everything you said, then also changed the "Char" arguments to key ones, and changed the "1", "2", "3" to their key values without the " " and it worked :P/>/> now just to figure out a way to changed a bundled cable output based on a table :D/>/>