Jump to content




Entering Input in While loop


  • You cannot reply to this topic
2 replies to this topic

#1 gargoyle575

  • Members
  • 3 posts

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, 06 March 2013 - 06:28 PM.
added code tags


#2 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

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


#3 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

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.





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users