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

Text Input in Program

Started by CRUGG, 28 April 2018 - 10:38 AM
CRUGG #1
Posted 28 April 2018 - 12:38 PM
Hello! I want to make a text-based Program, which for e.g. prints a message. Then the user can enter something, then it should store the entered Sting into a variable, so I can code something like this after it:

if msg = "install" then
  print("Install..")
  [...]
end
if msg = "uninstall" then
  print("Uninstall..")
  [...]
end
if msg = "exit" then
  print("Exit..")
  [...]
end

My questions are:
- How can I make the text input and save the entered text into a variable
- How can I make it, that something will be executed, if the entered text isn't "install", "uninstall" or "exit"
- How can I make the program exit when entering exit
SquidDev #2
Posted 28 April 2018 - 12:55 PM
How can I make the text input and save the entered text into a variable
You'll want the read function for this:

local msg = read()

How can I make it, that something will be executed, if the entered text isn't "install", "uninstall" or "exit"
If you convert all but the "end if"s into "elseif", you can then have a fallback:

if msg == "install" then --# Your code
elseif msg == "uninstall" then --# Your code
elseif msg == "exit" then  --# Your code
else 
  error("Unknown command!", 0) --# Will print an error and exit
end
Also note the use of double equals (checking equality) rather than single equals.

How can I make the program exit when entering exit
If this is on the top-level/in a loop just use return:

--# ...
elseif msg == "exit" then
  return
else
--# ...
Otherwise you could use error with some friendly message.
CRUGG #3
Posted 06 August 2018 - 09:52 AM
How can I make the text input and save the entered text into a variable
You'll want the read function for this:

local msg = read()

How can I make it, that something will be executed, if the entered text isn't "install", "uninstall" or "exit"
If you convert all but the "end if"s into "elseif", you can then have a fallback:

if msg == "install" then --# Your code
elseif msg == "uninstall" then --# Your code
elseif msg == "exit" then  --# Your code
else
  error("Unknown command!", 0) --# Will print an error and exit
end
Also note the use of double equals (checking equality) rather than single equals.

How can I make the program exit when entering exit
If this is on the top-level/in a loop just use return:

--# ...
elseif msg == "exit" then
  return
else
--# ...
Otherwise you could use error with some friendly message.

Hey! I know it's an old topic, but how can I make it so when the command is unknown, it prints something like "Unknown command, please use x, y or z!" and then it goes back to the same questions.
Basically I want the comments in the following code to be replaced with something that's working

--| POINT A
print("Please enter x, y or z")
if msg == "x" then --Code here
elseif msg == "y" then --Code here
elseif msg == "z" then  --Code here
else
  print("Unknown command, please use x, y or z")
  --> GOTO POINT A
end
Stekeblad #4
Posted 06 August 2018 - 11:04 AM
Hey! I know it's an old topic, but how can I make it so when the command is unknown, it prints something like "Unknown command, please use x, y or z!" and then it goes back to the same questions.
Basically I want the comments in the following code to be replaced with something that's working

--| POINT A
print("Please enter x, y or z")
if msg == "x" then --Code here
elseif msg == "y" then --Code here
elseif msg == "z" then  --Code here
else
  print("Unknown command, please use x, y or z")
  --> GOTO POINT A
end

Add a loop

local invalidAnswer = true
while invalidAnswer do
  print("Please enter x, y or z")
  if msg == "x" then
    invalidAnswer = false
    --Code here
  elseif msg == "y" then
    invalidAnswer = false
    --Code here
  elseif msg == "z" then
    invalidAnswer = false
    --Code here
  else
    print("Unknown command, please use x, y or z")
  end
end
Bomb Bloke #5
Posted 06 August 2018 - 12:41 PM
If you're intending to use this sort of thing at multiple points within your script, then you might consider bundling it off into a new function:

local function getInput(...)
	while true do
		local result = read()
		
		for i = 1, #arg do if result == arg[i] then return result end end
		
		print("Unknown command, please use one of the below:")

		for i = 1, #arg do print(arg[i]) end
	end
end

print("Please enter x, y or z")

local msg = getInput("x", "y", "z")

If you want to ignore case sensitivity, then you can switch to "read():lower()" on the third line.