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

[Lua][Error] problem with a rednet program

Started by Axolotl, 21 January 2013 - 10:58 AM
Axolotl #1
Posted 21 January 2013 - 11:58 AM
This program is just supposed to be a menu that runs other rednet related programs when the user selects an option. When I run it I get the error:
bios:338: [string "rednet"]:35: 'then' expected

this is the code

term.clear() --this first part is just text
term.setCursorPos(1,1)
print("Welcome to RedNet!")
print("Please select an action.")
print("")
print("1. Receive")
print("2. Send Message")
print("3. Broadcast Message")
print("4. Configure Modem")
print("5. Announce")
print("")
write("Perform action ")

local input = read() -- this part is suppose to run the corresponding programs
if input == "1" then
  shell.run(netreceive) -- these are the programs that correspond to each option
end -- is there suppose to be an "end" after each if?
if input == "2" then
  shell.run(netsend)
end
if input == "3" then
  shell.run(netbroadcast)
end
if input == "4" then
  shell.run(netset)
end
if input == "5" then
  shell.run(netannounce)
end
if input ~= "1","2","3","4","5" then -- i have no idea if i did this line correctly
  print("Please enter a valid option.")
  sleep(2)
  shell.run(rednet) -- this is just to restart the program in the event of an invalid input
end
Luanub #2
Posted 21 January 2013 - 12:07 PM
The error is in one of the other programs that it is trying to run. According to the error message the error is on line 35, your program only has 34 lines. Will need to see the other codes. Also why run separate programs? It's better to just use functions if you can.

Edit: I also just noticed your if statements could use a little improving and your last one will not work the way you want it to. it should be something like

if input ~= "1" or input ~= "2" or input ~= "3" and so on...

However there is a better way.. Since you are only checking against 1 input it is better to use only 1 if statement instead of several like you have.

Here is an improved if statement to check the input( take note of the comments other errors corrected)

local input = read()
if input == "1" then
  shell.run("netreceive") -- you need double quotes around your program names
elseif input == "2" then  --continue the if statement, it will go down the list until it find a condition that matches or runs out of options
  shell.run("netsend")
elseif input == "3" then
  shell.run("netbroadcast")
elseif input == "4" then
  shell.run("netset")
elseif input == "5" then
  shell.run("netannounce")
else --since nothing else matched please do this
  print("Please enter a valid option.")
  sleep(2)
  shell.run("rednet") -- this is the program that is generating your error message you should at least rename it to something other then rednet, since rednet is already a name of an API
end

Is the code you have listed the rednet program? If so and you are doing "shell.run("rednet") to restart the program you're nesting the program within itself which will eventually cause a stack overflow. Its very bad coding practice and can simply be avoided using a loop…


while true do
local input = read()
if input == "1" then
  shell.run("netreceive")
elseif input == "2" then
  shell.run("netsend")
elseif input == "3" then
  shell.run("netbroadcast")
elseif input == "4" then
  shell.run("netset")
elseif input == "5" then
  shell.run("netannounce")
else --since nothing else matched please do this
  print("Please enter a valid option.")
  sleep(2)
end
end
Edited on 21 January 2013 - 11:24 AM
Axolotl #3
Posted 21 January 2013 - 12:14 PM
The error is in one of the other programs that it is trying to run. According to the error message the error is on line 35, your program only has 34 lines. Will need to see the other codes. Also why run separate programs? It's better to just use functions if you can.

I know it's weird to run separate programs but when I started coding this I didn't know better. I messed with the code and found out that it actually works perfectly if I just put the program names in quotes. I appreciate the help though.
Luanub #4
Posted 21 January 2013 - 12:25 PM
I would read my last edit. It's a fix that you will want to implement if I was right on the file names. It's also a good example of why its best to avoid shell.run() when you can.
Edited on 21 January 2013 - 11:27 AM
Axolotl #5
Posted 21 January 2013 - 02:02 PM
Thanks. I used the code you suggested and I'm starting to rewrite it so it uses functions instead of separate programs.