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

Another Nooby Question

Started by Sir_Mr_Bman, 11 October 2013 - 05:47 PM
Sir_Mr_Bman #1
Posted 11 October 2013 - 07:47 PM
So I have this code here:
http://pastebin.com/ep7wkvMU
And I don't know why it is giving me errors.
Please help?
Symmetryc #2
Posted 11 October 2013 - 07:51 PM
Just looking at it, I think it might be because you're calling h.readAll twice. I'm not entirely sure though.
jay5476 #3
Posted 11 October 2013 - 09:35 PM
your code
Spoiler

-- Clear the screen.
term.clear()
term.setCursorPos(1, 1)
-- Open the password containing file.
fs.open("/word", "r")
local y = h.readAll()
-- Print a prompt asking for password...
print("Please enter the password: ")
local pass = h.readAll()
-- Display the users password as stars.
y = read("*")
if y == pass then
-- VALID PASSWORD
print("Password is VALID!")
redstone.setOutput("right", true)
sleep(5)
redstone.setOutput("right", false)
sleep(1)
os.shutdown()
else
-- PASSWORD INVALID
print("Password is INVALID!")
sleep(10)
os.shutdown()
end
theres a couple of things you do wrong
1. do this

local h = fs.open("/word","r")
so that it saves the data fs.open() returns
2. why do you have

y = h.readAll()
when it gets overriden here

y = read("*")
3. close your file handle at the end like so

h.close()
so an overall better code would be
Spoiler

-- Clear the screen.
term.clear()
term.setCursorPos(1, 1)
-- Open the password containing file.
local h = fs.open("/word", "r")

-- Print a prompt asking for password...
print("Please enter the password: ")
local pass = h.readAll()
h.close()
-- Display the users password as stars.
y = read("*")
if y == pass then
-- VALID PASSWORD
print("Password is VALID!")
redstone.setOutput("right", true)
sleep(5)
redstone.setOutput("right", false)
sleep(1)
os.shutdown()
else
-- PASSWORD INVALID
print("Password is INVALID!")
sleep(10)
os.shutdown()
end

TheOddByte #4
Posted 12 October 2013 - 07:18 AM
A tip, If you are getting an error and want people to help you, you should always post what error you are getting, and which line the error is on.
It makes it so much easier to help you, But my guess is that you haven't event used h when opening the file

	fs.open("/word", "r")

Should be

	h = fs.open("/word", "r")

And close the file as mentioned above

	h.close()

Since then you can call h.readAll() etc.
And I have no clue why you are doing this

local y = h.readAll() -- Why are you doing this?

-- Some code..

y = read("*") -- When you are overriding 'y' here?
Sir_Mr_Bman #5
Posted 15 October 2013 - 06:17 PM
Wow, I feel slightly dumb, I just ignored parts of the wikipage on the file reader. Sorry!