Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

NOOB - can't figure out how to grab keyboard input

edited December 2016 in Questions Posts: 3

Hi all,

New to programming in general. I'm creating a program for school that calculates PI. I have the math working just fine but I want the user to enter a number, then grab this number and pass it to my procedure to perform my calculations. My code is below - I can't get the input from the keyboard to work. About 2/3 of the way down the code you will see a comment saying *** THIS ISN'T WORKING YET **. Not sure what I'm doing wrong.

Any pointers to articles or videos (answer's helpful too ;-) ) would be appreciated.

-- Assignment 7

-- Use this function to perform your initial setup
function setup()  
end

function dothemath (a)

  -- setup variables
  -- first variable - fac - changes from +1 to -1 on each loop
  -- this is so the formula will go number 1 - number 2 + number 3 ....
  fac = 1
  sum = 0

     for i = 1, a do
        term = fac / (2 * i - 1)
        fac = fac * -1 
        sum = sum + term
    end
      pi = (4*sum)  
end

-- when touched the keyboard will display
function touched(touch)
    if touch.state==ENDED then
        showKeyboard()
    end
end

-- if the 'return' key is pressed on the keyboard,
-- hide the keyboard  
function keyboard(key)
    if key == "\n" then
        hideKeyboard()
    end
end


-- This function gets called once every frame
function draw()
    -- This sets a dark background color 
    background(40, 40, 50)

    -- This sets the line thickness
    strokeWidth()

    -- Do your drawing here
     fill(122, 255, 0, 255)
     fontSize(25)
     font("Baskerville-bold")

    -- writes the text for correct counter
    text("How many iterations?",WIDTH*.5,HEIGHT*.8)
    fontSize(20)
    text("(Touch screen to enter number)",WIDTH*.5,HEIGHT*.75)
    fontSize(25)

     -- grab keyboard input, convert to number and pass to function
     -- to do the math
    -- ***** THIS ISN"T WORKING YET *** 
    a = keyboardBuffer()

    if a ~= nil then
      answer = tonumber("a")
      -- this is what I want to do
      -- pass in the keyboard answer, convert it to a number
      -- and calculate the answer

      -- dothemath(answer)

      -- hard code an answer for now to ensure it works
      -- which it does!
      dothemath(4)

      -- print answer to screen
      text("PI is ", WIDTH*.5,HEIGHT*.40)
      text(pi, WIDTH*.5,HEIGHT*.35)  
    end

end

Comments

  • edited December 2016 Posts: 87

    @oarnone

    Is this what your looking for?
    You can use this for the keyboard and then use the variable str the for calculations


    function setup() str = "" end function draw() background(255, 255, 255, 255) fontSize(100) --text(str,WIDTH/2,HEIGHT/2) buffer = keyboardBuffer() fill(255) if string.len(buffer)>0 then -- this line was changed fill(0, 255, 188, 255) text(buffer,WIDTH/2,HEIGHT/2) else fill(0, 255, 188, 255) text(str,WIDTH/2,HEIGHT/2) end end function touched() showKeyboard() str="" end function keyboard(key) if key == RETURN then str = buffer hideKeyboard() print(str) end end
  • dave1707dave1707 Mod
    Posts: 7,550

    @oarnone The code answer=tonumber("a") should be answer=tonumber(a) . When you call hideKeyboard(), that clears the keyboardBuffer, so you should move it to a variable before the call. Also, when you post code, put 3 ~'s on a line before and after the code so it formats correctly.

  • dave1707dave1707 Mod
    Posts: 7,550

    @oarnone Try making these changes

    function keyboard(key)
        if key == "\n" then
            a=keyboardBuffer()       <---- changed
            hideKeyboard()
        end
    end
    
        -- ***** THIS ISN"T WORKING YET *** 
        --a = keyboardBuffer()    <----  changed, commented code
    
          dothemath(answer)      <-----  changed
    
  • dave1707dave1707 Mod
    edited December 2016 Posts: 7,550

    @oarnone I posted this somewhere on the forum awhile ago, but I couldn't find it, so here it is again. It calculates digits of Pi. Just change the value for digits in the code. I'm not sure what the limit is for digits. Just in case you're interested.

    EDIT: I used this link to create the code below.

    http://www.pi314.net/eng/goutte.php
    
    -- calculate digits of Pi
    
    function setup()
        local digits=1000  -- number of digits of Pi     <---- change digits
        print("Calculating "..digits.." digits of pi.")
        start=os.clock()
        local pi={}
        local tab={}
        local mf=math.floor
        local size=mf(digits/2)
        local size1=mf(size*6.8)
        local sum
        local carry
        local b
        for i=0,size1 do
            tab[i]=20
        end
        for t=1,size do
            carry=0
            for i=size1,0,-1 do
                sum=tab[i]*100+carry
                b=i*2+1
                tab[i]=sum%b
                carry=mf(sum/b)*i
            end       
            tab[0]=sum%100
            table.insert(pi,mf((sum)/100))
        end
        carry=0
        local x
        for z=size,1,-1 do
            x=pi[z]
            x=x+carry
            if x>99 then
                x=x-100
                carry=1
            else
                carry=0
            end
            pi[z]=string.format("%02d",x)
        end
        pi[1]="3.1"
        print(table.concat(pi))
        print()
        print(digits.." digits of Pi in")
        print(os.clock()-start,"seconds")
    end
    
  • Thanks everyone. I will try this out today.

  • Got it working.. Thanks for the help.

Sign In or Register to comment.