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

Need Help With Code Please

Started by moneymaster2012, 17 June 2014 - 02:05 PM
moneymaster2012 #1
Posted 17 June 2014 - 04:05 PM
Ok, so, I made a little program where users can get a hotel room. (Well, just by telling the user what room number they are in.) And I was doing all that when I spotted a problem.


THE CODE:


password = math.random(1000,9999)
   function register()
	term.clear()
	term.setCursorPos(5,10)
	write"Password: "
	passwordA = read()
	 if passwordA == password then		   -------THIS IS THE PROBLEM. On this if else statement, it always goes to the "else" instead of the "if" part. I did type in the correct number password, but it doesn't
		 room = math.random(1,5)						  work
		 term.clear()
		 term.setCursorPos(5,10)
		 print("Your Room Number Is "..room..".")
		 term.setCursorPos(5,11)
		 print("Have a nice day!")
		 sleep(5)
		 os.reboot()
	 else
		 term.clear()
		 term.setCursorPos(5,10)
		 print("That Password is invalid. Please notify")
		 term.setCursorPos(5,11)
		 print("a staff member.")
		 sleep(5)
		 os.reboot()
	 end
	end
term.clear()	-------- PROGRAM STARTS HERE
term.setCursorPos(5,10)
write"Password for Password: "
passwordFP = read()
if passwordFP == "ben" then
	term.clear()
	term.setCursorPos(5,10)
	print("Password: "..password)
	sleep(5)
	register()
else
	register()
end
—————————————————————————


Thank you for reading this thread, and if you do actually take time looking at it and helping this program, that would mean a lot. If you can't understand what the problem is, I'm very sorry. I'm at a young age.
Bomb Bloke #2
Posted 17 June 2014 - 04:14 PM
read() always hands you a string, whereas your "password" variable will contain a number.

You could change the first line to:

password = tostring(math.random(1000,9999))

or the input reading line to:

passwordA = tonumber(read())
Neywiny #3
Posted 17 June 2014 - 09:27 PM
You also have a problem where the program ways write, don't know if that has come up but you should have it as either print("blah") of write("blah")
Bomb Bloke #4
Posted 18 June 2014 - 03:11 AM
He's using valid code there. If you only want to pass a single argument to a function, then the brackets are optional.
Edited on 18 June 2014 - 01:11 AM