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

Debugging that I can't fix?!

Started by TurtlesAndStuff, 11 April 2013 - 09:01 AM
TurtlesAndStuff #1
Posted 11 April 2013 - 11:01 AM
I have recently posted a topic for trying to fix this program but this is a completely new bug that, again, I cant fix. Making my lockup program more secure, I have added in a part to make a new file so the guessCount stays if someone trys to reboot it. Here is the code:

function lockUp()
   os.pullEvent = os.pullEventRaw
  
   local openGuessFile = io.open("guessCounter", "w")
   file:write(guessesSoFarFile)
   file:close()
   local openGuessFile = io.open("guessCounter", "r")
   local guessesSoFar = files:read("*n")
   file:close
  
   GUESSES_ALLOWED = 3
   password = "Password1"
   lockUpTime = 60
   lockUpTimeLeft = lockUpTime
   greeting = "Please verify that you have permission to access this ______: "
  
   while guessesSoFar < GUESSES_ALLOWED do
	  term.clear()
	  term.setCursorPos(1, 1)
	  textutils.slowPrint(greeting)
	  textutils.slowPrint("Attempts remaining:   "..GUESSES_ALLOWED - guessesSoFar)
	  textutils.slowPrint("Enter password:   ")
	
	  passwordGuesses = read("*")
	
	  if guess == password then
		 term.clear()
		 term.setCursorPos(1, 1)
		 textutils.slowPrint("Password Correct!")
		 textutils.slowPrint("Access Granted!!!")
		 textutils.slowPrint("..........Loading..........")
	
	  else
		 guessesSoFar = guessesSoFar + 1
		 term.clear()
   term.setCursorPos(1, 1)
		 textutils.slowPrint("Password Incorrect!")
		 textutils.slowPrint("Access Denied!!!")
		 textutils.slowPrint("..........Locking..........")
	  end
   end
  
   while lockUpTimeLeft > 0 do
	  term.clear()
	  term.setCursorPos(1, 1)
	  print("Number of password guesses exceeded.")
	  print()
	  print("Locking terminal for"..lockUpTimeLeft.." seconds.")
	  lockUpTimeLeft = lockUpTimeLeft - 1
	  sleep(1)
   end
  
   if lockUpTimeLeft == 0 then
	  term.clear()
	  term.setCursorPos(1, 1)
	  textutils.slowPrint("..........Rebooting program..........")
	  lockUp()
   end
end
lockUp()
It says that something is wrong in the variables near the top of the function. Any help would be appreciated! Thank you.
Kingdaro #2
Posted 11 April 2013 - 11:04 AM
Near the top,


   file:close

should be


   file:close()
Lyqyd #3
Posted 11 April 2013 - 11:06 AM
So, you open the file handle into `openGuessFile`, but then expect that value to teleport into `file`?
TurtlesAndStuff #4
Posted 11 April 2013 - 11:19 AM
So, you open the file handle into `openGuessFile`, but then expect that value to teleport into `file`?
What? Is what your saying that the;

   local openGuessFile = io.open("guessCounter", "w")
   file:write(guessesSoFarFile)
   file:close()
   local openGuessFile = io.open("guessCounter", "r")
   local guessesSoFar = files:read("*n")
   file:close()
needs to be;

   local file = io.open("guessCounter", "w")
   file:write(guessesSoFarFile)
   file:close()
   local file = io.open("guessCounter", "r")
   local guessesSoFar = files:read("*n")
   file:close()
?

Sorry I am new to programming in general ;)/> :wacko:/>
Sora Firestorm #5
Posted 11 April 2013 - 12:38 PM
It's fine to name your variable that, but when you use something like file:something(), it needs to
be openGuessFile:something(), because openGuessFile has the file data, not the variable file.
Did you write file:something() based on code tutorials?
The way it works is that the tutorial 'defaults' to your file handle being
'file'. In this case, your file handle needs to be 'openGuessFile'.

He thought file:something() worked something like table.something(), thus he didn't know to replace file with the variable.
TurtlesAndStuff #6
Posted 11 April 2013 - 12:58 PM
It's fine to name your variable that, but when you use something like file:something(), it needs to
be openGuessFile:something(), because openGuessFile has the file data, not the variable file.
Did you write file:something() based on code tutorials?
The way it works is that the tutorial 'defaults' to your file handle being
'file'. In this case, your file handle needs to be 'openGuessFile'.

He thought file:something() worked something like table.something(), thus he didn't know to replace file with the variable.
I put it like that thinking it needed to be, it was like that in a short thing that somebody put in my old topic about the other problem.
Thank you for the help ;)/>
TurtlesAndStuff #7
Posted 11 April 2013 - 01:33 PM
Ok, so, I have fixed that portion of the code now I have another error coming up when I try to run it. This is the error;
io:47: Unsupported format
and this is the code;

function lockUp()
   os.pullEvent = os.pullEventRaw
  
   local guessFile = io.open("guessCounter", "w")
   guessFile:write(guessesSoFarFile)
   guessFile:close()
   local guessFile = io.open("guessCounter", "r")
   local guessesSoFar = guessFile:read("*n")
   guessFile:close()
  
   GUESSES_ALLOWED = 3
   password = "Password1"
   lockUpTime = 60
   lockUpTimeLeft = lockUpTime
   greeting = "Please verify that you have permission to access this ______: "
  
   while guessesSoFar < GUESSES_ALLOWED do
	  term.clear()
	  term.setCursorPos(1, 1)
	  textutils.slowPrint(greeting)
	  textutils.slowPrint("Attempts remaining:   "..GUESSES_ALLOWED - guessesSoFar)
	  textutils.slowPrint("Enter password:   ")
	 
	  passwordGuesses = read("*")
	 
	  if guess == password then
		 term.clear()
		 term.setCursorPos(1, 1)
		 textutils.slowPrint("Password Correct!")
		 textutils.slowPrint("Access Granted!!!")
		 textutils.slowPrint("..........Loading..........")
	 
	  else
		 guessesSoFar = guessesSoFar + 1
		 term.clear()
   term.setCursorPos(1, 1)
		 textutils.slowPrint("Password Incorrect!")
		 textutils.slowPrint("Access Denied!!!")
		 textutils.slowPrint("..........Locking..........")
	  end
   end
  
   while lockUpTimeLeft > 0 do
	  term.clear()
	  term.setCursorPos(1, 1)
	  print("Number of password guesses exceeded.")
	  print()
	  print("Locking terminal for"..lockUpTimeLeft.." seconds.")
	  lockUpTimeLeft = lockUpTimeLeft - 1
	  sleep(1)
   end
  
   if lockUpTimeLeft == 0 then
	  term.clear()
	  term.setCursorPos(1, 1)
	  textutils.slowPrint("..........Rebooting program..........")
	  lockUp()
   end
end
lockUp()
I have looked at line 47 and the line only contains this;

	  term.setCursorPos(1, 1)
and this is the section that it is in;

   while lockUpTimeLeft > 0 do
	  term.clear()
	  term.setCursorPos(1, 1)
	  print("Number of password guesses exceeded.")
	  print()
	  print("Locking terminal for"..lockUpTimeLeft.." seconds.")
	  lockUpTimeLeft = lockUpTimeLeft - 1
	  sleep(1)
   end
Please help, I would really appreciate it ;)/> :wacko:/> :blink:/>
Lyqyd #8
Posted 11 April 2013 - 02:45 PM
`guessFile:read("*n")` should have either "*a" or "*l" instead of "*n".
TurtlesAndStuff #9
Posted 12 April 2013 - 03:27 AM
if i change it to "*a" or "*i" will it still read the first number of the file?
Lyqyd #10
Posted 12 April 2013 - 06:37 AM
It will read either the remainder of the file or the next line of the file. There is no mode to read the next number it finds in the file.
TurtlesAndStuff #11
Posted 12 April 2013 - 09:23 AM
Ok thanks
TurtlesAndStuff #12
Posted 12 April 2013 - 09:33 AM
now it says unsupported format again for line 47 :l help
Lyqyd #13
Posted 12 April 2013 - 11:10 AM
Please post the updated code.
TurtlesAndStuff #14
Posted 18 April 2013 - 01:44 PM
Ok here it is:

function lockUp()
   os.pullEvent = os.pullEventRaw
  
   local guessFile = io.open("guessCounter", "w")
   guessFile:write(guessesSoFarFile)
   guessFile:close()
   local guessFile = io.open("guessCounter", "r")
   local guessesSoFar = guessFile:read("*a")
   guessFile:close()
  
   GUESSES_ALLOWED = 3
   password = "Password1"
   lockUpTime = 60
   lockUpTimeLeft = lockUpTime
   greeting = "Please verify that you have permission to access this ______: "
  
   while guessesSoFar < GUESSES_ALLOWED do
	  term.clear()
	  term.setCursorPos(1, 1)
	  textutils.slowPrint(greeting)
	  textutils.slowPrint("Attempts remaining:   "..GUESSES_ALLOWED - guessesSoFar)
	  textutils.slowPrint("Enter password:   ")
	 
	  passwordGuesses = read("*")
	 
	  if guess == password then
		 term.clear()
		 term.setCursorPos(1, 1)
		 textutils.slowPrint("Password Correct!")
		 textutils.slowPrint("Access Granted!!!")
		 textutils.slowPrint("..........Loading..........")
	 
	  else
		 guessesSoFar = guessesSoFar + 1
		 term.clear()
   term.setCursorPos(1, 1)
		 textutils.slowPrint("Password Incorrect!")
		 textutils.slowPrint("Access Denied!!!")
		 textutils.slowPrint("..........Locking..........")
	  end
   end
  
   while lockUpTimeLeft > 0 do
	  term.clear()
	  term.setCursorPos(1, 1)
	  print("Number of password guesses exceeded.")
	  print()
	  print("Locking terminal for"..lockUpTimeLeft.." seconds.")
	  lockUpTimeLeft = lockUpTimeLeft - 1
	  sleep(1)
   end
  
   if lockUpTimeLeft == 0 then
	  term.clear()
	  term.setCursorPos(1, 1)
	  textutils.slowPrint("..........Rebooting program..........")
	  lockUp()
   end
end
lockUp()
Lyqyd #15
Posted 19 April 2013 - 04:47 AM
I'm sorry, I had my normal Lua and CC Lua confused for a moment; this is one of the few places they differ. Try changing it to "*all".