Jump to content




read() bug



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

#1 applesauce10189

  • Members
  • 162 posts

Posted 18 January 2014 - 11:31 PM

I found a weird bug that I THINK is caused by read() basically, in a program I've written if you leave your caps lock on and try entering the password on my password program it doesn't do term.clear() as it should or anything, it just goes down a line, but if you turn off caps lock then it works perfectly as it should. btw all this happens when you put in the wrong password but if you put the right password without caps then it opens the door. Here's my code.

os.pullEvent = os.pullEventRaw
function password()
term.clear()
term.setCursorPos(1,1)
term.setTextColor(1024)
print("AppleOS V1")
term.setTextColor(1)
print("If you want to open the door, use the correct password.")
print("If you want to edit this computer, put the admin password.")
while true do
  local input = read("*")
    if input == "example1" then
      redstone.setOutput("right", true)
      sleep(3)
      redstone.setOutput("right", false)
      os.reboot()
    elseif input == "example2" then
      term.clear()
      term.setCursorPos(1,1)
      term.setTextColor(1024)
      print("AppleOS V1")
      break
    else return
      password()
    end
  end
end
password()

Quick note. In my latest version of the program (this one) I added the
else return
  password()
part, and it apparently works now, sorry to waste the time of who ever read this,

#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 18 January 2014 - 11:36 PM

When in doubt if something is a ComputerCraft bug, it normally isn't... Generally it is a good idea to just post it in Ask a Pro...

#3 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 19 January 2014 - 01:26 AM

Moved to Ask a Pro.

#4 Bomb Bloke

    Hobbyist Coder

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

Posted 19 January 2014 - 06:25 AM

Are you saying that you expect "EXAMPLE1" to be treated the same as "example1"? The texts ARE different, so the script is perfectly correct in treating them as such.

If you don't want it to do that, then you can convert what the user types to lowercase. For example:

local input = string.lower( read("*") )

Also bear in mind that it's poor practise to have the password() function call itself - this prevents each version of the running function from ending, so they all build up in RAM until your script crashes.

#5 applesauce10189

  • Members
  • 162 posts

Posted 19 January 2014 - 07:14 AM

I think you all missed the point of what I'm talking about. I wasn't saying the bug was the program I was saying that without caps lock it will say "WRONG!" when you do it wrong but in all caps if you put the wrong password it just goes down a line. That's not even a part of the program.

#6 Bomb Bloke

    Hobbyist Coder

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

Posted 19 January 2014 - 07:21 AM

Er, no, it won't say "WRONG!" under any circumstances. That text does not exist within the code you posted.

Without the "else return password()" business, entering an incorrect password would result in the while loop repeating without doing anything else, which results in "read()" being called again.

#7 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 19 January 2014 - 07:23 AM

its not a bug. even my version of the read function — which fixes all the bugs with the default and adds a bunch more features — when inserted into your code moves to a new line when the correct password is entered, while the redstone interaction is occurring.

View PostBomb Bloke, on 19 January 2014 - 06:25 AM, said:

Also bear in mind that it's poor practise to have the password() function call itself - this prevents each version of the running function from ending, so they all build up in RAM until your script crashes.
While it is still bad practise in most cases to use recursion and I think the OP should definitely fix this problem, in this case the script will not crash with an overflow due to the fact that they've used tail-recursion; return password()

Edited by theoriginalbit, 19 January 2014 - 07:23 AM.


#8 applesauce10189

  • Members
  • 162 posts

Posted 19 January 2014 - 07:42 AM

View PostBomb Bloke, on 19 January 2014 - 07:21 AM, said:

Er, no, it won't say "WRONG!" under any circumstances. That text does not exist within the code you posted.

Without the "else return password()" business, entering an incorrect password would result in the while loop repeating without doing anything else, which results in "read()" being called again.
If you read the original post, at the bottom I say this is a newer version and the code and I didn't figure out the new version worked fine until afterwards. With each and every post I make in this website I feel stupider and stupider.

#9 Bomb Bloke

    Hobbyist Coder

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

Posted 19 January 2014 - 08:14 AM

View Postapplesauce10189, on 19 January 2014 - 07:42 AM, said:

If you read the original post, at the bottom I say this is a newer version and the code and I didn't figure out the new version worked fine until afterwards. With each and every post I make in this website I feel stupider and stupider.
But you POSTED after working that out - hence I assumed you had some point or other you wished to make, or something you wished to know.

Beats me what it was, but you may as well understand why the script does what it does. :)

View Posttheoriginalbit, on 19 January 2014 - 07:23 AM, said:

While it is still bad practise in most cases to use recursion and I think the OP should definitely fix this problem, in this case the script will not crash with an overflow due to the fact that they've used tail-recursion; return password()
Had to look that term up to see why - interesting to know, thanks!





3 user(s) are reading this topic

0 members, 3 guests, 0 anonymous users