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)
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)