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

where are my ends wrong

Started by makerimages, 02 January 2013 - 10:18 PM
makerimages #1
Posted 02 January 2013 - 11:18 PM
hi, in


--[[Copyright Makerimages, all of the code here was made by makerimages and you may not distribute this code without written premission]]--
--set screen colours
term.clear()
term.setCursorPos(1,1)
term.setBackgroundColor(colors.lightBlue)
term.clear()
term.setBackgroundColor(colors.yellow)
local width=4
local size={term.getSize()}
for w=1,width do
  term.setCursorPos(1,size[2]-w+1)
  for x=1,size[1] do
	write(' ')
  end
end
--load apis and set more colors
term.setBackgroundColor(colors.lightBlue)
term.setCursorPos(1,3)
os.loadAPI("/APIS/mOSapi")
mOSapi.Logo()
sleep(5)
term.clear()
term.setBackgroundColor(colors.lightBlue)
term.clear()
term.setBackgroundColor(colors.yellow)
local width=4
local size={term.getSize()}
for w=1,width do
  term.setCursorPos(1,size[2]-w+1)
  for x=1,size[1] do
	write(' ')
  end
end
  --check for user dir
term.clear()
term.setCursorPos(1,1)
term.setBackgroundColor(colors.lightBlue)
term.clear()
term.setBackgroundColor(colors.yellow)
local width=4
local size={term.getSize()}
for w=1,width do
  term.setCursorPos(1,size[2]-w+1)
  for x=1,size[1] do
	write(' ')
  end
  end
term.setBackgroundColor(colors.lightBlue)
if not fs.exists("/mOS/Users") then
	fs.makeDir("/mOS/Users")
	term.setCursorPos(1,1)
	print("Hang on! It seems that you do not have a user account set up yet! Lets do that now!")
	print("Please select a Username: ")
  
	repeat
		term.setCursorPos(1,3)
		term.clearLine()
		write(" > ")
		s_newName = read()
	until s_newName ~= "" and not string.find(s_newName, " ")
  
	print("Please, choose a password to use in order to protect your account:")
	repeat
		term.setCursorPos(1,6)
		term.clearLine()
		write(" > ")
		s_newPassword = read("*")
	until s_newPassword ~= "" and not string.find(s_newPassword, " ")
	fileHandle = fs.open("/mOS/Users/"..s_newName,"w")
	fileHandle.write(s_newPassword)
	fileHandle.close() -- Always close a file after you're done with it.
os.reboot();

else
term.setBackgroundColor(colors.lightBlue)
term.clear()
term.setBackgroundColor(colors.yellow)
local width=4
local size={term.getSize()}
for w=1,width do
term.setCursorPos(1,size[2]-w+1)
for x=1,size[1] do
	write(' ')
end
end
end
term.setBackgroundColor(colors.lightBlue)
term.setCursorPos(21,10)
print("Username:")
term.setCursorPos(21,11)
term.setBackgroundColor(colors.gray)
local loginname= read("")
if fs.exists("/mOS/Users" ..loginname) then
  term.setBackgroundColor(colors.lightBlue)
  term.setCursorPos(21,12)
  print("Password:")
  term.setCursorPos(21,13)
  term.setBackgroundColor(colors.gray)
  local loginpassword= read("*")
  loginfile= fs.open("/mos/Users/" ..loginname,"a")
  local filestuff={}
  local line = loginfile.readLine() -- This function reads the next line in the file, until the end.
  repeat -- Start a loop which will read lines into a table until no lines are left.
  table.insert(filestuff,line) -- Puts the value of the current line into the table we have.
  line = loginfile.readLine() -- read the next line
  until line == nil -- readLine() returns nil when the end of the file is reached.
  loginfile.close() -- Close up the file ready for use again.
  if loginpassword==filestuff[1] then
  term.setBackgroundColor(colors.lightBlue)
  term.setCursorPos(21,14)
  print("User found! Logging in")
  sleep(2)
  shell.run("/mos/OS")
end
end
else
  term.setBackgroundColor(colors.lightBlue)
  term.setCursorPos(21,14)
  print("User not found, rebooting system!")
  sleep(2)
  os.reboot()
end

it asks for eof on line 119 (the last else) if I add a end there and press enter to get the else to next line it asks for one onn 118-What ends do I have wrong?? how to fix to make it run OK?
Doyle3694 #2
Posted 02 January 2013 - 11:21 PM
eof, it means you have 1 to MANY end
makerimages #3
Posted 03 January 2013 - 12:11 AM
ok, htat worked, but now- If i insert a legit username in :

term.setBackgroundColor(colors.lightBlue)
term.setCursorPos(21,10)
print("Username:")
term.setBackgroundColor(colors.gray)
term.setCursorPos(21,11)
local loginname= read()
if fs.exists("/mOS/Users"..loginname) then
  term.setBackgroundColor(colors.lightBlue)
  term.setCursorPos(21,12)
  print("Password:")
  term.setCursorPos(21,13)
  term.setBackgroundColor(colors.gray)
  local loginpassword= read("*")
  loginfile= fs.open("/mos/Users/"..loginname,"a")
  local filestuff={}
  local line = loginfile.readLine() -- This function reads the next line in the file, until the end.
  repeat -- Start a loop which will read lines into a table until no lines are left.
  table.insert(filestuff,line) -- Puts the value of the current line into the table we have.
  line = loginfile.readLine() -- read the next line
  until line == nil -- readLine() returns nil when the end of the file is reached.
  loginfile.close() -- Close up the file ready for use again.
  if loginpassword==filestuff[1] then
  term.setBackgroundColor(colors.lightBlue)
  term.setCursorPos(21,14)
  print("User found! Logging in")
  sleep(2)
  shell.run("/mos/OS")
end

else if not fs.exists("/mOS/Users"..loginname) then
  term.setBackgroundColor(colors.lightBlue)
  term.setCursorPos(21,14)
  print("User not found, rebooting system!")
  sleep(2)
  os.reboot()
end
end

then it still says username not found and reboots how to fix?
Doyle3694 #4
Posted 03 January 2013 - 12:28 AM
it's 'elseif', not 'else if'
remiX #5
Posted 03 January 2013 - 01:11 AM
Also, you're checking if it exists, so it can only exist or not exist, so use if and else, no elseif is needed
And open the file in read mode, not append


loginfile = fs.open("/mos/Users/"..loginname,"r") -- You were missing an '/' after Users, also use "r" to read, not "a" -- "a" is for append, to write
--[[ This is another way of handling files, and is
    the most common way to use files ]]
if loginfile then -- If it exists.
    passwordFromFile = loginfile.readLine() -- May as well just read the password in the file now
    loginfile.close()
    term.setBackgroundColor(colors.lightBlue)
    term.setCursorPos(21,12)
    print("Password:")
    term.setCursorPos(21,13)
    term.setBackgroundColor(colors.gray)
    passwordFromUser = read("*")
    if passwordFromUser == passwordFromFile then
        term.setBackgroundColor(colors.lightBlue)
        term.setCursorPos(21,14)
        print("User found! Logging in")
        sleep(2)
        shell.run("/mos/OS")
    end
else
    term.setBackgroundColor(colors.lightBlue)
    term.setCursorPos(21,14)
    print("User not found, rebooting system!")
    sleep(2)
    os.reboot()
end
makerimages #6
Posted 03 January 2013 - 01:55 AM
NOW the program stops after I enter the username

full current code:


--[[Copyright Makerimages, all of the code here was made by makerimages and you may not distribute this code without written premission]]--
--set screen colours
term.setTextColor(colors.white)
term.clear()
term.setCursorPos(1,1)
term.setBackgroundColor(colors.lightBlue)
term.clear()
term.setBackgroundColor(colors.yellow)
local width=4
local size={term.getSize()}
for w=1,width do
  term.setCursorPos(1,size[2]-w+1)
  for x=1,size[1] do
    write(' ')
  end
end
--load apis and set more colors
term.setBackgroundColor(colors.lightBlue)
term.setCursorPos(1,3)
os.loadAPI("/APIS/mOSapi")
mOSapi.Logo()
sleep(5)
term.clear()
term.setBackgroundColor(colors.lightBlue)
term.clear()
term.setBackgroundColor(colors.yellow)
local width=4
local size={term.getSize()}
for w=1,width do
  term.setCursorPos(1,size[2]-w+1)
  for x=1,size[1] do
    write(' ')
  end
end
  --check for user dir
term.clear()
term.setCursorPos(1,1)
term.setBackgroundColor(colors.lightBlue)
term.clear()
term.setBackgroundColor(colors.yellow)
local width=4
local size={term.getSize()}
for w=1,width do
  term.setCursorPos(1,size[2]-w+1)
  for x=1,size[1] do
    write(' ')
  end
  end
term.setBackgroundColor(colors.lightBlue)
if not fs.exists("/mOS/Users") then
    fs.makeDir("/mOS/Users")
    term.setCursorPos(1,1)
    print("Hang on! It seems that you do not have a user account set up yet! Lets do that now!")
    print("Please select a Username: ")
   
    repeat
	    term.setCursorPos(1,3)
	    term.clearLine()
	    write(" > ")
	    s_newName = read()
    until s_newName ~= "" and not string.find(s_newName, " ")
   
    print("Please, choose a password to use in order to protect your account:")
    repeat
	    term.setCursorPos(1,6)
	    term.clearLine()
	    write(" > ")
	    s_newPassword = read("*")
    until s_newPassword ~= "" and not string.find(s_newPassword, " ")
    fileHandle = fs.open("/mOS/Users/"..s_newName,"w")
    fileHandle.write(s_newPassword)
    fileHandle.close() -- Always close a file after you're done with it.
os.reboot();

else
term.setBackgroundColor(colors.lightBlue)
term.clear()
term.setBackgroundColor(colors.yellow)
local width=4
local size={term.getSize()}
for w=1,width do
term.setCursorPos(1,size[2]-w+1)
for x=1,size[1] do
    write(' ')
end
end
end
term.setBackgroundColor(colors.lightBlue)
term.setCursorPos(21,10)
print("Username:")
term.setBackgroundColor(colors.gray)
term.setCursorPos(21,11)
local loginname= read()
if fs.exists("/mOS/Users"..loginname) then
  loginfile = fs.open("/mos/Users/"..loginname,"r") -- You were missing an '/' after Users, also use "r" to read, not "a" -- "a" is for append, to write
--[[ This is another way of handling files, and is the most common way to use files ]]
if loginfile then -- If it exists.
    passwordFromFile = loginfile.readLine() -- May as well just read the password in the file now
    loginfile.close()
    term.setBackgroundColor(colors.lightBlue)
    term.setCursorPos(21,12)
    print("Password:")
    term.setCursorPos(21,13)
    term.setBackgroundColor(colors.gray)
    passwordFromUser = read("*")
    if passwordFromUser == passwordFromFile then
	    term.setBackgroundColor(colors.lightBlue)
	    term.setCursorPos(21,14)
	    print("User found! Logging in")
	    sleep(2)
	    shell.run("/mos/OS")
    end
else
    term.setBackgroundColor(colors.lightBlue)
    term.setCursorPos(21,14)
    print("User not found, rebooting system!")
    sleep(2)
    os.reboot()
end
end
remiX #7
Posted 03 January 2013 - 06:20 AM
Change

if fs.exists("/mOS/Users"..loginname) then
to
if fs.exists("/mOS/Users/"..loginname) then