Jump to content




Break not working like I need


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

#1 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 12 November 2012 - 04:23 PM

I've got a block of code in my script here that is supposed to log in to my system. However, it is not leaving the loop I created. It does successfully log in when I enter the credentials, but then it restarts the loop. It's supposed to leave the loop as soon as it's logged in. What did I do wrong?
-- 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


#2 D3matt

  • Members
  • 830 posts

Posted 12 November 2012 - 04:44 PM

Your break is breaking out of your if/then comparison, not out of the loop, I think. Make it a function and use return?

#3 XoX

  • New Members
  • 53 posts

Posted 12 November 2012 - 05:04 PM

It should work just fine.

#4 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 12 November 2012 - 05:04 PM

View PostD3matt, on 12 November 2012 - 04:44 PM, said:

Your break is breaking out of your if/then comparison, not out of the loop, I think. Make it a function and use return?

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 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 12 November 2012 - 05:25 PM

View PostLyqyd, on 12 November 2012 - 05:04 PM, said:

View PostD3matt, on 12 November 2012 - 04:44 PM, said:

Your break is breaking out of your if/then comparison, not out of the loop, I think. Make it a function and use return?

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
I've tried that, and a few other variations of the same thing. It doesn't work though, and I don't know why...

#6 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 12 November 2012 - 05:50 PM

Then you'll need to post the whole code.

#7 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 12 November 2012 - 06:03 PM

eh.....how fun. I'll give you the startup link, the rest should run automatically.
http://pastebin.com/GWUUfNeh

#8 cheekycharlie101

  • Members
  • 231 posts

Posted 12 November 2012 - 06:59 PM

try this. this is how i use loops for logins. at the top define a varible. like this

locked = true
while locked do
-- code

then when you login to break the loop just do this.

locked = false
its 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 ChunLing

  • Members
  • 2,027 posts

Posted 12 November 2012 - 09:47 PM

Use repeat ... until logged, not while not logged do ... end. That eliminates the need for break.

#10 Espen

    Curious Explorer

  • Members
  • 708 posts

Posted 13 November 2012 - 12:58 AM

Just want to chip in with some general info about break vs. return:
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 Sarcastic_Bannana

  • New Members
  • 11 posts

Posted 13 November 2012 - 03:53 AM

Forgive me if I'm wrong but I experienced a similar problem of not being able to break out of a loop however I got arround that by using break end:

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 :P/>

#12 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 13 November 2012 - 04:44 AM

I updated the code to work with repeat until logged == true, but it does not work. It will break out of the loop if I enter the user/pass correctly twice, but I shouldn't have to do that....

#13 Fiery Arcanine

  • New Members
  • 19 posts
  • LocationHome

Posted 13 November 2012 - 06:35 AM

Now, tell me. What does it need to do?
Stop after first one correct?

By the way add this script in the beginning:
local pullEvent = os.pullEvent
os.pullEvent = os.pullEventRaw
and at the end of the code (when the password is correct)
os.pullEvent = pullEvent
It will prevent people from terminating the program
Source (CC wiki)

#14 remiX

  • Members
  • 2,076 posts
  • LocationSouth Africa

Posted 13 November 2012 - 07:58 AM

View PostFiery Arcanine, on 13 November 2012 - 06:35 AM, said:

Now, tell me. What does it need to do?
Stop after first one correct?

By the way add this script in the beginning:
local pullEvent = os.pullEvent
os.pullEvent = os.pullEventRaw
and at the end of the code (when the password is correct)
os.pullEvent = pullEvent
It 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 Fiery Arcanine

  • New Members
  • 19 posts
  • LocationHome

Posted 13 November 2012 - 08:18 AM

View PostsIdEkIcK_, on 13 November 2012 - 07:58 AM, said:

View PostFiery Arcanine, on 13 November 2012 - 06:35 AM, said:

Now, tell me. What does it need to do?
Stop after first one correct?

By the way add this script in the beginning:
local pullEvent = os.pullEvent
os.pullEvent = os.pullEventRaw
and at the end of the code (when the password is correct)
os.pullEvent = pullEvent
It 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 Cranium

    Ninja Scripter

  • Moderators
  • 4,031 posts
  • LocationLincoln, Nebraska

Posted 13 November 2012 - 12:39 PM

I already do use ctrl t prevention on it. But that's not hte issue I'm having. I need to know why it wants to loop repeatedly even after logging in successfully.

#17 cheekycharlie101

  • Members
  • 231 posts

Posted 13 November 2012 - 07:37 PM

i know this is NOT the answer you want. but no one can really find an answer. maybe you should just make a more simple lock. i know this is not the right way of dealing with program problems but if "lua pro's" cant even get it working then i dont think you will find a problem.

#18 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 13 November 2012 - 07:45 PM

View Postcheekycharlie101, on 13 November 2012 - 07:37 PM, said:

i know this is NOT the answer you want. but no one can really find an answer. maybe you should just make a more simple lock. i know this is not the right way of dealing with program problems but if "lua pro's" cant even get it working then i dont think you will find a problem.

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 cheekycharlie101

  • Members
  • 231 posts

Posted 13 November 2012 - 08:46 PM

View PostLyqyd, on 13 November 2012 - 07:45 PM, said:

View Postcheekycharlie101, on 13 November 2012 - 07:37 PM, said:

i know this is NOT the answer you want. but no one can really find an answer. maybe you should just make a more simple lock. i know this is not the right way of dealing with program problems but if "lua pro's" cant even get it working then i dont think you will find a problem.

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.
thats fair enough. i just think that what is the point of having an error in your program thats really hard to solve and you can just write a easier script that does the same thing and does work. i just think its a slight waste of time thats all.

#20 Espen

    Curious Explorer

  • Members
  • 708 posts

Posted 13 November 2012 - 10:46 PM

@Cranium:
Spoiler
Ok, scratch everything I just said. I've downloaded your program in the meantime and played around with it. It's not your tables, they seem to be fine.
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. :P/>

Edited by Espen, 14 November 2012 - 12:46 AM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users