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

Entering Input in While loop

Started by gargoyle575, 06 March 2013 - 12:16 PM
gargoyle575 #1
Posted 06 March 2013 - 01:16 PM
How do you input data using read() when your in a while loop.


term.clear()
term.setCursorPos(1,1)
print("Enter Command:")
local password = read()
while true do
   if password == "Engage Terminal" then
	  rs.setBundledOutput("bottom", colors.combine(rs.getBundledOutput("bottom"), colors.white))
	  read()
   elseif password == "Engage Code Room" then
	  rs.setBundledOutput("bottom", colors.combine(rs.getBundledOutput("bottom"), colors.orange))
	  read()
   elseif password == "Disengage All" then
	  rs.setBundledOutput("bottom", colors.subtract(rs.getBundledOutput("bottom"), colors.white, colors.orange))
	  read()
   elseif password == "Shutdown" then
	  break
   end
end

This is what I tried using but only the first line I enter after starting the program does something. For instance I can type in "Engage Code Room" and only the set of code under it will fire, anything else entered after that does nothing.
Edited by
Lyqyd #2
Posted 06 March 2013 - 06:27 PM
Split into new topic.

You were probably trying to do something like this:


term.clear()
term.setCursorPos(1,1)
while true do
    print("Enter Command:")
    local password = read()
    if password == "Engage Terminal" then
        rs.setBundledOutput("bottom", colors.combine(rs.getBundledOutput("bottom"), colors.white))
    elseif password == "Engage Code Room" then
        rs.setBundledOutput("bottom", colors.combine(rs.getBundledOutput("bottom"), colors.orange))
    elseif password == "Disengage All" then
        rs.setBundledOutput("bottom", colors.subtract(rs.getBundledOutput("bottom"), colors.white, colors.orange))
    elseif password == "Shutdown" then
        break
    end
end
immibis #3
Posted 06 March 2013 - 09:37 PM
Notice how in Lyqyd's version, the call to read() is inside the loop, so it runs every time the loop runs.
In your version, it's outside the loop, so it runs once before your program enters the loop.