Jump to content




String Limit


  • You cannot reply to this topic
5 replies to this topic

#1 koslas

  • Members
  • 62 posts
  • LocationChristchurch, New Zealand

Posted 20 August 2014 - 01:57 PM

I've got a program, which you can register for, and I want a limit of 15 to be in the username, but I'm not sure how to make a limit of the number or characters in a string, I've tried searching, but haven't found anything
Here is my register function

function register()
	    nSelect = 1
	    clear()
	    border()
	    term.setCursorPos(2,18)
	    print("Version: "..version)
	    term.setCursorPos(1,3)
	    cPrint("Please Register")
	    cPrint("")
	    cWrite("Username: ")
	    print("")
	    cWrite("Password: ")
	    print("")
	    cWrite("Confirm Password: ")
	    term.setCursorPos(26,5)
	    user = read()
	    term.setCursorPos(26,6)
	    pass = read("*")
	    term.setCursorPos(30,7)
	    confirm = read("*")
	    cPrint("")
--	  Make Account
	    if fs.exists("/.slots/users/"..user.."/password") then
			    cPrint("Account already created")
			    sleep(2)
	    else
			    if pass == confirm then
					    fs.makeDir(".slots/users/"..user)
					    makeAccount = fs.open("/.slots/users/"..user.."/password", "w")
					    makeAccount.write(pass)
					    makeAccount.close()
					    cPrint("Account created")
					    freeSpins = 5
					    sleep(2)
					    spinLoop()
			    else
					    cPrint("Passwords do not match")
					    sleep(2)
			    end
	    end
end


#2 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 20 August 2014 - 02:18 PM

Have a read of my post here.

#3 koslas

  • Members
  • 62 posts
  • LocationChristchurch, New Zealand

Posted 20 August 2014 - 02:22 PM

I'm looking at your custom read function program, could you quickly explain how to use it? As it looks a bit too confusing for me, just scimming the code. And reading the description, as there is no explaination for the second arguement.

Edited by koslas, 20 August 2014 - 02:24 PM.


#4 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 20 August 2014 - 02:41 PM

The second argument, just like the ComputerCraft read function, is designed for a history playback. Basically you know how in CraftOS shell you can press up and it will show you your last run lines, that is how they are provided, through the history argument.
So a basic usage example where the history is able to be seen is the following
local history = {}
while true do
  local input = read(nil, history)
  table.insert(history, input)
end
however for this particular case you don't need to use history, so providing a nil value there will have it ignored. so reading with your requirements would be read(nil, nil, 15) to get a 15 character username using my read function.

#5 koslas

  • Members
  • 62 posts
  • LocationChristchurch, New Zealand

Posted 20 August 2014 - 02:44 PM

Okay, that seems easy, and if I wanted a limit on a password I would do read("*", nil, 15) just as an example for a 15 character max password? Also would adding this to my code, make me need to change all my read functions that I currently use to read(nil, nil, nil) or, read("*", nil, nil)?

Edited by koslas, 20 August 2014 - 02:47 PM.


#6 theoriginalbit

    Semi-Professional ComputerCrafter

  • Moderators
  • 7,332 posts
  • LocationAustralia

Posted 20 August 2014 - 04:02 PM

that is precisely how you'd do a password input.

no you'd not need to change all your read functions, if you omit arguments it assumes nil, hence the fact that you don't have to supply the 4th argument in that call either. The only reason we need to specify the nil arguments is when we're wanting to supply a specific argument, such as the read limit; I deliberately designed this function override to work with ComputerCraft native read calls :)





2 user(s) are reading this topic

0 members, 2 guests, 0 anonymous users