This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
Alerith's profile picture

Creation of an API error

Started by Alerith, 20 February 2013 - 02:49 PM
Alerith #1
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 =)
tesla1889 #2
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
immibis #3
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
Alerith #4
Posted 20 February 2013 - 04:49 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

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

instead of
print(""..res)

Is that way?
LBPHacker #5
Posted 20 February 2013 - 06:08 PM
Yup, because res is part of the calc API.
Alerith #6
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
ChunLing #7
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).
Alerith #8
Posted 20 February 2013 - 10:01 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).

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