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

Run through tables. Simple register and login

Started by Alerith, 09 April 2015 - 05:47 AM
Alerith #1
Posted 09 April 2015 - 07:47 AM
Hi again, I have another question regarding a simple register and login with tables. The thing is that I can't run through the tables using the loop For. I don't know what I'm doing bad. I searched in the web and in the forum but I can't find something that I can use.
Here is my code:

local users = {}
local passwords = {}

local key, keymenu, uslog, pwdlog, us_aux, pwd_aux, i

print("1. Login")
print("2. Register")
keymenu = read()

if keymenu == 2 then
    repeat
        for i=1, #users do
	        term.clear()
	        write("User: ")
	        users[i] = read()
	        for i=1, #passwords do
		        term.clear()
		        write("Password: ")
		        passwords[i] = read("*")
	        end
        end
        print("Continue? Y/N")
        key = read()
    until key == N
else
    term.clear()
    term.setCursorPos(1,1)
    write("User: ")
    uslog = read()
    write("Password: ")
    pwdlog = read("*")
    for i=1, #users do
	    for i=1, #passwords do
		    if uslog == users[i] or pwdlog == passowrds[i] then
			    us_aux = uslog
			    pwd_aux = pwdlog
		    end
	    end
    end

    if us_aux == uslog and pwd_aux == pwdlog then
	    term.clear()
	    term.setCursorPos(24,9)
	    print("Login successful")
    end
end
 

I'm pretty sure there is another way much simplier than this, but that is not my problem right now, I just want to finish this first. If you execute the code, It will prompt the first menu, login and register. Select register and it will ask user and pwd. When the pwd is entered, the program is terminated. :/

Thank you in advance
Dragon53535 #2
Posted 09 April 2015 - 08:04 AM
Your program is never registering, you're actually trying to login. Your problem lies with this line:

if keymenu == 2 then
This is never evaluating to true, since read() returns a string and not a number, what you probably want is

if keymenu == "2" then
which will then run your register part.

In your register part however, you'll still have problems.

for i=1, #users do
At the start of your code, #users will be 0, you cannot start from 1, go up, and reach 0. So this loop will NEVER execute.
You probably would want to rewrite that whole loop into this:

term.clear()
write("User: ")
users[#users+1] = read() --# Set the next index in the users table to the username
term.clear()
write("Password: ")
passwords[#users] = read("*") --# set the SAME index in the password table to the password

And then at the END of your register part. it will error again. or at least not work correctly.

until key == N
N is being seen as a variable, and since it is not defined anywhere, is set to nil. If you just press enter, or type text in the read() before hand, it will NEVER equal nil. What you probably want is that N to be in quotes to make it a string.


Also, typo here:

if uslog == users[i] or pwdlog == passowrds[i] then
Oh, and you'll probably want that or to be an and, since someone could just guess the username and get in.

You're going to want to change the i inside one of these for loops, since there cannot be two different variables named i. For as long as you are inside the #passwords loop, i is whatever number THAT loop is on. so if the #users loop is at 3, and #passwords is at 1, then i == 1.

for i=1, #users do
		    for i=1, #passwords do
				    if uslog == users[i] or pwdlog == passowrds[i] then
						    us_aux = uslog
						    pwd_aux = pwdlog
				    end
		    end
    end


And then FINALLY:
Look into the FS API on how to save usernames and passwords to files. and then SHA256 to hash the passwords so no one can read them.




Edit: Did I miss ANYTHING?
Edited on 09 April 2015 - 06:12 AM
DannySMc #3
Posted 09 April 2015 - 02:24 PM
- snip -

On this use the following to convert a string to number:
keymenu = tonumber(read())
This will return 2 and not "2" unless you type af2 then it will error.
HPWebcamAble #4
Posted 09 April 2015 - 08:09 PM
On this use the following to convert a string to number:
keymenu = tonumber(read())
This will return 2 and not "2" unless you type af2 then it will error.

It shouldn't error unless you don't give it anything
(What is af2?)


tonumber()  --#> bad argument, value expected

tonumber("") --#> nil

tonumber("10") --#> 10

tonumber("10hi")  --#> nil