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

How to stop a while loop when an option is executed?

Started by CitricThunder, 08 October 2012 - 11:23 PM
CitricThunder #1
Posted 09 October 2012 - 01:23 AM
I wan't to make my menu system stop once an option is executed. So far I can make it run a different program but the menu still lingers, fully functional. I wan't this to go away and I don't know how. Here's my code.



function menu(id, text)
  if sid == id then
	write"> "
  else
	write"* "
  end
  print(text)
end

TerminalMode = 0

function os.pullEvent()
local event, p1, p2, p3, p4, p5 = os.pullEventRaw()
if event == "terminate" then
if TerminalMode == 0 then

end
end
return event, p1, p2, p3, p4, p5
end


term.clear()
term.setCursorPos(1, 2)
print(" #################################################")
print(" #                                               #")
print(" #                                               #")
print(" #                                               #")
print(" #                                               #")
print(" #                                               #")
print(" #                                               #")
print(" #                                               #")
print(" #                                               #")
print(" #                                               #")
print(" #                                               #")
print(" #                                               #")
print(" #                                               #")
print(" #                                               #")
print(" #                                               #")
print(" #                                               #")
print(" #################################################")
term.setCursorPos(15, 1)
print("State Bank of the Craft")

term.setCursorPos(45, 1)
print("ATM #1")

sid = 0

while true do

term.setCursorPos(15,8)
menu(0, "Withdraw")
term.setCursorPos(15,9)
menu(1, "Deposit")
term.setCursorPos(15,10)
menu(2, "Check Balance")
event, key = os.pullEvent("key")
if key == 200 then
if sid == 0 then
  sid = sid + 2
elseif sid == 1 then
  sid = sid - 1
elseif sid == 2 then
  sid = sid - 1
end
end
if key == 208 then
if sid == 0 then
  sid = sid + 1
elseif sid == 1 then
  sid = sid + 1
elseif sid == 2 then
  sid = sid - 2
end
end
if key == 28 then
if sid == 0 then
  shell.run("/withdraw") -- I wan't the menu to stop when this program runs.
elseif sid == 1 then
  shell.run("/deposit") -- And this one.
elseif sid == 2 then
  shell.run("/checkbal") -- And this one.
end
end
if key == 42 then
  event, key2 = os.pullEvent("key")
  term.setCursorPos(45, 17)
  print("CTRLP")
  if key2 == 46 then
	shell.run("/controlpanel")
  end
end
end
faubiguy #2
Posted 09 October 2012 - 01:43 AM
Using the the keyword break in a loop causes the loop to exit.
In your case:

while true do
-- snip
if key == 28 then
if sid == 0 then
  shell.run("/withdraw")
  break
elseif sid == 1 then
  shell.run("/deposit")
  break
elseif sid == 2 then
  shell.run("/checkbal")
  break
end
end
-- snip
end
Kingdaro #3
Posted 09 October 2012 - 01:44 AM
The "break" keyword is the best way of getting out of loops. Just change your lines like so:


if sid == 0 then
  shell.run("/withdraw") -- I wan't the menu to stop when this program runs.
  break
elseif sid == 1 then
  shell.run("/deposit") -- And this one.
  break
elseif sid == 2 then
  shell.run("/checkbal") -- And this one.
  break

Also, protip, those menu borders you're printing can be done easier like this:

-bleh, never mind, these forums have odd code formatting.

EDIT: I hate being ninja'd.
CitricThunder #4
Posted 09 October 2012 - 01:50 AM
Using the the keyword break in a loop causes the loop to exit.
In your case:

while true do
-- snip
if key == 28 then
if sid == 0 then
  shell.run("/withdraw")
  break
elseif sid == 1 then
  shell.run("/deposit")
  break
elseif sid == 2 then
  shell.run("/checkbal")
  break
end
end
-- snip
end

Ah thanks