Jump to content




What is wrong here?


  • You cannot reply to this topic
2 replies to this topic

#1 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 07 April 2014 - 02:57 AM

Normally I would never resort to simply posting my code and saying "What's wrong?", but I have been forced to resort to this as I have no idea what is wrong.
function promptForUser(...)
 local Args = {...}
 print("Please Enter Username & Password")
 term.write("Username: ")
 local Username = read()
 term.write("Password: ")
 local uPass = read("*")
 local User = {}
 local cPass = {}
 for i = 1, #Args/2, 2 do
  User[i] = Args[i]
  cPass[i] = Args[i+1]
 end
 for k,v in ipairs(User) do
  if v == Username then
   local x = k
  end
 end
 if Username == User[x] and uPass == cPass[x] then
  print("Welcome "..Username.."!")
  return Username
 else
  print("Incorrect Username or Password!")
  return promptForUser(...)
 end
end
The only thing I can say is the error seems to be related to defining x. If I forcefully change x to 1, the values input for that username & password return correctly and are accepted if correct.

#2 Bomb Bloke

    Hobbyist Coder

  • Moderators
  • 7,099 posts
  • LocationTasmania (AU)

Posted 07 April 2014 - 03:06 AM

 KingofGamesYami, on 07 April 2014 - 02:57 AM, said:

Normally I would never resort to simply posting my code and saying "What's wrong?", but I have been forced to resort to this as I have no idea what is wrong.

If you're not aware that anything's wrong, then why are you posting in the first place? ;) There's always symptoms to describe.

Anyway, in this case you're lucky and the issue is obvious - variable localisation. When you declare "x" as local, you're doing so within an "if" block - hence "x" only exists within it. Guess what happens when you try to access that variable outside of that block.

Try something like this:

 local x  -- "x" is now local to the whole function.
 for k,v in ipairs(User) do
  if v == Username then
   x = k
  end
 end

Another point, I don't much like the look of this:

 for i = 1, #Args/2, 2 do
  User[i] = Args[i]
  cPass[i] = Args[i+1]
 end

Because you've not included the rest of your script, I can't say whether it'll do what you want. I rather suspect, however, that you'd want to ditch the "/2".

#3 KingofGamesYami

  • Members
  • 3,002 posts
  • LocationUnited States of America

Posted 07 April 2014 - 03:39 AM

 Bomb Bloke, on 07 April 2014 - 03:06 AM, said:

 KingofGamesYami, on 07 April 2014 - 02:57 AM, said:

-snip-

If you're not aware that anything's wrong, then why are you posting in the first place? ;) There's always symptoms to describe.

Anyway, in this case you're lucky and the issue is obvious - variable localisation. When you declare "x" as local, you're doing so within an "if" block - hence "x" only exists within it. Guess what happens when you try to access that variable outside of that block.

Try something like this:

 local x  -- "x" is now local to the whole function.
for k,v in ipairs(User) do
  if v == Username then
   x = k
  end
end
Thanks! That definitely sounds like the problem.

 Bomb Bloke, on 07 April 2014 - 03:06 AM, said:

Another point, I don't much like the look of this:

 for i = 1, #Args/2, 2 do
  User[i] = Args[i]
  cPass[i] = Args[i+1]
end

Because you've not included the rest of your script, I can't say whether it'll do what you want. I rather suspect, however, that you'd want to ditch the "/2".
What I am doing there is simply splitting the arguments into two different arrays. Yes, I do want to divide by two because if I don't, it will run twice as many times as needed (due to the increment being 2) Edit: After testing it, yes I do need to take out that.
Edit: This is the entire script. It is a function.
If you mean how I use it:
os.loadapi("api")
User = api.promptForUser("Username", "Password", "Username", "Password", "Username", "Password") --#note you can have infinite usernames and passwords
if User == "Username" then
--#do this (haven't actually made it do anything here yet)
else
--# do something else
end

Edited by KingofGamesYami, 07 April 2014 - 03:51 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users