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

[question] If (something) or (something else) then

Started by Exerro, 23 June 2012 - 05:27 PM
Exerro #1
Posted 23 June 2012 - 07:27 PM
Im trying to make a multi-user login here is what i have:

if usernameinput = username or if usernameinput = username 2 then
it doesn't work…can someone help?
It will be in this code:

term.setCursorPos(3,3)
print("This is the Login page. Default settings are;")
term.setCursorPos(3,4)
print("username: newuser")
term.setCursorPos(3,5)
print("password: pass")
term.setCursorPos(3,7)
write("username: ")
usernameinput = read()
term.setCursorPos(49,7)
print("O")

term.setCursorPos(3,8)
write("password: ")
passwordinput = read("*")
term.setCursorPos(49,8)
print("O")

  filereader = fs.open("password", "r")
  password = filereader.readLine()
  filereader.close()
  fileread = fs.open("username", "r")
  username = fileread.readLine()
  fileread.close()
if passwordinput == password and usernameinput == username then
MysticT #2
Posted 23 June 2012 - 08:10 PM
It should be:

if input == username1 or input == username2 then
Just one if.
Exerro #3
Posted 23 June 2012 - 08:16 PM
i just tried this and it didn't work, could it be because username2/password2 don't exist

if passwordinput == password or password2 and usernameinput == username or username2 then
MysticT #4
Posted 23 June 2012 - 09:48 PM
You need to compare both times:

if (passwordinput == password or passwordinput == password2) and (usernameinput == username or usernameinput == username2) then
doing:

if a == b or c then
it checks if a equals b, or c is not nil or false.
this:

if a == b or a == c then
checks if a equals b, or a equals c.
Lyqyd #5
Posted 24 June 2012 - 10:51 AM
You need to compare both times:

if (passwordinput == password or passwordinput == password2) and (usernameinput == username or usernameinput == username2) then
doing:

if a == b or c then
it checks if a equals b, or c is not nil or false.
this:

if a == b or a == c then
checks if a equals b, or a equals c.

Your if statement is technically valid, but OP's logic is terrible. That would allow either password to be used with either username, so as long as you know your password, you can log in as any user.

awsumben13, you'd be much better off switching them around to be distinct cases, such as:


if (passwordinput == password and usernameinput == username) or (passwordinput == password2 and usernameinput == username2) then

A better (and more easily extended approach) would be to load a list of usernames and passwords from a file into a table, then check the table entries:


local validLogin = false
for uNum, uInfo in ipairs(usersTable) do
	if uInfo.name == usernameinput then
		if uInfo.pass == passwordinput then
			validLogin = true
		end
		break
	end
end
if validLogin then