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

[question] terminate a program?

Started by NanoBob, 05 May 2013 - 06:24 AM
NanoBob #1
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
theoriginalbit #2
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)
NanoBob #3
Posted 05 May 2013 - 08:27 AM
Thanks for the quick respond!
theoriginalbit #4
Posted 05 May 2013 - 08:27 AM
No problems, read the edit I just made.
NanoBob #5
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
SadKingBilly #6
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().
NanoBob #7
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
wahok #8
Posted 05 May 2013 - 12:28 PM
thanks that was really helpfull