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

How do I have one thing equal more than one things?

Started by Kadecamz, 29 September 2012 - 06:57 PM
Kadecamz #1
Posted 29 September 2012 - 08:57 PM
K, I'm making a computer login program that has multiple login usernames, but instead of using "if input == something or something then"
I'm trying to use "usernamecorrect = { kadecamz, Kadecamz }" and "if input == usernamecorrect".

But its not working, please tell me what I'm doing wrong.
Anonomit #2
Posted 29 September 2012 - 09:00 PM
You don't need two names of varying capitals if you use string.lower()


usernameCorrect = "kadecamz"
input = string.lower(read("*"))
if input == usernameCorrect then
--stuff to do
end

This way you could type in kadecamz, Kadecamz or kAdEcAmZ if you want, and it would still work.
Doyle3694 #3
Posted 29 September 2012 - 09:00 PM
I'm doing exactly such a program right atm.
local userFile
local passwordFile
local users = {}
local passwords = {}
local passwordEntered
local userEntered
local userNum = 0
local lineRead

function readUsersAndPasswords()
fs.makeDir("passwordFolder")
userFile = fs.open("passwordFolder/users","r")
passwordFile = fs.open("passwordFolder/passwords","r")


lineRead = userFile.readLine()
repeat
table.insert(users,lineRead)
lineRead = userFile.readLine()
until lineRead == nil

lineRead = passwordFile.readLine()
repeat
table.insert(passwords,lineRead)
lineRead = passwordFile.readLine()
until lineRead == nil
passwordFile.close()
userFile.close()
end

function password()
   textutils.slowPrint("Please enter your password "..users[userNum], 15)
   passwordEntered = read("*")
   if passwordEntered == passwords[userNum] then
	  term.clear()
	  term.setCursorPos(1,1)
	  textutils.slowPrint("Password match.", 15)
	  textutils.slowPrint("Welcome "..users[userNum], 15)
   elseif passwordEntered == "retry" then
	  username()
   else
	  term.clear()
	  term.setCursorPos(1,1)
	  textutils.slowPrint("Password missmatch. Please try again.", 15)
	  textutils.slowPrint("Type 'retry' to enter a new username.", 15)
	  password()
   end
end

function username()
   userNum = 0
   term.clear()
   term.setCursorPos(1,1)
   textutils.slowPrint("Welcome", 15)
   sleep(1)
   term.clear()
   term.setCursorPos(1,1)
   textutils.slowPrint("Please enter your username", 15)
   userEntered = read()
   for i=1, #users do
	  if userEntered == users[i] then
   	  userNum = i
		 break
	  end
   end
   if userNum == 0 then
	  textutils.slowPrint("Username could not be recognized. Please try again", 15)
	  username()
   else
	  term.clear()
	  term.setCursorPos(1,1)
	  textutils.slowPrint("Username recognized.", 15)
	  sleep(1)
	  term.clear()
	  term.setCursorPos(1,1)
	  password()
   end
end

readUsersAndPasswords()
username()

end
  


it saves usernames and passwords inside files.. I'm right now coding so that different users can do different stuff.
jag #4
Posted 29 September 2012 - 09:10 PM
Well if you use this function
-- The function returns true if the element is a value inside of the table
function table.contains(table, element)
  for _, value in pairs(table) do
	if value == element then
	  return true
	end
  end
  return false
end
and then got a array/table with different usernames
usernamecorrect = {
  "kadecamz",
  "Kadecamz",
  "anonomit",
  "Anonomit",
  "Doyle3694",
  "doyle3694"
}
then you can easily combine the input with the function like this
input = read()
if table.contains("usernamecorrect", input) then
  -- code to be executed
end
jag #5
Posted 29 September 2012 - 09:18 PM
I'm doing exactly such a program right atm.
Spoiler
local userFile
local passwordFile
local users = {}
local passwords = {}
local passwordEntered
local userEntered
local userNum = 0
local lineRead

function readUsersAndPasswords()
fs.makeDir("passwordFolder")
userFile = fs.open("passwordFolder/users","r")
passwordFile = fs.open("passwordFolder/passwords","r")


lineRead = userFile.readLine()
repeat
table.insert(users,lineRead)
lineRead = userFile.readLine()
until lineRead == nil

lineRead = passwordFile.readLine()
repeat
table.insert(passwords,lineRead)
lineRead = passwordFile.readLine()
until lineRead == nil
passwordFile.close()
userFile.close()
end

function password()
   textutils.slowPrint("Please enter your password "..users[userNum], 15)
   passwordEntered = read("*")
   if passwordEntered == passwords[userNum] then
	  term.clear()
	  term.setCursorPos(1,1)
	  textutils.slowPrint("Password match.", 15)
	  textutils.slowPrint("Welcome "..users[userNum], 15)
   elseif passwordEntered == "retry" then
	  username()
   else
	  term.clear()
	  term.setCursorPos(1,1)
	  textutils.slowPrint("Password missmatch. Please try again.", 15)
	  textutils.slowPrint("Type 'retry' to enter a new username.", 15)
	  password()
   end
end

function username()
   userNum = 0
   term.clear()
   term.setCursorPos(1,1)
   textutils.slowPrint("Welcome", 15)
   sleep(1)
   term.clear()
   term.setCursorPos(1,1)
   textutils.slowPrint("Please enter your username", 15)
   userEntered = read()
   for i=1, #users do
	  if userEntered == users[i] then
   	  userNum = i
		 break
	  end
   end
   if userNum == 0 then
	  textutils.slowPrint("Username could not be recognized. Please try again", 15)
	  username()
   else
	  term.clear()
	  term.setCursorPos(1,1)
	  textutils.slowPrint("Username recognized.", 15)
	  sleep(1)
	  term.clear()
	  term.setCursorPos(1,1)
	  password()
   end
end

readUsersAndPasswords()
username()

end
  


it saves usernames and passwords inside files.. I'm right now coding so that different users can do different stuff.
Really cool code! And really helpful!!

You just got me thinking of making a user system of my own!
Kadecamz #6
Posted 29 September 2012 - 09:23 PM
also, whenever I view a thread the top half of the screen is black. (bug with the forums, not my code)

And now I am getting a problem with my code on line 16



usernamecorrect = {
  "kadecamz",
  "Kadecamz"
}
correctpassword = {
  "1337",
  "kade"
  }
term.clear()
term.setCursorPos(1,1)
print("User/Access") --just for looks
print("ACCESS DENIED")
term.setCursorPos(17,7)
write("USERNAME: ")
username = read()
if table.contains("usernamecorrect", username) then --Getting "startup:16: attempt to call nil" on this line
print(" ")
write("Reading Username")
textutils.slowPrint("...")
print(username.." is a correct username!")
sleep(3)
term.setCursorPos(17,7)
term.clearLine()
term.setCursorPos(17,7)
write("PASSWORD: ")
password = read()
if password == correctpassword then
write("Reading Password")
textutils.slowPrint("...")
print(password.." is a correct password!")
sleep(3)
term.clear()
term.setCursorPos(1,1)
print("User/Access") --just for looks
print("GRANTED")
sleep(3)
shell.run("granted")
else
write("Reading Input")
textutils.slowPrint("...")
print("Wrong Entry!")
sleep(3)
os.reboot()
end
end
Kadecamz #7
Posted 29 September 2012 - 09:23 PM
EDIT: Accidental double post.
Doyle3694 #8
Posted 29 September 2012 - 09:30 PM
does it crash when you start the program? or when you type in your username?
Kadecamz #9
Posted 29 September 2012 - 09:31 PM
When i press enter

When i press enter
MysticT #10
Posted 29 September 2012 - 09:32 PM
The table.contains function doesn't exist. And you can't add it just like that, because the table api is protected. You would have to define a function (with another name, and outside the table api) that has the code jag_e_nummer_ett posted (wich he probably didn't test, since it would throw an error).
So, just add the function before using it, and change table.contains to it's name:

local function table_contains(t, val)
  for _,v in ipairs(t) do
    if v == val then
	  return true
    end
  end
  return false
end
...
if table_contains(usernamecorrect, username) then
...
Doyle3694 #11
Posted 29 September 2012 - 09:34 PM
oh, "usernamecorrect" shall not be in qoutes
jag #12
Posted 29 September 2012 - 09:53 PM
The table.contains function doesn't exist. And you can't add it just like that, because the table api is protected. You would have to define a function (with another name, and outside the table api) that has the code jag_e_nummer_ett posted (wich he probably didn't test, since it would throw an error).
So, just add the function before using it, and change table.contains to it's name:

local function table_contains(t, val)
  for _,v in ipairs(t) do
	if v == val then
	  return true
	end
  end
  return false
end
...
if table_contains(usernamecorrect, username) then
...
Let me ask you this: Did you test it?

Because I did and it worked!
ChaddJackson12 #13
Posted 29 September 2012 - 10:25 PM
Yeah, I would also like to know this :)/>/>
MysticT #14
Posted 29 September 2012 - 10:35 PM
The table.contains function doesn't exist. And you can't add it just like that, because the table api is protected. You would have to define a function (with another name, and outside the table api) that has the code jag_e_nummer_ett posted (wich he probably didn't test, since it would throw an error).
So, just add the function before using it, and change table.contains to it's name:

local function table_contains(t, val)
  for _,v in ipairs(t) do
	if v == val then
	  return true
	end
  end
  return false
end
...
if table_contains(usernamecorrect, username) then
...
Let me ask you this: Did you test it?

Because I did and it worked!
Hmm… it looks like they removed the global tables protection in 1.42, that's good, it wasn't really needed.
Sorry, I haven't tested 1.42 too much.
Anyway, that's in 1.42, so if someone plays in a previous version it will throw an error saying "Attempt to write to global".
jag #15
Posted 29 September 2012 - 10:37 PM
Spoiler
The table.contains function doesn't exist. And you can't add it just like that, because the table api is protected. You would have to define a function (with another name, and outside the table api) that has the code jag_e_nummer_ett posted (wich he probably didn't test, since it would throw an error).
So, just add the function before using it, and change table.contains to it's name:

local function table_contains(t, val)
  for _,v in ipairs(t) do
	if v == val then
	  return true
	end
  end
  return false
end
...
if table_contains(usernamecorrect, username) then
...
Let me ask you this: Did you test it?

Because I did and it worked!
Hmm… it looks like they removed the global tables protection in 1.42, that's good, it wasn't really needed.
Sorry, I haven't tested 1.42 too much.
Anyway, that's in 1.42, so if someone plays in a previous version it will throw an error saying "Attempt to write to global".
Well in that case, you're right!
So then go with your version


local function table_contains(t, val)
  for _,v in ipairs(t) do
	if v == val then
	  return true
	end
  end
  return false
end
Kadecamz #16
Posted 29 September 2012 - 11:42 PM
Oh…I'm in 1.4.1….
sadface

Oh…I'm in 1.4.1….
sadface
MysticT #17
Posted 29 September 2012 - 11:56 PM
Oh…I'm in 1.4.1….
sadface

Oh…I'm in 1.4.1….
sadface
That's why I said that you need to change the name. Making the changes I posted above should work.