Question:
I created a system quickly to test a new variable, but I don't know how to get it to print the users inserted in the table, and when I go to print
it it shows this: table: 0035AE18
My code:
Conta = {balance = 0}
Contas = {}
Senhas = {}
Inicial = 5000
function Conta:withdraw (v)
Conta.Balance = Conta.Balance - v
end
function Login()
io.write("Digite seu Usuario\n")
usuariol = io.read()
io.write("Digite sua senha\n")
senhal = io.read()
for i=0, #Contas do
for j=0, #Senhas do
if usuariol == Contas[i] and senhal == Senhas[j] then
io.write("Logado com sucesso!")
Cadastro()
end
end
end
end
function Cadastro()
io.write("Ola, seja bem vindo ao sistema de Banco 1.0 By Charles \n")
io.write("Deseja criar uma conta? \n")
resposta = io.read()
if resposta == "sim" or resposta == "Sim" then
io.write("Ok, informe seu nome de usuario \n")
usuario = io.read()
io.write("Informe sua senha \n")
senha = io.read()
io.write("Aguarde um instante!\n")
if #Contas == 0 and #Senhas == 0 then
table.insert(Contas, 1, usuario)
table.insert(Senhas, 1, senha)
else
table.insert(Contas, usuario)
table.insert(Senhas, senha)
end
Login()
elseif resposta == "tabela" then
print(#Contas)
print(#Senhas)
end
end
Cadastro()
The system works like this: it creates the account, then you log back in to create an account.
Let's suppose I created 3 accounts and then in the part "you want to create an account" I type "table" it will show me the number of accounts and passwords created.
But I want to display them not by size capacity but by string . Ex:
t = {'teste'}
print(t)
Console:
table: test
Answer:
I think this is what you want:
Conta = {balance = 0}
Contas = {}
Senhas = {}
Inicial = 5000
function Conta:withdraw (v)
Conta.Balance = Conta.Balance - v
end
function Login()
io.write("Digite seu Usuario\n")
usuariol = io.read()
io.write("Digite sua senha\n")
senhal = io.read()
for i=0, #Contas do
for j=0, #Senhas do
if usuariol == Contas[i] and senhal == Senhas[j] then
io.write("Logado com sucesso!")
Cadastro()
end
end
end
end
function Cadastro()
io.write("Ola, seja bem vindo ao sistema de Banco 1.0 By Charles \n")
io.write("Deseja criar uma conta? \n")
resposta = io.read()
if resposta == "sim" or resposta == "Sim" then
io.write("Ok, informe seu nome de usuario \n")
usuario = io.read()
io.write("Informe sua senha \n")
senha = io.read()
io.write("Aguarde um instante!\n")
if #Contas == 0 and #Senhas == 0 then
table.insert(Contas, 1, usuario)
table.insert(Senhas, 1, senha)
else
table.insert(Contas, usuario)
table.insert(Senhas, senha)
end
Login()
elseif resposta == "tabela" then
for i,v in ipairs(Contas) do
print(v)
end
for i,v in ipairs(Senhas) do
print(v)
end
end
end
Cadastro()
See working on ideone . And on repl.it. Also posted on GitHub for future reference .
I used the most recommended technique to scan the entire array but you could use the same one you used to check if username and password are registered.
I think you should review how this data is stored.
Your code will bring memory issues (roughly speaking) to the way it's organized.
There are a few other problems but I think you'll still mess with it a lot.