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

Problems with everything within a loop

Started by Hydrotronics, 07 November 2015 - 10:34 PM
Hydrotronics #1
Posted 07 November 2015 - 11:34 PM
Pastebin: http://pastebin.com/E2qm6s5r

So, i am making myself an OS and i have run into a problem here:

whenever I run this program, it wants an end on line 32, but when i place an end there, it says "eof expected" on line 33! and that's an elseif statement! This is really aggrovating me and i just cannot get my head around it!

Please Explain why it is doing this!

Many Thanks, Hydro
KingofGamesYami #2
Posted 07 November 2015 - 11:49 PM
I went through your code, properly indented it, and added comments where I know something is wrong. I don't know how you intend / intended to nest the various if/elseif statements, so you'll have to deal with this yourself.

Spoiler

sid = 0

function clear()
	term.clear()
	term.setCursorPos(1,1)
end

function menu(id, text)
	if sid == id then
		write"> "
	else
		write"| "
	end
	print(text)
end

while true do
	clear()
	menu(0, "Desktop")
	menu(1, "Create/Edit Program")
	menu(2, "Console")
	menu(3, "Shutdown")
	event, key = os.pullEvent("key")
	if key == 200 then
		print(sid)
		if sid > 0 then
			sid = sid - 1
		else
			sid = sid + 3
		end
	end
	--#PROBLEM HERE!!  NO IF STATEMENT STILL OPEN FOR ELSEIF TO USE
	elseif key == 208 then
		print(sid)
		if sid < 3 then
			sid = sid + 1
		else
			sid = sid - 3
		end
	end
	--#PROBLEM HERE!!  NO IF STATEMENT STILL OPEN FOR ELSEIF TO USE
	elseif key == 28 then
		if sid == 0 then
			break
		end
		shell.run(".desktop")
		--#PROBLEM HERE!!  NO IF STATEMENT STILL OPEN FOR ELSEIF TO USE
		elseif sid == 1 then
			term.clear()
			term.setCursorPos(1,1)
			print"Enter Name Of File:"
			write""
			input = read()
			shell.run("edit "..input)
		--#PROBLEM HERE!!  NO IF STATEMENT STILL OPEN FOR ELSEIF TO USE
		elseif sid == 2 then
			break
			shell.run(".Console")
		end
		--#PROBLEM HERE!!  NO IF STATEMENT STILL OPEN FOR ELSEIF TO USE
		elseif sid == 3 then
			term.clear()
			term.setCursorPos(10,9)
			print"Shutting Down..."
			sleep(1)
			os.shutdown()
		end
	end
end
Lupus590 #3
Posted 07 November 2015 - 11:52 PM
elseif act as an end for the prior if

remove the ends on lines 31,39,55

edit: :ph34r:/> 'd
Edited on 07 November 2015 - 10:53 PM
KingofGamesYami #4
Posted 07 November 2015 - 11:58 PM
elseif act as an end for the prior if

No, it doesn't. You still need the end, but you don't need an end for each if / elseif / else. You need 1 end per conditional.
Hydrotronics #5
Posted 07 November 2015 - 11:58 PM
So i have done some editing and now it's requesting an end to the while statement at line 54! then when i put the end in it does the same thing.
Pastebin

so basically it's just gone down :P/>
Edited on 07 November 2015 - 10:59 PM
KingofGamesYami #6
Posted 08 November 2015 - 12:04 AM
There's a problem at the bottom, but I'm not exactly sure what you want it to do. I've indented it properly (PLEASE indent properly, it makes finding these problems so much easier).


sid = 0

function clear()
  term.clear()
  term.setCursorPos(1,1)
end

function menu(id, text)
  if sid == id then
    write"> "
  else
    write"| "
  end
  print(text)
end

while true do
  clear()
  menu(0, "Desktop")
  menu(1, "Create/Edit Program")
  menu(2, "Console")
  menu(3, "Shutdown")
  event, key = os.pullEvent("key")
  if key == 200 then
    print(sid)
    if sid > 0 then
      sid = sid - 1
    else
      sid = sid + 3
    end
  elseif key == 208 then
    print(sid)
    if sid < 3 then
      sid = sid + 1
    else
      sid = sid - 3
    end
  elseif key == 28 then
    if sid == 0 then
      break
    end
    shell.run(".desktop")
  elseif sid == 1 then
    term.clear()
    term.setCursorPos(1,1)
    print"Enter Name Of File:"
    write""
    input = read()
    shell.run("edit "..input)
  elseif sid == 2 then
    break
  end
  shell.run(".Console")
  --#NO IF STATEMENT OPEN FOR THIS ELSEIF
  elseif sid == 3 then
    term.clear()
    term.setCursorPos(10,9)
    print"Shutting Down..."
    sleep(1)
    os.shutdown()
  end
end
Edited on 07 November 2015 - 11:05 PM
Hydrotronics #7
Posted 08 November 2015 - 12:16 AM
Ok, I removed the end blocking the elseif, and now it wants an end to end the if at line 24! and it want's it at line 52
Code

Edit: Also, i think i indented it properly this time XD
Edited on 07 November 2015 - 11:18 PM
KingofGamesYami #8
Posted 08 November 2015 - 12:20 AM
Spoiler

sid = 0

function clear()
  term.clear()
  term.setCursorPos(1,1)
end

function menu(id, text)
  if sid == id then
    write"> "
  else
    write"| "
  end
  print(text)
end

while true do
  clear()
  menu(0, "Desktop")
  menu(1, "Create/Edit Program")
  menu(2, "Console")
  menu(3, "Shutdown")
  event, key = os.pullEvent("key")
  if key == 200 then
    print(sid)
    if sid > 0 then
      sid = sid - 1
    else
      sid = sid + 3
    end
  elseif key == 208 then
    print(sid)
    if sid < 3 then
      sid = sid + 1
    else
      sid = sid - 3
    end
  elseif key == 28 then
    if sid == 0 then
      break
    end
    shell.run(".desktop")
  elseif sid == 1 then
    term.clear()
    term.setCursorPos(1,1)
    print"Enter Name Of File:"
    write""
    input = read()
    shell.run("edit "..input)
  elseif sid == 2 then
    break
    shell.run(".Console") --#you can't have things after break.  Either 
                          --#1) run .Console above the break or
                          --#2) run .Console after the loop
  elseif sid == 3 then
    term.clear()
    term.setCursorPos(10,9)
    print"Shutting Down..."
    sleep(1)
    os.shutdown()
  end
  end --#This end is not needed.  Remove it
end
Hydrotronics #9
Posted 08 November 2015 - 12:24 AM
Thank you! you were correct with the breaks, I am new to those. And also i did need the 3rd end, to clsoe the while statement all together
Lupus590 #10
Posted 08 November 2015 - 12:41 AM
elseif act as an end for the prior if

No, it doesn't. You still need the end, but you don't need an end for each if / elseif / else. You need 1 end per conditional.

that's what I meant, sorry if I wasn't clear