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

help with my program

Started by Spongy141, 27 November 2012 - 02:38 AM
Spongy141 #1
Posted 27 November 2012 - 03:38 AM

print("Welcome to spongy141's account program, for the follow ?s please reply with and only a yes or no.")
term.write("Hello welcome! Are you new here?")
if read() == ("yes") then
print("Well then you need to make an account!")
print("First your going to enter your username, then password")
local username = read()
local password = read("*")
print("Your account information:")
print(username)
print(password)
else
print("Please fill in your account information")
term.write("Account username:?")
if read() == username then
term.write("Now your password:")
if read("*") == password then
print("Welcome-")
write(username)
else
print("Incorrect")
os.reboot()
end
else
print("Incorrect")
os.reboot()
end
end
right now, all I need to do it allow the username and password to be saved in a other program, then used from that program again, please help.
Kingdaro #2
Posted 27 November 2012 - 03:56 AM
Here's some nice username/password loading and saving functions.


function saveUserdata(filename, user, pass)
  local file = fs.open(filename, 'w')
  if file then
    file.writeLine(user)
    file.writeLine(pass)
    file.close()
  end
end

function loadUserdata(filename)
  local file = fs.open(filename, 'r')
  if file then
    local user = file.readLine()
    local pass = file.readLine()
    return user, pass
  end
end

And here are the functions in your script:

function saveUserdata(user, pass)
	local file = fs.open('.users/'..user, 'w')
	if file then
		file.write(pass)
		file.close()
	end
end

function loadUserdata(user)
	local file = fs.open('.users/'..user, 'r')
	if file then
		local pass = file.readAll()
		file.close()
		return pass
	end
end


print("Welcome to spongy141's account program, for the follow ?s please reply with and only a yes or no.")
term.write("Hello welcome! Are you new here?")
if read() == ("yes") then
	print("Well then you need to make an account!")
	print("First your going to enter your username, then password")

	local username = read()
	local password = read("*")

	print("Your account information:")
	print(username)
	print(password)

	saveUserdata(username, password) -- saves the user's user and pass to a filename of the username
	-- in the folder ".users"
	-- so if someone registers with the username "Bob", it'll save to the file ".users/Bob"
else
	print("Please fill in your account information")
	term.write("Account username:?")

	local username = read()
	local password = loadUserdata(username)

	if password then -- checks if the username exists (if the password exists, so does the user)
		term.write("Now your password:")
		if read("*") == password then
			print("Welcome-")
			write(username)
		else
			print("Incorrect Password")
			os.reboot()
		end
	else
		print("Username does not exist")
		os.reboot()
	end
end

Went ahead and indented it too, it really helps ^^
Spongy141 #3
Posted 27 November 2012 - 11:09 AM
Here's some nice username/password loading and saving functions.


function saveUserdata(filename, user, pass)
  local file = fs.open(filename, 'w')
  if file then
	file.writeLine(user)
	file.writeLine(pass)
	file.close()
  end
end

function loadUserdata(filename)
  local file = fs.open(filename, 'r')
  if file then
	local user = file.readLine()
	local pass = file.readLine()
	return user, pass
  end
end

And here are the functions in your script:

function saveUserdata(user, pass)
	local file = fs.open('.users/'..user, 'w')
	if file then
		file.write(pass)
		file.close()
	end
end

function loadUserdata(user)
	local file = fs.open('.users/'..user, 'r')
	if file then
		local pass = file.readAll()
		file.close()
		return pass
	end
end


print("Welcome to spongy141's account program, for the follow ?s please reply with and only a yes or no.")
term.write("Hello welcome! Are you new here?")
if read() == ("yes") then
	print("Well then you need to make an account!")
	print("First your going to enter your username, then password")

	local username = read()
	local password = read("*")

	print("Your account information:")
	print(username)
	print(password)

	saveUserdata(username, password) -- saves the user's user and pass to a filename of the username
	-- in the folder ".users"
	-- so if someone registers with the username "Bob", it'll save to the file ".users/Bob"
else
	print("Please fill in your account information")
	term.write("Account username:?")

	local username = read()
	local password = loadUserdata(username)

	if password then -- checks if the username exists (if the password exists, so does the user)
		term.write("Now your password:")
		if read("*") == password then
			print("Welcome-")
			write(username)
		else
			print("Incorrect Password")
			os.reboot()
		end
	else
		print("Username does not exist")
		os.reboot()
	end
end

Went ahead and indented it too, it really helps ^^
Wow thank you for the help, but the reason I do not indent is because since I use notepad++ it does something like that for me. But really thank you for the help, probably would have took me about 2 months to get it, thanks for the help.