Jump to content




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


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

#1 CitricThunder

  • Members
  • 33 posts

Posted 09 October 2012 - 12: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


#2 faubiguy

  • Members
  • 213 posts

Posted 09 October 2012 - 12: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


#3 Kingdaro

    The Doctor

  • Members
  • 1,636 posts
  • Location'MURICA

Posted 09 October 2012 - 12: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.

#4 CitricThunder

  • Members
  • 33 posts

Posted 09 October 2012 - 12:50 AM

View Postfaubiguy, on 09 October 2012 - 12:43 AM, said:

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





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users