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

Password Program Failing to execute failure and override functions,

Started by ComputerWizz1508, 22 June 2013 - 05:26 AM
ComputerWizz1508 #1
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
TheOddByte #2
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
Zudo #3
Posted 22 June 2013 - 09:01 AM
Try putting the function definitions before you call them.
Bomb Bloke #4
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.
ChunLing #5
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.
ComputerWizz1508 #6
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.
Bomb Bloke #7
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.
ChunLing #8
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.
ComputerWizz1508 #9
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.
Bomb Bloke #10
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.
ComputerWizz1508 #11
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.
ComputerWizz1508 #12
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
ChunLing #13
Posted 28 June 2013 - 05:19 AM
If you need help on finding out why, it is necessary to post the revised code.
Lyqyd #14
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.