10 posts
Posted 22 June 2013 - 07:26 AM
This is my code. When entering the wrong password it is supposed to send out a wireless redstone signal that is attached to the back, and when the override password is enter it is supposed to close the program.
When entering the correct password(s) it does work.
wr = peripheral.wrap("back")
local pass
local found
local attempts
function waitPass()
term.clear()
term.setCursorPos(1,1)
print("Please enter a valid password.")
print("——————————")
status, pass = pcall(read)
end
function checkPass()
file = io.open("passwords")
for line in file:lines() do
if pass == OverrideProgram then
exit()
elseif pass == line then
found = true
else
attempts = attempts+1
end
end
end
function reset()
name = ""
found = false
attempts = 0
end
function run()
if found == true then
wrfreq(1001)
pulseWR(10)
elseif found == false and attempts == 2 then
wrfreq(1011)
for i = 1, 3 do
pulseWR(1)
end
wrfreq(1017)
for i = 1, 3 do
pulseWR(1)
end
else
waitPass()
end
end
function pulseWR(time)
rs.setOutput("back", true)
pcall(sleep, time/2)
rs.setOutput("back", false)
pcall(sleep, time/2)
end
function wrfreq(freq)
wr.setFreq(freq)
end
while true do
reset()
waitPass()
checkPass()
run()
end
1852 posts
Location
Sweden
Posted 22 June 2013 - 08:42 AM
Please post the error you are getting…
Is it when you enter an incorrect password?
And use code tags instead of quote
1114 posts
Location
UK
Posted 22 June 2013 - 09:01 AM
Try putting the function definitions before you call them.
7083 posts
Location
Tasmania (AU)
Posted 22 June 2013 - 09:02 AM
The order of the function declarations doesn't matter so much if they're global. Though they should ideally be local where possible.
Some
IO API functions aren't available. lines() is apparently one of them.
I'm guessing the program always acts as though the correct password is entered? You're a little ambiguous about what the actual problem is.
2005 posts
Posted 22 June 2013 - 09:11 AM
if pass == OverrideProgram then
OverrideProgram isn't defined anywhere else, so it should be nil, but pass should never be nil.
I don't know how your passwords file is structured, but I suspect that this code could be better structured.
10 posts
Posted 22 June 2013 - 05:04 PM
if pass == OverrideProgram then
OverrideProgram isn't defined anywhere else, so it should be nil, but pass should never be nil.
I don't know how your passwords file is structured, but I suspect that this code could be better structured.
This part checks to see if the entered string is equal to "OverrideProgram", if it is it should then fire the exit() command.
7083 posts
Location
Tasmania (AU)
Posted 22 June 2013 - 08:07 PM
That's all well and good, but since you haven't defined that variable anywhere, it'll never match.
2005 posts
Posted 22 June 2013 - 08:14 PM
if pass == OverrideProgram then
OverrideProgram isn't defined anywhere else, so it should be nil, but pass should never be nil.
I don't know how your passwords file is structured, but I suspect that this code could be better structured.
This part checks to see if the entered string is equal to "OverrideProgram", if it is it should then fire the exit() command.
If you want to see if the entered string is equal to "OverrideProgram", then it should be
if pass == "OverrideProgram" then
Also, that part should be outside of the loop, you only need to check it once.
10 posts
Posted 22 June 2013 - 11:00 PM
If anyone could write a program that I can use as a substitute with the same functionality for this I would be very great full.
7083 posts
Location
Tasmania (AU)
Posted 22 June 2013 - 11:28 PM
You'd need to show the format of your "passwords" file, or at least explain how many you want it to accept.
10 posts
Posted 27 June 2013 - 02:45 AM
You'd need to show the format of your "passwords" file, or at least explain how many you want it to accept.
THe password file is simply another program file saved as "passwords" and formatted as below:
minecraft
jghfy
pokemon
lovelife
(Example password file.)
Each password is on a new line.
10 posts
Posted 27 June 2013 - 02:46 AM
if pass == OverrideProgram then
OverrideProgram isn't defined anywhere else, so it should be nil, but pass should never be nil.
I don't know how your passwords file is structured, but I suspect that this code could be better structured.
This part checks to see if the entered string is equal to "OverrideProgram", if it is it should then fire the exit() command.
If you want to see if the entered string is equal to "OverrideProgram", then it should be
if pass == "OverrideProgram" then
Also, that part should be outside of the loop, you only need to check it once.
Thank you have made the change however program still fails to run properly
2005 posts
Posted 28 June 2013 - 05:19 AM
If you need help on finding out why, it is necessary to post the revised code.
8543 posts
Posted 28 June 2013 - 11:21 AM
The :lines() iterator on io-opened file handles is most certainly available, even if io.lines is not. His usage of `file:lines()` is correct. In my opinion, more people should use the io library, in part because of this exact feature.