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

[Lua] Need help, string <eof> expected?

Started by popdog15, 31 May 2012 - 09:23 PM
popdog15 #1
Posted 31 May 2012 - 11:23 PM
Ok, I'm getting an error for string <eof> expected.
Here is the code.


x = io.read()

    -- Simple prints :3
print "Press enter after you write in the name"
write "Name : "
print  (" Is this the name "..x.." you want?")
	 -- Variables.
   y = yes
   n = no
   i = read()

   -- If they have the name they want.
if i == y then
print "Good!"
end
	 -- If they dont. Says I'm getting the error on line 16, by the way.
elseif i == n then
   print "Ok, then lets try again."
   os.sleep(3)
   term.clear()
   os.sleep(1)
   shell.run( "random" )
  end

I would appreciate some help :)/>/>
MysticT #2
Posted 31 May 2012 - 11:33 PM
You have an extra end. You don't have to close the if if you are going to use else or elseif, you just need an end to close all of them:

if <condition> then
  -- do something
elseif <another condition> then
  -- do another thing
else
-- do something else
end -- just one end

Edit: also, you should put the read() after the print and the write, so it shows the text and then waits for input. And put quotes around the yes and no.
cant_delete_account #3
Posted 01 June 2012 - 12:44 AM
Here's fixed code:

name = read()
print("Press enter after you write in the name")
write("Name: ")
print("Is this the name '"..name.."' the name you want? (yes/no)")
answer = read()
if answer:lower() == "yes" then
print ("Good!")
else
print ("Ok, then lets try again.")
sleep(3)
term.clear()
sleep(1)
shell.run("random")
end
MysticT #4
Posted 01 June 2012 - 01:14 AM
Some changes:

while true do
  print("Press enter after you write in the name")
  write("Name: ")
  local name = read()
  print("Is this the name '"..name.."' the name you want? (yes/no)")
  answer = string.lower(read())
  if answer == "yes" then
    print("Good!")
    break
  else
    print ("Ok, then lets try again.")
    sleep(1)
    term.clear()
    term.setCursorPos(1, 1)
  end
end
Don't use shell.run to run the same program, it will cause an error after some time and crash the computer, it's better to use a loop.