Lua print(table) in string (taking all its values)

Asked

Viewed 680 times

5

I created a system quickly to test a new variable, but I do not know how to print the users inserted in the table, and when I give print he shows it: 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 into it for account creation.

Let’s suppose I created 3 accounts and then in the "you want to create account" part I type "table" it will show me the number of accounts and passwords created.

But I want to display them not in size capacity but by string. Ex:

t = {'teste'}
print(t)

Console:

table: test

  • There was a vote to undo the exclusion of your question, which indicates that probably someone wants to answer it. So I broke the exclusion. Please let me know if you don’t like it. Thank you.

  • Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? You need something to be improved?

1 answer

3

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()

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

I used the most recommended technique to sweep through array but you could use the same one you used to check if user and password are registered.

I think you should review how this data is stored.

Your code will bring memory problems (roughly speaking) in the way it is organized.

There are some other problems but I think you’ll still touch it a lot.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.