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

Input Intermittenly exiting program

Started by Brodur, 12 July 2013 - 07:07 PM
Brodur #1
Posted 12 July 2013 - 09:07 PM
Hey there, I just made a login kind of program and I am running into some issuse, but no errors though, so I think I just botched it a tich.
Here is the code:

local oldPull = os.pullEvent
os.pullEvent = os.pullEventRaw
-- Above, getting rid of those pesky terminate "hackers" --
-- Below, defining accounts and passwords --
local a = {
a[1]="Brodur",
a[2]="Hard24get"
}
-- I'd like to condense these in the future if possible --
local p = {
p[1]="qwerty25",
p[2]="blackmesa"
}
term.clear()
term.setCursorPos(1,1)
print("Welcome to the MesaNet, running on BroDOS!")
sleep(0.5)
term.setCursorPos(1,3)
  print("Username: ")
  print("Password: ")
sleep(0.5)
term.setCursorPos(11,3)
input = read()  -- Entering username --
if input==a[1] then --Does the account even exist? if so, continue!--
  term.setCursorPos(11,4)
elseif input==a[2] then
  term.setCursorPos(11,4)
elseif input=="I give up" then -- Self explanatory --
os.reboot()
else
  term.clearLine()
  print("No such account!")
  sleep(1)
  shell.run("login")

code = read("*") -- Seeing if accounts and passwords match, if they do, login! --
if code==p[1] and input==a[1] then
  print(" ")
  print("Welcome "..a[1].."!")

elseif code==p[2] and input==a[2] then
  print(" ")
  print("Welcome "..a[2].."!")
else
  print("Invalid password!")
  sleep(1)
  shell.run("login")
end
end
os.pullEvent = oldPull -- Resetting pullEvent --
In the above, it intermittenly skips the password phase if I enter
Brodur
I have no idea why it is doing this, just that it is.
Hard24get
appears to be working fine, which leads me to believe that the issue is in the following code snippets somewhere:

if input==a[1] then --Does the account even exist? if so, continue!--
  term.setCursorPos(11,4)

code = read("*") -- Seeing if accounts and passwords match, if they do, login! --
if code==p[1] and input==a[1] then
  print(" ")
  print("Welcome "..a[1].."!")

Also, while we're at it, is there any way I could condese those two tables to have both password and username in one, and then create a part like this:

if code==p[1] and input==a[1] then
  print(" ")
  print("Welcome "..a[1].."!")
elseif code==p[2] and input==a[2] then
  print(" ")
  print("Welcome "..a[2].."!")
that can have an infinite(or high) number of accounts and passwords?

Also, pastebin, so you can inspect it on your own console.

–Edit–

Okay, now this is odd. It worked fine before, but now any input that matches a[1,2] or p[1,2] crashes the computer, forcing a shutdown.
Edited on 12 July 2013 - 07:28 PM
MR_nesquick #2
Posted 12 July 2013 - 09:30 PM
computercraft doesn't like the character –> [ ] <– inside a table. they are used to read the table.
try to use a1 = "Brodur"

example:

local table = {name1 = "string", name2 = "something"}
print(table.name1)
print(table.name2)
---------------------------------
string
someting

and under shell.run("login") you have forgotten to end the if statement

and and… shell.run(login) not ("login")

:)/> :)/> :)/> :)/> :)/> :)/>
pastebin working code:
pastebin get JP0Qygcv login
http://pastebin.com/JP0Qygcv
Brodur #3
Posted 13 July 2013 - 12:26 AM
I figured put what I had done wrong, since the if/elses were not separated by an end, but instead run all together, the last set was accidentally skipped occasionally.
I have fixed that part. The question of condensing and making infinite still stands.

Thanks for the help anyhow mate.
MatazaNz #4
Posted 13 July 2013 - 02:21 AM
You could try making 1 table, and call it users, like this:


users = {
  [user1] = {pass = "password1"};
  [user2] = {pass = "password2"}
}

This way, you can infinitely expand the users table to your liking.
Brodur #5
Posted 13 July 2013 - 10:18 AM
Ah, thank you Mataza. To vihctory! (Ignore that spelling, it's the phlegm)

Also, how would i go about doing the if/else's for that table? Ex: I know user1 can be changed to ex: Brodur, but how/what do I call the password when referring to it in the code?

Also, the program is crashing again. I think I am going to start again from scratch.

–Edit–
Also.. heh, this table is wrong in some way:

users = {
   [Brodur] = {pass = "qwerty25"};
   [Hard24get] = {pass = "blackmesa"};
   [Guest] = {pass = ""}
   [Admin] = (pass = ""}
}


It returns line 8, table index expected, got nil. Line 8 is the first line of this table.
albrat #6
Posted 14 July 2013 - 03:30 PM

local a = {
[1]={ "Brodur", "qwerty25" }, [2]= { "Hard24get", "blackmesa" }
}

your table is called by a[x][1] and a[x][2] which returns a[x][1] = Username a[x][2]= Password where x is the number of users listed in your login codes


for i = 1,#a do
  if input == a[i][1] then valid = true passval = i break end
end
if valid then
  code = read(*)
	if code == a[passval][2] then passvalid = true break end
end
if passvalid and valid then
  -- unlock code here
else
print("failed")
sleep(3)
os.reboot()
end
that basically will check all usernames and passwords in the table and if any are both valid it will proceed..
albrat #7
Posted 14 July 2013 - 03:44 PM
I decided not to edit my post above because this is basically a re-write of your code with changes that I hope will help….

Spoiler

-- antihack
local oldPull = os.pullEvent
os.pullEvent = os.pullEventRaw
-- varible list
local a = {
[1]={ "Brodur", "qwerty25" }, [2]= { "Hard24get", "blackmesa" }
}   -- new data can be inserted into the table easily [3] [4] etc...
-- display setup
term.clear()
term.setCursorPos(1,1)
print("Welcome to the MesaNet, running on BroDOS!")
sleep(0.5)
term.setCursorPos(1,3)
  print("Username: ")
  print("Password: ")
sleep(0.5)
term.setCursorPos(11,3)
-- get username
input = read() 
term.setCursorPos(11,4) -- Set term position for pass input
-- Always grab the password
code = read(*)
-- if you skip that you let people know when they guessed a username

-- check username against table
  for i = 1,#a do
  if input == a[i][1] then valid = true passval = i break end  -- if a username matched store the record number passval
end

-- Check the password for the 1 user
if valid then
	    if code == a[passval][2] then passvalid = true break end -- this is where passval checks only the password of the username we gave
end
-- now if we have a valid username and a valid password
if passvalid and valid then
  -- unlock code here
  os.pullEvent = oldPull
  print("welcome "..a[passval][1].." system unlocked")
  sleep(2)
else
print("failed")
sleep(3)
os.reboot() -- I assume this file is called from startup
end
Brodur #8
Posted 14 July 2013 - 10:40 PM
Thank you for the help, I will continue to butcher your code until I get a better grasp of it. As for the virables
 i, passval 
are they general LUA things? I did not see them defined in the program.
MR_nesquick #9
Posted 14 July 2013 - 10:59 PM
passval = i = 1 or 2 (in this case)


print("welcome "..a[passval][1].." system unlocked")
..a[1or2][1]..

if it's 1 then it's going to print: <welcome brodur system unlock>
if it's 2 then it's going to print: <welcome Hard24get system unlock>

defined in this for loop

for i = 1,#a do

#a =how many strings/int there is in your username table
albrat #10
Posted 15 July 2013 - 05:16 AM
technically I should have defined passval at the start of the program with :-

local passval = 0
then we always have a blank slate for passval, but since we only assign a vlaue to it if the username succeeds I decided not to assign it to a zero value… As our checks and usage are only when we succeed in finding a user in our table.

it took me several hours of searching to find a way to do the above code when I first tried to do the same thing. I hope you can translate what it is doing, it would be a good learning exercise to understand it. :)/>

Thank you MR_nesquick for your quick response to the question about the varibles. :D/>
Brodur #11
Posted 15 July 2013 - 09:11 AM
Ah, thank you very much folks. I'm off to go play around with tables then :)/>