105 posts
Posted 05 May 2013 - 08:24 AM
hey guys,
I made a program, and i want to when i put a specif input the program will stop
example
x=read()
if x==("stahp dude") then
[some code to terminate a program]
end
i know i could just make a random error apear but that wouldn't look nice.
so i need a piece of code that will terminate a program.
thanks in advance
7508 posts
Location
Australia
Posted 05 May 2013 - 08:26 AM
if you don't supply an error message to the error function it will not display anything so you could do something like this
local input = read()
if input == 'close damn you' then
term.clear()
term.setCursorPos(1,1)
error()
end
using error will exit a program from any level. however other methods are using break to exit an infinite loop, or return to exit a function, or even return to exit the program (when the return is used outside a function)
105 posts
Posted 05 May 2013 - 08:27 AM
Thanks for the quick respond!
7508 posts
Location
Australia
Posted 05 May 2013 - 08:27 AM
No problems, read the edit I just made.
105 posts
Posted 05 May 2013 - 08:29 AM
hmmm, next problem: I want the program to print a message, and then stop the program….
and I want the message to remain visible
EDIT: figured it out, i just removed the
term.clear()
and it works! thanks a lot
169 posts
Posted 05 May 2013 - 08:30 AM
You could have the program close gracefully (by having the program run out of executable code), use error(), or use return.
I, personally, prefer using the first method but it can be difficult to pull off. So I tend to fall back on error().
105 posts
Posted 05 May 2013 - 08:35 AM
The problem is i have a 200 line program, and I can't make it run out of code bacause the read() is somewhere in the beginning
7 posts
Posted 05 May 2013 - 12:28 PM
thanks that was really helpfull