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

OS error with if statement

Started by DannySMc, 31 December 2013 - 08:12 PM
DannySMc #1
Posted 31 December 2013 - 09:12 PM
Okay so I am making a mini operating system that will assist me in the minecraft game as well as on my server, and I am getting the same error:

bios:339: [string "uos.lua"]:406: 'end' expected (to close 'if' at line 404)
I can't seem to find the problem :S
Here is the function that the error occurs:

function homeScreen() -- Finished
redMsgCount = 0
uapi.cs()
local feed = uapi.loadConfig("whatsnew")
-- Draw Instructions
uapi.drawBox(1, 51, 19, 1, " ", txtColour, bkgColour)
uapi.printC("Press the number of the option", 19, false, txtColour, bkgColour)
-- Draw time at bottom of screen
uapi.drawBox(1, 51, 18, 1, " ", txtColour, bkgColour)
uapi.printC("Rednet Messages Received: "..redMsgCount, 18, false, txtColour, bkgColour)
-- Create top banner
uapi.drawBox(1, 51, 1, 1, " ", txtColour, bkgColour)
uapi.printC("uOS -> Welcome "..username, 1, false, txtColour, bkgColour)
-- Draw options list
paintutils.drawLine(1,2,1,17,colours[bkgColour])
paintutils.drawLine(51,2,51,17,colours[bkgcolour])
uapi.printA("1. Main menu", 3, 3, false, txtColour, bkgColour)
uapi.printA("2. CraftOS Boot", 3, 5, false, txtColour, bkgColour)
-- Draw whastnew feed
uapi.printA("What's New Feed:", 3, 7, false, txtColour, bkgColour)
uapi.printA("Unavailable", 3, 9, false, txtColour, bkgColour)
uapi.printA("Unavailable", 3, 10, false, txtColour, bkgColour)
uapi.printA("Unavailable", 3, 11, false, txtColour, bkgColour)
uapi.printA("Unavailable", 3, 12, false, txtcolour, bkgColour)
uapi.printA("Unavailable", 3, 13, false, txtColour, bkgColour)

uapi.printA("Any bugs you may find, please report them!", 3, 15, false, txtColour, bkgColour)

if not modemSide == nil then -- Opens rednet
  rednet.open(modemSide)
end

while true do -- Clock, Rednet, Char Events
local event, arg1, arg2, arg3, arg4, arg5 = os.pullEventRaw()
  if event == "timer" and arg1 == timer then -- Timer Input
   drawTime()
  elseif event == "rednet" then -- Rednet messages
   intSenderID = tostring(arg1)
   strSerMessage = arg2
   intDisTravelled = tostring(arg3)
   strMessage = textutils.unserialize(strSerMessage)
  
   if fs.exists("msgs/"..intSenderID) == true then -- deletes message from same sender if exists
    fs.delete("msgs/"..intSenderID)
   end
  
   file = fs.open("msgs/"..intSenderID, "w")
   file.writeLine("Sender ID: "..intSenderID)
   file.writeLine("Distance: "..intDisTravelled.." blocks away")
   file.write(strMessage)
   file.close()
  
   rednet.send(arg1, "Auto:R")
  
   redMsgCount = tonumber(redMsgCount)
   redMsgCount = redMsgCount + 1
   redMsgCount = tostring(redMsgCount)
  
   uapi.printC("Rednet Messages Received: "..redMsgCount, 18, false, txtColour, bkgColour)
  elseif event == "char" then -- Actual User Input
   if arg1 == "1" then
    break
    mainMenu()
   elseif arg1 == "2" then
    break
    termboot()
   end
  end
end
end

the error is at the bottom line 404 is where it says -> if arg1 == "1" then <-
and line 406 is the mainMenu() function which is empty but I shall post the code:


function mainMenu()
-- Settings (TextColour, BackgroundColour, username, password)
-- uShare (Allows file sharing)
-- uChat (Chat program)
-- uRetrieve (Has updates for uOS and programs)
-- uAPI (Installed and loaded on boot)
-- uNotepad (Text editor for uOS)
-- Terminal Boot (Boots shellOS)
end

I could not figure it out at all, hope you can help!
Thanks
Lyqyd #2
Posted 31 December 2013 - 09:25 PM
break must be the last statement in a block. You can't have any code after it in the block it appears in. Why do you have break before the mainMenu and termboot calls?
DannySMc #3
Posted 31 December 2013 - 09:29 PM
break must be the last statement in a block. You can't have any code after it in the block it appears in. Why do you have break before the mainMenu and termboot calls?

I thought it would break the loop process cause I don't want it to keep running while another function runs (this has happened once before)
Bomb Bloke #4
Posted 31 December 2013 - 10:20 PM
It WILL break the loop, but since the loop is then broken, it won't even make it to the next function call.

Simply put, if you don't want an active homeScreen() function call while running mainMenu()/termboot(), then homeScreen() cannot call those functions. Note that homeScreen() won't be "doing" anything while the other functions run - it'll just be sitting there in the background, waiting for them to finish.
Lyqyd #5
Posted 31 December 2013 - 11:07 PM
It won't even break the loop. Lua really doesn't like it when break isn't the last statement in a block. That's where the end expected error is coming from–it wants there to be an end immediately after the break.
DannySMc #6
Posted 01 January 2014 - 08:03 AM
It WILL break the loop, but since the loop is then broken, it won't even make it to the next function call.

Simply put, if you don't want an active homeScreen() function call while running mainMenu()/termboot(), then homeScreen() cannot call those functions. Note that homeScreen() won't be "doing" anything while the other functions run - it'll just be sitting there in the background, waiting for them to finish.

Oh I see thanks! Yeah I took it out and it works

It won't even break the loop. Lua really doesn't like it when break isn't the last statement in a block. That's where the end expected error is coming from–it wants there to be an end immediately after the break.

Ahh I see