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

Program is not running or throwing any error

Started by Quinncunx, 14 June 2012 - 12:03 AM
Quinncunx #1
Posted 14 June 2012 - 02:03 AM
I have been working on a simple door entry system, that allows a user to run an initial configuration of the code they want to use and the side of the console that it wants the output to be, however, I must of made a mistake in the code, but I cannot see what it is, if anyone could poke at the code and point me to the line number where I am going wrong I would be very grateful.


local pwfile ="pwfile.txt"
local sidefile ="sidefile.txt"
local file = fs.open (pwfile, "r")
local file2 = fs.open (sidefile, "r")
-- Checks if the password file and side file cfg exists
if file and file2 then
local password = file.readAll()
local side = file2.readAll()
file.close()
file2.close()
term.clear()
print("Key Code:")
print("Hit F9 to change your keycode")
local event, param1 = os.pullEvent(key)
-- Checks if f9 has been hit and opens change keycode dialogue
if param1 == 67 then
  local pw = "pwfile.txt"
  local file = fs.open (pwfile, "r")
  file.close()
  print("Please enter your current keycode:")
  local input read("*")
-- checks the entered password is correct
  if input == password then
	print("Now enter your new keycode:")
	local pwin read("*")
-- writes new password to file
	local npwin = pwin
	file = fs.open(pwfile, "w")
	file.write(npwin)
	file.close()
	os.reboot()
  else
  print("Your keycode was incorrect")
  sleep(5)
  os.reboot()
  end
-- runs actual password check to open door
  local input = read("*")
  if input == password then
   print("Keycode correct!")
   rs.setOutput(side, true)
   sleep(5)
   rs.setOutput(side, false)
   os.reboot()
  else
-- if keycode was not correct
   print("Keycode incorrect")
   sleep(2)
   os.shutdown()
  end
else
-- runs intial configuration of the system
print("Initial Configuration - Please enter your desired keycode:")
local inputpw = read("*")
local pwnew = inputpw
file = fs.open(pwfile, "w")
file.write(pwnew)
file.close()
term.clear()
print("Initial Configuration - Please enter the side of the console that the door is connected to\n Right | Left | Back")
local inputside = read()
local sidenew = inputside
file2 = fs.open(sidefile, "w")
file2.write(sidenew)
file2.close()
print("Settings configured OS will now reboot")
sleep(5)
os.reboot()
end
end
D3matt #2
Posted 14 June 2012 - 02:26 AM
It gets to os.pullEvent() and waits for key 67. It never gets it, so it never continues.
my_hat_stinks #3
Posted 14 June 2012 - 03:02 AM
It gets to os.pullEvent() and waits for key 67. It never gets it, so it never continues.

Not true
local event, param1 = os.pullEvent(key)

This is what they have, since they've not used " ", key is most likely nil, meaning it's waiting for any event at all.



Could you tell us at what point it's freezing, so we don't have to guess?
You've got plenty of prints there, so you should be able to narrow it down for us
Quinncunx #4
Posted 14 June 2012 - 10:37 AM
As soon as I run the program it freezes, I will add in some prints prior to

if file and file2 then
to see if its getting to that point
tfoote #5
Posted 14 June 2012 - 03:58 PM
at the very top you have

local pwfile ="pwfile.txt"
local sidefile ="sidefile.txt"
TRY it with a space before the " on both the lines
It would then look like

local pwfile = "pwfile.txt"
local sidefile = "sidefile.txt"
BigSHinyToys #6
Posted 14 June 2012 - 04:24 PM
the problem is with the logic. your program checks for the files but the files don't exist yet so skips all the other instructions and ends.

add print("hello") bellow the last end and run it.
edit the main problem is that there is two ends at the bottom where there should be one the other should be ablv the else that triggers the initial configure.

This code is not total fixed but will run enough you should be able to work from here
Spoiler

local pwfile ="pwfile.txt"
local sidefile ="sidefile.txt"
local file = fs.open (pwfile, "r")
local file2 = fs.open (sidefile, "r")
-- Checks if the password file and side file cfg exists
if file and file2 then
    local password = file.readAll()
    local side = file2.readAll()
    file.close()
    file2.close()
    term.clear()
    print("Key Code:")
    print("Hit F9 to change your keycode")
    local event, param1 = os.pullEvent(key)
    -- Checks if f9 has been hit and opens change keycode dialogue
    if param1 == 67 then
	    local pw = "pwfile.txt"
	    local file = fs.open (pwfile, "r")
	    file.close()
	    print("Please enter your current keycode:")
	    local input read("*")
	    -- checks the entered password is correct
	    if input == password then
		    print("Now enter your new keycode:")
		    local pwin read("*")
	    -- writes new password to file
		    local npwin = pwin
		    file = fs.open(pwfile, "w")
		    file.write(npwin)
		    file.close()
		    os.reboot()
	    else
		    print("Your keycode was incorrect")
		    sleep(5)
		    os.reboot()
	    end
-- runs actual password check to open door
	    local input = read("*")
	    if input == password then
		    print("Keycode correct!")
		    rs.setOutput(side, true)
		    sleep(5)
		    rs.setOutput(side, false)
		    os.reboot()
	    else
-- if keycode was not correct
		    print("Keycode incorrect")
		    sleep(2)
		    os.shutdown()
	    end
    end
else
-- runs intial configuration of the system
    print("Initial Configuration - Please enter your desired keycode:")
    local inputpw = read("*")
    local pwnew = inputpw
    file = fs.open(pwfile, "w")
    file.write(pwnew)
    file.close()
    term.clear()
    print("Initial Configuration - Please enter the side of the console that the door is connected ton Right | Left | Back")
    local inputside = read()
    local sidenew = inputside
    file2 = fs.open(sidefile, "w")
    file2.write(sidenew)
    file2.close()
    print("Settings configured OS will now reboot")
    sleep(5)
    os.reboot()
end
print("hello")
Quinncunx #7
Posted 16 June 2012 - 05:01 PM
Thanks for that input, it did make the program run, and I have altered some of the logic now, the only problem I seem to have now is the Key event is not working any suggestions

-Revised Code


local pwfile = "pwfile.txt"
local sidefile = "sidefile.txt"
local file = fs.open (pwfile, "r")
local file2 = fs.open(sidefile, "r")
--Checks if the password and side file cfg exists if true then runs phase 1 if false then runs phase 2
print("checks for cfg")
if file and file2 then
-- PHASE 1
local password = file.readAll()
local side = file2.readAll()
file.close()
file2.close()
term.clear()
print("Key Code:")
print("Hit F9 to change your keycode")
--need to move actual check to here
local input = read("*")
-- masks the keycode input

if input == password then
   print("Keycode Correct!")
   rs.setOutput(side, true)
   sleep(5)
   rs.setOutput(side, false)
   os.reboot()
  
-- Dim key event
local key, param1 = os.pullEvent(key)
-- checks to see if F9 has been hit
elseif param1 == 67 then
-- changing keycode code
	 local pw = "pwfile.txt"
	 local file = fs.open (pwfile "r")
	 file.close()
	 print("Please enter your current keycode:")
	 local input read("*")
--checks the entered password is correct
	   if input == password then
		  print("Now enter your new keycode:")
		  local pwin read ("*")
		  local npwin = pwin
		  file = fs.open(pwfile, "w")
		  file.write(npwin)
		  file.close()
		  print("Console will now reboot")
		  sleep(2)
		  os.reboot()
	   else
		  print("Your keycode was incorrect")
		  sleep(5)
		  os.reboot()
	   end
	else
	   print("Keycode Incorrect")
	   sleep(2)
	   os.shutdown()
	end

else
--Phase 2
--runs initial cfg
   print("Inital Configuration - Please enter your desired keycode:")
   local inputpw = read("*")
   local pwnew = inputpw
   file = fs.open(pwfile, "w")
   file.write(pwnew)
   file.close()
   term.clear()
   print("Inital Configuration - Please enter the side of the console that the door is connected ton Right | Left | Back")
   local inputside = read()
   local sidenew = inputside
   file2 = fs.open(sidefile, "w")
   file2.write(sidenew)
   file2.close()
   print("settings configured Console will now reboot")
   sleep(5)
   os.reboot()
end
--Print("This is the end, Debug line")

– EDIT I have solved this now.
Edited on 17 June 2012 - 03:11 PM