Main Menu

<eof> expected near 'else'

Started by Gtb81, Jan 01, 2021, 07:11 PM

Previous topic - Next topic

Gtb81

So this is the first time i have actually tried writing my own program, I am trying to make a control program for the BigReactors mod and i want to have a control terminal, but i can't seem to get past this else statement.  error: <eof> expected near 'else'

local stop, start, temp = "stop", "start", "temp"
while true do
   term.clear()
   term.setCursorPos(1,1)
   write("Please enter a command: ")
   local input = read()
   if input == start then shell.run("start") end

   if input == stop then shell.run("stop") end

   if input == temp then shell.run("temp") end
 
end

 else
  term.clear()
  write("Unknown command.")
  sleep(2)
end

Lupus590

You end the while loop and then have an else keyword without a corresponding if.

Also, you might want to fix the indentation on your read.

Gtb81

#2
Sorry if i dont understand, i am very new to lua, how do you suggest i rewrite the else statement? i am trying to add an error message if none of the known commands are typed in.

Lupus590

Then you want to put in into the loop and use elseif statements instead of multiple if statements.

local stop, start, temp = "stop", "start", "temp"
while true do
   term.clear()
   term.setCursorPos(1,1)
   write("Please enter a command: ")
   local input = read()
   if input == start then shell.run("start") -- elseif has to start with an if
                             -- note the lack of an end for the if, the elseif does this for us
   elseif input == stop then shell.run("stop") -- note the lack of a space, we could do else if but
                     -- then we would need to add extra ends and the proper indentation would get messy

   elseif input == temp then shell.run("temp")

   else  -- you can have an else as the last part of an elseif, end is also valid to finish off a elseif chain
     term.clear()
     write("Unknown command.")
     sleep(2)
   end -- we still need a end here, but just one for the entire elseif chain
end

 

Gtb81

thank you, i knew about elseif statements though i had not known how to use them. Is there a way to add the ability to write into the command prompt a variable? ie rods (0-100) or maybe some literature where i might learn more about this?