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

Repeat password validation

Started by IntoMadness, 12 August 2015 - 05:04 PM
IntoMadness #1
Posted 12 August 2015 - 07:04 PM
Hello dear computercrafters, I'm new in this magical programming world and experiencing some problems with a simple program.
I have set up a password system which works fine but I want to add a system that you get exactly 3 chances to enter the password until the pc reboots.
My code is as follows.

term.clear()
term.setCursorPos(1, 1)
print("security system online")
print("enter password please")
local password = 1234
local input = read("-")
if input == password then
print("access granted")
else
print("access denied")
repeat untill input == password
end

Everything goes right with the correct password but the program closes itself when I enter the wrong password!
KingofGamesYami #2
Posted 12 August 2015 - 07:57 PM
this line:

repeat until input == password

Is going to check if the input equals the password, forever. It won't as for more input, it'll just keep checking the values.

CC normally would error your program with 'too long without yielding', but since you don't make any calls to CC functions within that it will shut down the computer instead.

Perhaps you should move just the repeat to the top of your program…
Bomb Bloke #3
Posted 13 August 2015 - 01:34 AM
That is to say, more like this sort of structure:

term.clear()
term.setCursorPos(1, 1)
print("security system online")
print("enter password please")

local password = 1234

repeat
	local input = read("-")
	if input == password then
		print("access granted")
	else
		print("access denied")
	end
until input == password

You may want to review this guide.
Edited on 12 August 2015 - 11:34 PM
IntoMadness #4
Posted 13 August 2015 - 12:38 PM
Great, worked like a charm! Now I was just wondering how to make the pc reboot itself after 3 wrong password codes?
flaghacker #5
Posted 13 August 2015 - 05:50 PM
Great, worked like a charm! Now I was just wondering how to make the pc reboot itself after 3 wrong password codes?

Don't. Rebooting a computer as standard behavior in a program is a horrible idea. Use some actual delaying instead:

term.clear()
term.setCursorPos(1, 1)
print("security system online")
print("enter password please")

local password = "1234"

//this will keep track of how many attempts have been done
local tryCount = 0;

repeat
	local input = read("-")
	if input == password then
		print("access granted")
	else
		print("access denied")
		//a failed attemp  so we count it
		tryCount = tryCount + 1
		//if this is the third one
		if tryCount == 3 then
			//inform the user there's going to be a delay
			print("no attempts left. try again in 5 seconds...")
			//the delay itself
			sleep(5)
			//reset the attempt counter
			tryCount = 0
		//if it's not the third failed attempt
		else
			//tell the user how many attempts they have left
			print(3 - tryCount .. " attempts left")
		end
	end
until input == password

The added parts are heavely commented, I hope you understand them.

If you ever really need to reboot the computer, use os.reboot.

Edit: edited code to use awesome's comment, see post below
Edited on 13 August 2015 - 04:16 PM
Exerro #6
Posted 13 August 2015 - 06:01 PM
Don't. Rebooting a computer as standard behavior in a program is a horrible idea.

Yeah, I agree with you here. Imagine someone has an operating system that doesn't sandbox os.reboot, and the user has a really important program open in another window. This would reboot, and they would lose their progress in the other window.

The code very nearly works, but you should take differing types into account. Remember "123" ~= 123:


...
local password = 1234 -- this is a number
...
local input = read("-") -- this is a string
if input == password then -- this will always fail
...

If you need proof:
Spoiler

To solve this, use tonumber() to convert the input into a number:


local input = tonumber( read("-") )

Or store the password as a string:


local password = "1234"
flaghacker #7
Posted 13 August 2015 - 06:14 PM
you should take differing types into account. Remember "123" ~= 123:

I just compied Bomb's code, I didn't even notice that. Thanks for pointing that out, I'm going to edit the code in my previous post.
IntoMadness #8
Posted 13 August 2015 - 07:21 PM
Thanks again. In the Mean time I've written a secind program but I keep getting the expected image, x, y error at line 61 and I don't understand why?

–loading screens GIF
_ld = paintutils.loadImage("/os/.load/ld")
_ldI = paintutils.loadImage("/os/.loadI/ldI")
_ldII = paintutils.loadImage("/os/.loadII/ldII")
_ldIII = paintutils.loadImage("/os/.loadIII/ldIII")
_ldIV = paintutils.loadImage("/os/.loadIV/ldIV")
_ldV = paintutils.loadImage("/os/.loadV/ldV")
_ldVI = paintutils.loadImage("/os/loadVI/ldVI")
_ldVII = paintutils.loadImage("/os/.loadIII/ldVII")
_ldVIII = paintutils.loadImage("/os/.loadIV/ldVIII")
_ldIX = paintutils.loadImage("/os/.loadV/ldIX")
_ldX = paintutils.loadImage("/os/loadVI/ldX")

–Program
drawLoadScreen1 = function()
term.setBackgroundColor(colors.blue)
term.clear()
term.setCursorPos(1,1)
paintutils.drawImage(_ld, 1, 1)
os.sleep(1)
end
drawLoadScreen2 = function()
term.setBackgroundColor(colors.blue)
term.clear()
term.setCursorPos(1,1)
paintutils.drawImage(_ldI, 1, 1)
os.sleep(1)
end
drawLoadScreen3 = function()
term.setBackgroundColor(colors.blue)
term.clear()
term.setCursorPos(1,1)
paintutils.drawImage(_ldII, 1, 1)
os.sleep(3)
end
drawLoadScreen4 = function()
term.setBackgroundColor(colors.blue)
term.clear()
term.setCursorPos(1,1)
paintutils.drawImage(_ldIII, 1, 1)
os.sleep(3)
end
drawLoadScreen5 = function()
term.setBackgroundColor(colors.blue)
term.clear()
term.setCursorPos(1,1)
paintutils.drawImage(_ldIV, 1, 1)
os.sleep(3)
end
drawLoadScreen6 = function()
term.setBackgroundColor(colors.blue)
term.clear()
term.setCursorPos(1,1)
paintutils.drawImage(_ldV, 1, 1)
os.sleep(3)
end
drawLoadScreen7 = function()
term.setBackgroundColor(colors.blue)
term.clear()
term.setCursorPos(1,1)
paintutils.drawImage(_ldVI, 1, 1)
os.sleep(3)
end
drawLoadScreen8 = function()
term.setBackgroundColor(colors.blue)
term.clear()
term.setCursorPos(1,1)
paintutils.drawImage(_ldVII, 1, 1)
os.sleep(3)
end
drawLoadScreen9 = function()
term.setBackgroundColor(colors.blue)
term.clear()
term.setCursorPos(1,1)
paintutils.drawImage(_ldVIII, 1, 1)
os.sleep(3)
end
drawLoadScreen10 = function()
term.setBackgroundColor(colors.blue)
term.clear()
term.setCursorPos(1,1)
paintutils.drawImage(_ldIX, 1, 1)
os.sleep(3)
end
drawLoadScreen11 = function()
term.setBackgroundColor(colors.blue)
term.clear()
term.setCursorPos(1,1)
paintutils.drawImage(_ldX, 1, 1)
os.sleep(3)
end
drawLoadScreen12 = function()
term.setBackgroundColor(colors.blue)
term.clear()
term.setCursorPos(1,1)
paintutils.drawImage(_ldXI, 1, 1)
os.sleep(3)
end
os.sleep(5)

–Init
init = function()
drawLoadScreen1()
drawLoadScreen2()
drawLoadScreen3()
drawLoadScreen4()
drawLoadScreen5()
drawLoadScreen6()
drawLoadScreen7()
drawLoadScreen8()
drawLoadScreen9()
drawLoadScreen10()
drawLoadScreen11()
drawLoadScreen12()
end
–Main Commands

init()
HPWebcamAble #9
Posted 13 August 2015 - 08:27 PM
I've written a secind program but I keep getting the expected image, x, y error at line 61 and I don't understand why?

This is line 61:

paintutils.drawImage(_ldVI, 1, 1)
You've given it 2 numbers, so it must be the image that's having issues.

You define '_ldVI' on line 8:

_ldVI = paintutils.loadImage("/os/loadVI/ldVI")
It looks correct at first glance. So the path must be incorrect.
Did you miss a period (.) before 'loadVI'? Many of the other ones have a period there (Though the one on line 12 is also missing its period)