Text input for pseudo-AI [Solved]

Hello!
I’m playing around with Codea (again :)) ) and wanted to create a “pseudo” interactive program (ELIZA type).
I’ve been using a code from @sanit (http://twolivesleft.com/Codea/Talk/discussion/844/simple-keyboard-input-and-output/p1) and I wanted to draw questions and answers on the screen from corresponding tables.
I’ve tried different things without success. How can be accomplished that.
My up-to-now running code (which reads just one item from each table! 8-} ) is as follows:


function setup()
    print("Touch to show Keyboard\
and press return to show a message")
    q = {"What is your name?","What is your occupation?"} -- program's questions
    a = {"I'm pleased to meet you ","That's an interesting job "} --program's answers
    said = false
    i=1
    memory ={} -- this is for saving answers from user.
end

function draw()
    background(255)
    textMode(LEFT)

    drawquestion(q[i]) -- drawing the questions
    drawanswer(a[i]) -- drawing the answers

end

function drawquestion(pre)
    text(pre,100,600)
    w,h = textSize(pre)
    if not said then
        memory[i] = keyboardBuffer()
    end
    if memory[i] then
        text(memory[i],100+w+10,600)
    end
end

function drawanswer(res)
    if i<3 and said then
        text(res..memory[i],100,600- h)
    end
end

function touched(touch)
    if touch.state==ENDED then
        showKeyboard()
        said = false
    end
end

function keyboard(key)
    if key == "\
" then
        hideKeyboard()
        said =true
    end
end 

@quezadav Try changing touched to this. There’s still an error after the last question.


function touched(touch)
    if touch.state==ENDED then
        showKeyboard()
        if said then
            i=i+1
        end
        said = false
    end
end

@quezadav Change draw to this to stop the error from the above code.


function draw()
    background(255)
    textMode(LEFT)

    if i<3 then
        drawquestion(q[i]) -- drawing the questions
        drawanswer(a[i]) -- drawing the answers
    end
end

Wow! That was the fastest working help in all Codealand :open_mouth:
Many thanks!

@quezadav Change these functions so the if statement depends on the size of q .


function draw()
    background(255)
    textMode(LEFT)
    if i<=#q then
        drawquestion(q[i]) -- drawing the questions
        drawanswer(a[i]) -- drawing the answers
    end
end

function drawanswer(res)
    if said then
        text(res..memory[i],100,600- h)
    end
end

OK. Thanks again.