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

[Question] Adding a password to a menu system

Started by TuxDeep, 20 November 2012 - 02:13 PM
TuxDeep #1
Posted 20 November 2012 - 03:13 PM
Hello, since this is my first post on these forums, I'll begin by saying that I apologize if the thread title is incorrect, since I don't know if normal CC coding would be considered 'lua' or not, hence my single [Question] tag. My program is meant to send the user to a menu, where they can pick either 'Nuclear Management' or 'Forcefield Management', and it works nicely; however, I was wondering if I could add some password protection before people can access the computer. I have tried adding this line of code before my main code, but it didn't work; a password prompt pops up, but even if I enter the password correctly, nothing happens.


while true do
term.clear()
term.setCursorPos(1,1)
print("Enter access code:")
local input = read("*")
   if input == "codeone" then

then my code


while x ~= "exit" do
		   print("--------------------")
		   print("TownyHub main menu:")
		   print("Enter 'nuclear' for nuclear management.")
		   print("Enter 'forcefield' for forcefield management.")
		   print("Enter 'exit' to go back to the previous menu.")
		   write("Choose an option now, please.")
		   print("--------------------")
		   x = io.read()
		   if x == "nuclear" then
		  
				 while y ~= "exit" do
				 print("-------------------")
				 print("Nuclear menu:")
				 print("To turn the reactor on, type on. To turn the reactor off, type off.")
				 print("To return to the main menu, type exit.")
				 write("")
				 print("-------------------")
				 y = io.read()
				 if y == "off" then
						rs.setOutput("left", true)
						print("----------------")
						print("You have chosen to turn off the nuclear reactor.")
						elseif y == "on" then
						rs.setOutput("left", false)
						print("----------------")
						print("You have chosen to turn on the nuclear reactor.")
				   end
			   end
		   end
		  if x == "forcefield" then
		  
				   while u ~= "exit" do
				   print("-----------------")
				   print("Forcefield menu:")
				   print("-")
				   print("To turn on forcefield, type 'on'. To turn it off, type 'off'")
				   print("To exit to the main menu, type 'exit'")
				   write("")
				   print("-----------------")
				   u = io.read()
				   if u == "on" then
						 rs.setOutput("right",true)
						 print("------------------")
						 print("Forcefields: Operational")
				   elseif u == "off" then
						 rs.setOutput("right",false)
						 print("------------------")
						 print("Forcefields: Non-Operational")
					  end
				   end
			   end
	
								  end							
	 end
	 end			
			


I apologize if this code is unkempt and just a pain to look at for some more experienced coders here, but I am an absolute beginner to any type of coding. I have used some reference code to fix up my code and produce what you see up there.

It would be extremely appreciated if someone could correct me or send me in the right direction.

Thanks a lot.
Goof #2
Posted 20 November 2012 - 10:17 PM
Before your menu system, you could just do:



local Password = "your password"
term.clear()
term.setCursorPos(1,1)
textutils.slowPrint("welcome user! Enter password, please")
textutils.slowWrite(" password > ")
passinput = read("#") -- this will input #'s instead of the letters you type.
if passinput == Password then
-- Your menu system here, or make a function with the menu.
else
term.clear()
term.setCursorPos(1, 1)
textutils.slowPrint("wrong password... Shutting down!")
sleep(4)
os.shutdown()
end







that will make a password system… Only if you enter the right code, it will start the menu.


__________________

Did i help? Then please preese the 1 ^
-Mikk809h
remiX #3
Posted 21 November 2012 - 01:44 AM
This should work perfectly :(/>/> If you need me to explain how it works, feel free to ask.

I haven't tested it but it should work with no errors.
Spoiler
code = "codeone"

function cls()
    term.clear()
    term.setCursorPos(1,1)
end

function nuclearReactor()
    cls()
    print("-------------------")
    print("Nuclear menu:")
    print("To turn the reactor on, type 'on'.nTo turn the reactor off, type 'off'.")
    print("To return to the main menu, type exit.")
    print("-------------------")
    write(" > ")
    input = string.lower(read())
    if input == "off" then
        rs.setOutput("left", true)
        print("n----------------")
        print("You have chosen to turn off the nuclear reactor.")
        sleep(2.5) return nuclearReactor()
    elseif input == "on" then
        rs.setOutput("left", false)
        print("n----------------")
        print("You have chosen to turn on the nuclear reactor.")
        sleep(2.5) return nuclearReactor()
    elseif input == "exit" then main()
    else return nuclearReactor()
    end
end

function forceField()
    cls()
    print("-----------------")
    print("Forcefield menu:")
    print("To turn on forcefield, type 'on'.nTo turn it off, type 'off'")
    print("To exit to the main menu, type 'exit'")
    print("-----------------")
    write(" > ")
    input = string.lower(read())
    if input == "on" then
        rs.setOutput("right",true)
        print("n------------------")
        print("Forcefields: Operational")
        sleep(2.5) return forceField()
    elseif input == "off" then
        rs.setOutput("right",false)
        print("n------------------")
        print("Forcefields: Non-Operational")
        sleep(2.5) return forceField()
    elseif input == "exit" then main()
    else return forceField()
    end
end

function menu()
    cls()
    print("--------------------")
    print("TownyHub main menu:")
    print("Enter 'nuclear' for nuclear management.")
    print("Enter 'forcefield' for forcefield management.")
    print("Enter 'exit' to go back to the previous menu.")
    print("Choose an option now, please.")
    print("--------------------")
    write(" > "
    op = string.lower(io.read())
    if op == "nuclear" then nuclearReactor()
    elseif op == "forcefield" then forceField()
    elseif op == "exit" then return
    else return menu()
    end
end

function main()
    cls()
    print("Enter access code:")
    write(" > ")
    input = read("*")
    if input == code then
        menu()
    else
        print("nIncorrect access code!")
        sleep(2)
        return main()
    end
end

-- Clean up
term.clear()
term.setCursorPos(1,1)
TuxDeep #4
Posted 21 November 2012 - 04:08 AM
Thank you so much, both of you, it's been bugging me all night. There's a problem, though, whenever I try to implement Mikk's code, it spits an error at me; however, for remiX's code, it just doesn't work. I tried using yours and then running the program on the same computer (I edited the code using Notepad++), but this time it doesn't even launch.

For example,
>hubcontrol
>

Like that, it doesn't even run the program. Sorry if I'm asking too much out of you guys, but I'm slowly creeping into the world of programming from CC, and it's sort of interesting. RemiX, I'd also appreciate if you explained what you did in your code, because I've never seen a layout like that. It looks like you removed my loop and replaced everything with functions, atleast from what I can infer.

Thanks again!



Edit: I've got college in a bit, so I won't be able to reply for ~2 hours.
remiX #5
Posted 21 November 2012 - 05:40 AM
Oh my word. I forgot one little line. Just before – Clean up put main() ^^ I forgot to call the function to start it :/