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

[error] Calculator

Started by Finnvos123, 23 March 2013 - 07:14 PM
Finnvos123 #1
Posted 23 March 2013 - 08:14 PM
I've started testing a bit with cc and made a simple calculator program. However, everytime I try to do * it says calculator:18: attempt to perform arithmetic on local 'f' (a nil value)
Here's the code:

function add(x, y)
x = tonumber(x)
y = tonumber(y)
result = x+y
print ("Result: "..result)
end

function sub(p, q)
p = tonumber(p)
q = tonumber(q)
result2 = p-q
print ("Result: "..result2)
end

function abc(f, m)
f = tonumber(f)
m = tonumber(m)
result3 = f*m
print ("Result: "..result3)
end

function cba(k, l )
k = tonumber(k)
l = tonumber(l)
result4 = k/l
print ("Result: "..result4)
end

print "Calculator"

print "+, -, * or /"
input = io.read()

if input == "+" then

io.write "Enter first number: "
a = io.read()
io.write "Enter second number: "
b = io.read()
add(a, b )

elseif input == "-" then

io.write "Enter first number: "
c = io.read()
io.write "Enter second number: "
d = io.read()
sub(c, d)

elseif input == "*" then

io.write "Enter first number: "
e = io.read()
io.write "Enter second number: "
f = io.read()
abc(c,d)

elseif input == "/" then

io.write"Enter first number: "
g = io.read()
io.write "Enter second number: "
h = io.read()
cba(g, h)

else
print "Invalid action"
end

I know it isn't really compact but I'm not that good at coding so I just wanted it to be simple to understand.
(Can't get code tags around it right now but will do later)
theoriginalbit #2
Posted 23 March 2013 - 08:17 PM
we need code or we cannot help… all i can tell you from that error code is that there is an error on line 18. and you are trying to add something.
Finnvos123 #3
Posted 23 March 2013 - 08:21 PM
But I've checked it if it was exactly the same as the other functions and it is, the only difference is the *, perhaps it's not the right symbol?
Ps: sorry, forgot to paste the code in before posting
theoriginalbit #4
Posted 23 March 2013 - 08:31 PM
your problem is on line 56 you have

abc(c,d)
but above there you use the variables e and f

io.write "Enter first number: "
e = io.read()
io.write "Enter second number: "
f = io.read()

I actually got a different error than you…. odd… I got "test:18: attempt to perform arithmetic __mul on nil and nil"
AndreWalia #5
Posted 23 March 2013 - 08:32 PM
on line 53 change

io.write "Enter first number: "
e = io.read()
io.write "Enter second number: "
f = io.read()
abc(c,d)
to

io.write "Enter first number: "
c = io.read()
io.write "Enter second number: "
d = io.read()
abc(c,d)
EDIT: Ninja'd :ph34r:/>
Finnvos123 #6
Posted 23 March 2013 - 08:45 PM
I really didn't expect it to be that simple…..
Thanks for helping so quick!
ps: you've probably got a different error because the las time I've tested it it wasn't in cc