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

Login System Issues

Started by ClassicRockFan, 07 April 2014 - 06:28 PM
ClassicRockFan #1
Posted 07 April 2014 - 08:28 PM
Hey guys, I've been doing a lot of lua coding recently,and this question is very nooby, but I'm having trouble getting my login script to work. Here is the code that I have. I don't know what is going wrong but its telling me that its an incorrect password. Thanks guys - CRF
P.S.: I'm trying to make a cash register type system


username = {"this slot", "00000"}
password = {"never works", "Admin"}
i = #username
write("Enter Your Cashier ID: ")
user = read()
for i = 1, i do
  if user == username[i] and pass == password[i] then
   access = true
  else
   access = false
  end
end
if access == true then
  print("Logging in...")
  sleep(1)
  titleBar()
elseif access == false then
  print("Incorrect username and password combination")
  sleep(2)
  os.reboot()
end
Edited on 07 April 2014 - 06:39 PM
guesswhat123212 #2
Posted 07 April 2014 - 08:38 PM
it looks like your looping all the combos so its only going to take the last list into account otherwise your access is going to keep getting changed in the loop. also your never asking for the password and reading it.
ClassicRockFan #3
Posted 07 April 2014 - 08:41 PM
Oops, that was a previous version, here is the current version

username = {"this slot", "00000"}
name = {"never works", "Admin"}
i = #username
write("Enter Your Cashier ID: ")
user = read()
for i = 1, i do
  if user == username[i] then
   access = true
   name = username[i]
  else
   access = false
  end
end
if access == true then
  print("Welcome "..name.."...")
  sleep(1)
  titleBar()
elseif access == false then
  print("Incorrect username and password combination")
  sleep(2)
  os.reboot()
end
Edited on 07 April 2014 - 06:49 PM
CometWolf #4
Posted 07 April 2014 - 08:43 PM
Where do you input the password…?
Agoldfish #5
Posted 07 April 2014 - 08:45 PM
There isn't a io.read() call… :P/>
ClassicRockFan #6
Posted 07 April 2014 - 08:48 PM
i just want to input the one thing which is the username. Ignore the password. I copied and pasted the new code in but the update didnt work properly, it should be updated on the previous post now.

It will also set the name of the cashier based upon their input id (username)

There isn't a io.read() call… :P/>

write("Enter Your Cashier ID: ")
user = read()
Edited on 07 April 2014 - 06:47 PM
CometWolf #7
Posted 07 April 2014 - 08:49 PM
The problem is your loop.

for i = 1, i do
  if user == username[i] then
   access = true
  else
   access = false
  end
end
Let's say you input the first username, access becomes true the first time the loop runs, but what happens the second time it runs? add a break statement after access = true to end the loop instantly.
Edited on 07 April 2014 - 06:50 PM
ClassicRockFan #8
Posted 07 April 2014 - 08:52 PM
okay, ill try it

perfect, thank you very much
Parzivail #9
Posted 07 April 2014 - 10:23 PM
OK, i see. The problem looks like it's in your loop; you are staring from slot 1 in the array, where the first index is 0. Also, "i" has already been declared, so consider changing the loop variable. You might want to change your loop from
for i = 1, i do
to
for j = 0,i do
.

Hope it helps.
-Parzivail
CometWolf #10
Posted 07 April 2014 - 10:42 PM
where the first index is 0.
Wrong, Lua by default starts it's arrays at 1. This means unless you add a value to 0 directly, it will always be nil.
Also, "i" has already been declared, so consider changing the loop variable.
This does not matter. It's the same as changing one variable to another value. Although admittedly the way he does it is kind of pointless.

for i=1,#username do
Would ofcourse make more sense, since the original value of i(#username) is discarded once it's used for the loop iterator anyway.
Cranium #11
Posted 07 April 2014 - 10:57 PM
You do want to break out of your loop as soon as you get a valid response. You can do this a number of ways, but based on your code, it'd be best if you use break.

username = {"this slot", "00000"}
name = {"never works", "Admin"}
i = #username
write("Enter Your Cashier ID: ")
user = read()
for i = 1, i do
  if user == username[i] then
   access = true
   name = username[i]
   break --# this will actually exit the loop early as soon as it finds a match within that table.
  else
   access = false
  end
end
if access == true then
  print("Welcome "..name.."...")
  sleep(1)
  titleBar()
elseif access == false then
  print("Incorrect username and password combination")
  sleep(2)
  os.reboot()
end
CometWolf #12
Posted 08 April 2014 - 05:26 AM
I swear, people on here just seem to ignore 90% of all replies…
Parzivail #13
Posted 08 April 2014 - 01:31 PM
where the first index is 0.
Wrong, Lua by default starts it's arrays at 1. This means unless you add a value to 0 directly, it will always be nil.

Haha thanks i'm still stuck in c++ XD
Edited on 08 April 2014 - 11:31 AM