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

read() bug

Started by applesauce10189, 18 January 2014 - 10:28 PM
applesauce10189 #1
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,
theoriginalbit #2
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…
Lyqyd #3
Posted 19 January 2014 - 01:26 AM
Moved to Ask a Pro.
Bomb Bloke #4
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.
applesauce10189 #5
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.
Bomb Bloke #6
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.
theoriginalbit #7
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.

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 on 19 January 2014 - 06:23 AM
applesauce10189 #8
Posted 19 January 2014 - 07:42 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.
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.
Bomb Bloke #9
Posted 19 January 2014 - 08:14 AM
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. :)/>

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!