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

Program doesn't reboot Computer when password is entered wrong

Started by UselessCookie, 30 April 2014 - 06:53 PM
UselessCookie #1
Posted 30 April 2014 - 08:53 PM
Hey guys,

I'm pretty new to ComputerCraft and this Forum, and I have started programming with Lua a few weeks before…
I have made a program where you have to put in your username and your password to get into the terminal.
Everything works fine until I get to the part where I have to enter the password <_</>

I have saved the code as the startup so its sort of a password log-in screen when you start the computer.

When I enter it correctly, it says "Hello" and I can acces the terminal, but when I enter it wrong it grants me access to the terminal too. It should actually say "Invalid Password" and then reboot the pc, but it ends the program instead :wacko:/>

Here is the code:

print("-----------------------")
print("CCM Computers Inustries")
print("-----------------------")
textutils.slowPrint("Welcome!")
sleep(1)
shell.run("clear")
pass1 = "Test"
pass2 = "Test2" -- This password is not used yet too
user1 = "ZetShock"
user2 = "Mineserver3000" -- This user isn't implemented yet.I want to get the part with user1 done first
print("-----------------------------------")
print("Please enter password and username: ")
print("-----------------------------------")
user = io.read()
if user == user1 then
password = read("*")
  if password == pass1 then
   textutils.slowPrint("Hello")
   sleep(1)
   end
   shell.run("clear")
  else
   print("Invalid Password")
   shell.run("reboot")
   end

Im pretty sure when the password the user has entered doesn't match with the original one, it should reboot…But it doesnt >.<
Again it just ends the program without outputting any errors.

Anyways,sorry if there is a really big mistake in this code but as I already said, I am new to Lua :P/>
I am trying to run this on my server so It would be awesome if you guys could help me with this!

Greetings
UselessCookie
CometWolf #2
Posted 30 April 2014 - 09:48 PM
This program would error while compiling, because you're missing an end. Im going to assume you didn't post your entire program. Also, inputting the wrong username would just end the program, since there is no else condition for "if user == user1 then"
Lyqyd #3
Posted 30 April 2014 - 10:01 PM
You missed the end after the sleep(1). His indentation is confusing the situation.
CometWolf #4
Posted 30 April 2014 - 10:19 PM
Ah indeed i did, my mistake. Cleaned up the indentation.

if user == user1 then
  password = read("*")
  if password == pass1 then
	textutils.slowPrint("Hello")
	sleep(1)
  end
  shell.run("clear") --at this point, the program will just end, regardless of the result of the password check
else
  print("Invalid Password")
  shell.run("reboot")
end
Well then, my original point was almost correct, it is however the password check that's missing an if statement, not the user.
Edited on 30 April 2014 - 08:19 PM
Bomb Bloke #5
Posted 01 May 2014 - 02:10 AM
That is to say, it may be a lot easier to get the username and password, then check them together:

Spoiler
while true do   -- Start a loop that repeats indefinitely.
	print("Gimme a username:")
	local user = read()

	print("Gimme a password:")
	password = read("*")

	if (user == user1 and password == pass1) or (user == user2 and password == pass2) then
		textutils.slowPrint("Hello")
		sleep(1)
		shell.run("clear")
		break  -- Escape from the loop.
	else
		print("Invalid username or password")
	end
end

I also recommend looking into tables, which make it far easier to manage a list of users (or a list of pretty much anything, for that matter):

Spoiler
users = {["ZetShock"] = "Test", ["Mineserver3000"] = "Test2", ["Bomb Bloke"] = "imah4x0ringu"}

while true do
	print("Gimme a username:")
	local user = read()

	print("Gimme a password:")
	password = read("*")

	if users[user] == password then
		textutils.slowPrint("Hello")
		sleep(1)
		shell.run("clear")
		break
	else
		print("Invalid username or password")
	end
end
UselessCookie #6
Posted 01 May 2014 - 01:39 PM
So first of all, thanks for your fast replies,this has really helped me out!

I have been confused about when i have to place end's and a bit how "if" loops work.
I think I have started coding this program too early because this is (to be honest) my 3rd program I have made. So indeed I still have to learn so much more about Lua.

I tried Bomb Bloke's code out and it actually worked! But what i still don't get, why do I need an "while true do" loop for this? When the password is wrong the PC shold restart,so does the "while true do" loop still make sence for this?
Also is there a way to access different programs depending on which username I use for the log in?

Thanks
CometWolf #7
Posted 01 May 2014 - 03:10 PM
Generally a rebooting password input is just annoying and a waste of time for everyone, which is why people tend to use a loop instead.

You could probably modify bomb's code to setup the usernames as tables aswell, then just store the password and the program within them.

users = {
  ZetShock = {
    password = "Test",
    program = "clear"
  },
  "Mineserver3000 = {
    password = "Test2",
    program = "edit derp"
  }
}
while true do
	    print("Gimme a username:")
	    local user = read()
	    print("Gimme a password:")
	    password = read("*")
	    if users[user] and users[user].password == password then
			    textutils.slowPrint("Hello")
			    sleep(1)
			    shell.run(users[user].program)
			    break
	    else
			    print("Invalid username or password")
	    end
end
Bomb Bloke #8
Posted 01 May 2014 - 04:35 PM
I have been confused about when i have to place end's and a bit how "if" loops work.

"if" blocks aren't loops. They just determine whether or not given chunks of code should be executed.

Regarding "end" statements, it's a lot easier to track where they should go if you're using correct indentation. This post will hopefully clarify matters for you somewhat.