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

[lua]HELP ASAP-EMERGENCY :P

Started by predatorxil, 24 August 2012 - 08:15 PM
predatorxil #1
Posted 24 August 2012 - 10:15 PM
Im trying to fix my program for my terminal.
Here is wahts going on, the program startsup, shows login screem, and if i input my username, then password it lets me in, if i input anything in, ANYTHING, it doesnt show an error, but it just goes to the regular line on the terminal, with the arrow '> ' <—-that line
and i have to reboot to get the login back.

I am also adding that if i type the correct username but the incorrect password it works fine, and i get the message saying access denied.
its only when i hit enter on the username entry.

here's my code


local sPath = ":/rom/programs"
term.clear()
term.setCursorPos(1,1)
print("---------------------------------------------------")
term.setCursorPos(1,2)
local function centerText(text)
local x,y = term.getSize()
local x2,y2 = term.getCursorPos()
term.setCursorPos(math.ceil((x / 2) - (text:len() / 2)), y2)
print(text)
end

centerText("|		  R.S. Reactor Station Terminal		  |")
term.setCursorPos(1,3)
print("-------------------------------------------------------")
term.setCursorPos(1,4)
print("User Database: C:/Reactor/Terminal/Users")
term.setCursorPos(1,6)
print("Users:")
term.setCursorPos(1,7)
print("- predatorxil - coolwater97")
term.setCursorPos(1,8)
print("-------------------------------------------------------")
term.setCursorPos(1,9)
textutils.slowPrint("Message: Input UserName")
term.setCursorPos(1,17)
textutils.slowPrint("OS Version 2.5")
user1 = "predatorxil"
user2 = "coolwater97"
password1 = "elite13034"
password2 = "sarah1157"
term.setCursorPos(1,10)
write("UserName: ")
input = read()
if input == user1 then
term.setCursorPos(10,9)
textutils.slowPrint("Input Password")
term.setCursorPos(1,11)
write("Password: ")
input = read("*")
if input == password1 then
  term.setCursorPos(10,9)
  textutils.slowPrint("Access Approved")
  sleep(.5)
  rs.setOutput("back", true)
  sleep(5)
  rs.setOutput("back", false)
  sleep(1)
  os.reboot()
	else
	 term.setCursorPos(10,9)
	 textutils.slowPrint("Access Denied; Reactor Lockdown Initiated")
	 sleep(10)
	 os.shutdown()
	 end
	end
end
inventor2514 #2
Posted 24 August 2012 - 10:34 PM
You only shutdown the computer if the password check fails. The program just keeps running and ends if the username check fails.
predatorxil #3
Posted 24 August 2012 - 10:40 PM
Response to last post: ^ ^ ^ I maybe new to programming, but i know that that is not true waht he said, os.reboot() restarts the program, i dont want it to shutdown.
and i have had successfull programs that have used os.reboot() and its not that, THERE IS NO ERROR BEING SHOWN. please understand that, its only when someone fails to input correct username, it fails.
Tube7770 #4
Posted 24 August 2012 - 10:58 PM
Response to last post: ^ ^ ^ I maybe new to programming, but i know that that is not true waht he said
It is actually true. Look at the "if input == user1 then" line. If "input" doesn't match "user1" the program skips the rest of the code and exits the program.
This is not an error in the program.
You need to add something like:

if input ~= user1 then
os.shutdown()
portablejim #5
Posted 25 August 2012 - 12:25 PM
Just re-indenting your code so you can see what is happening:

if input == user1 then
	term.setCursorPos(10,9)
	textutils.slowPrint("Input Password")
	term.setCursorPos(1,11)
	write("Password: ")
	input = read("*")
	if input == password1 then
		term.setCursorPos(10,9)
		textutils.slowPrint("Access Approved")
		sleep(.5)
		rs.setOutput("back", true)
		sleep(5)
		rs.setOutput("back", false)
		sleep(1)
		os.reboot()
	else
		term.setCursorPos(10,9)
		textutils.slowPrint("Access Denied; Reactor Lockdown Initiated")
		sleep(10)
		os.shutdown()
	end
end
end  -- There is an extra 'end' here that is not used.
So if the first if test fails, it jumps to the end, exiting the program normally.

One way to get it to work is to do

function denyAccess()
	term.setCursorPos(10,9)
	textutils.slowPrint("Access Denied; Reactor Lockdown Initiated")
	sleep(10)
	os.shutdown()
end

if input == user1 then
	term.setCursorPos(10,9)
	textutils.slowPrint("Input Password")
	term.setCursorPos(1,11)
	write("Password: ")
	input = read("*")
	if input == password1 then
		term.setCursorPos(10,9)
		textutils.slowPrint("Access Approved")
		sleep(.5)
		rs.setOutput("back", true)
		sleep(5)
		rs.setOutput("back", false)
		sleep(1)
		os.reboot()
	else
		denyAccess()
	end
else
	denyAccess()
end

Even better would be

accessGranted = false

if input == user1 then
	term.setCursorPos(10,9)
	textutils.slowPrint("Input Password")
	term.setCursorPos(1,11)
	write("Password: ")
	input = read("*")
	if input == password1 then
		accessGranted = true
	end
end

if accessGranted then
	term.setCursorPos(10,9)
	textutils.slowPrint("Access Approved")
	sleep(.5)
	rs.setOutput("back", true)
	sleep(5)
	rs.setOutput("back", false)
	sleep(1)
	os.reboot()
else
	term.setCursorPos(10,9)
	textutils.slowPrint("Access Denied; Reactor Lockdown Initiated")
	sleep(10)
	os.shutdown()
end