Jump to content




Creation of an API error


  • You cannot reply to this topic
7 replies to this topic

#1 Alerith

  • Members
  • 36 posts

Posted 20 February 2013 - 03:49 PM

Hello Pro's, i'm practicing lua programming and I decided to make a calculator (a text calculator) with an api created by me. This is the API:

--Definiciones de funcion calculadora
--Suma, resta, multiplicacion, division
--Potencia, raiz cuadrada
function add(x, y)
  res = x + y
end
function sub(x, y)
  res = x - y
end
function mult(x, y)
  res = x * y
end
function div(x, y)
  res = x / y
end
function pot(x, y)
  res = x ^ y
end
function rsqr(x)
  local r
  local t = 0
  r = x
  while t ~= r do
	t = r
	r = (x/r + r)/2
  end
  res = r
end

And this is de main program:
os.loadAPI("calc")
local opc
print("Pick a number")
print("")
print("1. Add")
print("2. Subtract")
print("3. Multiply")
print("4. Divide")
print("5. Square of a number")
print("6. Root Square")
opc = io.read()
if opc == "1" then
  print("First number:")
  x = io.read()
  print("Second number:")
  y = io.read()
  calc.suma(x, y)
  print("")
  print("Result: "..res)
  x = nil
  y = nil
elseif opc == "2" then
  print("First number:")
  x = io.read()
  print("Second number:")
  y = io.read()
  calc.resta(x, y)
  print("")
  print("Result: "..res)
  x = nil
  y = nil
elseif opc == "3" then
  print("First number:")
  x = io.read()
  print("Second number:")
  y = io.read()
  calc.mult(x, y)
  print("")
  print("Result: "..res)
  x = nil
  y = nil
elseif opc == "4" then
  print("First number:")
  x = io.read()
  print("Second number:")
  y = io.read()
  calc.div(x, y)
  print("")
  print("Result: "..res)
  x = nil
  y = nil
elseif opc == "5" then
  print("First number (base):")
  x = io.read()
  print("Second number:")
  y = io.read()
  calc.pot(x, y)
  print("")
  print("Result: "..res)
  x = nil
  y = nil
elseif opc == "6" then
  print("Number:")
  x = io.read()
  calc.sqr(x)
  print("")
  print("Result: "..res)
  x = nil
end

The problem is that the program don't add or deduct or nothing. It gives the same result always: "12". The "12" is because I had the functions in the api inside the main program without an api, and I was trying out the root square with 144.

Thanks =)

#2 tesla1889

  • Members
  • 351 posts
  • LocationSt. Petersburg

Posted 20 February 2013 - 04:15 PM

you could always try skipping the os.loadAPI part and just declare the functions in a table called calc

#3 immibis

    Lua God

  • Members
  • 1,033 posts
  • LocationWellington, New Zealand

Posted 20 February 2013 - 04:45 PM

The result gets saved to calc.res even though you just called it res. That happens for the same reason the functions are calc.add/calc.sub/etc when you called them add/sub/etc

#4 Alerith

  • Members
  • 36 posts

Posted 20 February 2013 - 04:49 PM

View Postimmibis, on 20 February 2013 - 04:45 PM, said:

The result gets saved to calc.res even though you just called it res. That happens for the same reason the functions are calc.add/calc.sub/etc when you called them add/sub/etc

So I have to put:
print(""..calc.res)

instead of
print(""..res)

Is that way?

#5 LBPHacker

  • Members
  • 766 posts
  • LocationBudapest, Hungary

Posted 20 February 2013 - 06:08 PM

Yup, because res is part of the calc API.

#6 Alerith

  • Members
  • 36 posts

Posted 20 February 2013 - 06:43 PM

Thanks to the response but when I replace that and run the program and tryied the option "add", I get other error:

Calculadora:21: attempt to concatenate string and nil

That makes reference to this line of code:
print("Result: "..calc.res)

=S

#7 ChunLing

  • Members
  • 2,027 posts

Posted 20 February 2013 - 08:07 PM

rewrite your API functions to return a value rather than set a value. This is much better style (mostly because it avoids the problem you're having).

#8 Alerith

  • Members
  • 36 posts

Posted 20 February 2013 - 10:01 PM

View PostChunLing, on 20 February 2013 - 08:07 PM, said:

rewrite your API functions to return a value rather than set a value. This is much better style (mostly because it avoids the problem you're having).

Yeah is working! Thanks to you and to the other people too :)





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users