-- starting script
bootUp()
logged = false
if userExists == false then
smartAPI.userCreate(2)
end
--login script
local attempt = 1
logged = false
while logged == false do
local user, pass = login()
local tUsers = smartAPI.loadUsers()
for i = 1, #tUsers do
if tUsers[i].username == user and tUsers[i].password == pass then
logged = true
end
end
if attempt >= 5 then
smartAPI.errorHandle("LOCK")
elseif logged == true then
break
elseif logged == false then
smartAPI.errorHandle("Username or password is incorrect")
attempt = attempt + 1
end
end
Break not working like I need
#1
Posted 12 November 2012 - 04:23 PM
#2
Posted 12 November 2012 - 04:44 PM
#3
Posted 12 November 2012 - 05:04 PM
#4
Posted 12 November 2012 - 05:04 PM
D3matt, on 12 November 2012 - 04:44 PM, said:
This is incorrect.
Break has some weird rules. It can only be the last statement of a block. Try this instead:
-- starting script
bootUp()
logged = false
if userExists == false then
smartAPI.userCreate(2)
end
--login script
local attempt = 1
local logged = false
while not logged do
local user, pass = login()
local tUsers = smartAPI.loadUsers()
for i = 1, #tUsers do
if tUsers[i].username == user and tUsers[i].password == pass then
logged = true
end
end
if attempt >= 5 then
smartAPI.errorHandle("LOCK")
elseif logged == false then
smartAPI.errorHandle("Username or password is incorrect")
attempt = attempt + 1
elseif logged == true then
break
end
end
#5
Posted 12 November 2012 - 05:25 PM
Lyqyd, on 12 November 2012 - 05:04 PM, said:
D3matt, on 12 November 2012 - 04:44 PM, said:
This is incorrect.
Break has some weird rules. It can only be the last statement of a block. Try this instead:
-- starting script
bootUp()
logged = false
if userExists == false then
smartAPI.userCreate(2)
end
--login script
local attempt = 1
local logged = false
while not logged do
local user, pass = login()
local tUsers = smartAPI.loadUsers()
for i = 1, #tUsers do
if tUsers[i].username == user and tUsers[i].password == pass then
logged = true
end
end
if attempt >= 5 then
smartAPI.errorHandle("LOCK")
elseif logged == false then
smartAPI.errorHandle("Username or password is incorrect")
attempt = attempt + 1
elseif logged == true then
break
end
end
#6
Posted 12 November 2012 - 05:50 PM
#7
Posted 12 November 2012 - 06:03 PM
http://pastebin.com/GWUUfNeh
#8
Posted 12 November 2012 - 06:59 PM
locked = true while locked do -- code
then when you login to break the loop just do this.
locked = falseits a basic method and works for me really well. try i. its kinda what you have done just a little less typing and it may work.
#9
Posted 12 November 2012 - 09:47 PM
#10
Posted 13 November 2012 - 12:58 AM
If you use break, then you will break out of the first loop (while, for, etc.) is in, but the program will then just continue to run any code that comes after the loop.
If you want the program to end instead, then you could us return, as that will either return from the function is was executed in, or return from your program (thus ending it) if it was executed outside of any function.
while true do
print("In the infinite loop that actually won't loop.^^")
break
end
print("This WILL be printed after the break.")
while true do
print("In the infinite loop that actually won't loop.^^")
return
end
print("This will NOT be printed after the return.")
But as mentioned above, ending your program with return won't work if you're returning from a function.
Then it might be better to return a boolean value and make the check outside of the function:
function foo()
while true do
print("In the infinite loop that actually won't loop.^^")
return false
end
print("This will NOT be printed after the break.")
return true -- pro forma, but not really of use in this example; if unclear, don“t hesitate to ask :P/>/>
end
if not foo() then
return
end
print("This will NOT be printed after foo() returns.")
#11
Posted 13 November 2012 - 03:53 AM
i=1
while i=1 do
...my code...
if nOption == 4 then
term.clear()
term.setCursorPos(1, 1)
break end
That broke my loop, not sure if it'll apply to you
#12
Posted 13 November 2012 - 04:44 AM
#13
Posted 13 November 2012 - 06:35 AM
Stop after first one correct?
By the way add this script in the beginning:
local pullEvent = os.pullEvent os.pullEvent = os.pullEventRawand at the end of the code (when the password is correct)
os.pullEvent = pullEventIt will prevent people from terminating the program
Source (CC wiki)
#14
Posted 13 November 2012 - 07:58 AM
Fiery Arcanine, on 13 November 2012 - 06:35 AM, said:
Stop after first one correct?
By the way add this script in the beginning:
local pullEvent = os.pullEvent os.pullEvent = os.pullEventRawand at the end of the code (when the password is correct)
os.pullEvent = pullEventIt will prevent people from terminating the program
Source (CC wiki)
I'd think he left it out for now so he can terminate if the program bugs and doesn't want to return to console
#15
Posted 13 November 2012 - 08:18 AM
sIdEkIcK_, on 13 November 2012 - 07:58 AM, said:
Fiery Arcanine, on 13 November 2012 - 06:35 AM, said:
Stop after first one correct?
By the way add this script in the beginning:
local pullEvent = os.pullEvent os.pullEvent = os.pullEventRawand at the end of the code (when the password is correct)
os.pullEvent = pullEventIt will prevent people from terminating the program
Source (CC wiki)
I'd think he left it out for now so he can terminate if the program bugs and doesn't want to return to console
It was supposed to for later, as you stated.
#16
Posted 13 November 2012 - 12:39 PM
#17
Posted 13 November 2012 - 07:37 PM
#18
Posted 13 November 2012 - 07:45 PM
cheekycharlie101, on 13 November 2012 - 07:37 PM, said:
I'm not sure the fix I suggested was actually even tried. I'm also not going to go through the effort of trying to download and run the installer script onto a fresh computer in minecraft just to look at the rest of the code, when it could easily have all been posted in the first place.
#19
Posted 13 November 2012 - 08:46 PM
Lyqyd, on 13 November 2012 - 07:45 PM, said:
cheekycharlie101, on 13 November 2012 - 07:37 PM, said:
I'm not sure the fix I suggested was actually even tried. I'm also not going to go through the effort of trying to download and run the installer script onto a fresh computer in minecraft just to look at the rest of the code, when it could easily have all been posted in the first place.
#20
Posted 13 November 2012 - 10:46 PM
In fact, there is absolutely no problem if I run this startup program manually (after making sure the automatic update doesn't overwrite it again of course).
What happens is that as soon as you enter a correct user and password, the startup program seems to run again.
That is, it does indeed exit the loop, but for some reason the whole program is being run again.
I then tried to run your startup-code from the startup-file via shell.run(), instead of having the code itself in there.
That somehow didn't work though, which really puzzles me. When I tried os.run() instead, then it works without a hitch, but then it still has the same problem as before, i.e. it runs two times in a row.
I did get it working with shell.run() only if I removed everything but the login procedure.
So I suspect there's something fishy going on within your APIs, where you have some proram logic that somehow causes the whole script to run at least one more time after it has ended.
I haven't really looked through the rest of your code, as it is quite a lot of files and code, so I have no idea where this might be happening.
But rest assured that is has nothing to do with break.
It works fine... just two times in a row.
Edited by Espen, 14 November 2012 - 12:46 AM.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users











