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

Error

Started by crazylady77, 07 August 2012 - 03:20 AM
crazylady77 #1
Posted 07 August 2012 - 05:20 AM
I am trying to build a banking system that takes withdrawled money for a balance file located /disk/bal I keep getting a error

Heres the code

write ("Please enter the amount of money you would like to withdraw: ")
local money = tonumber(read())
for dont=1. money do
tFile = fs.open('/disk/bal', 'r')
current = tFile.read()
tFile.close()
hFile = fs.open('/disk/bal', 'w')
hFile.write(tonumber(current) - tonumber(read()))
hFile.close()
end

Error is

withdraw:5: attempt to call nil

Please someone help me fix this. Remember though it has to store and read the balance amount from a floppy disk.
Zalerinian #2
Posted 07 August 2012 - 06:21 AM
I am trying to build a banking system that takes withdrawled money for a balance file located /disk/bal I keep getting a error

Heres the code

write ("Please enter the amount of money you would like to withdraw: ")
local money = tonumber(read())
for dont=1. money do
tFile = fs.open('/disk/bal', 'r')
current = tFile.read()
tFile.close()
hFile = fs.open('/disk/bal', 'w')
hFile.write(tonumber(current) - tonumber(read()))
hFile.close()
end

Error is

withdraw:5: attempt to call nil

Please someone help me fix this. Remember though it has to store and read the balance amount from a floppy disk.

try replacing


current = tFile.read()

with

current = tFile:read()

that should fix it.
Lyqyd #3
Posted 07 August 2012 - 06:32 AM
I am trying to build a banking system that takes withdrawled money for a balance file located /disk/bal I keep getting a error

Heres the code

write ("Please enter the amount of money you would like to withdraw: ")
local money = tonumber(read())
for dont=1. money do
tFile = fs.open('/disk/bal', 'r')
current = tFile.read()
tFile.close()
hFile = fs.open('/disk/bal', 'w')
hFile.write(tonumber(current) - tonumber(read()))
hFile.close()
end

Error is

withdraw:5: attempt to call nil

Please someone help me fix this. Remember though it has to store and read the balance amount from a floppy disk.

try replacing


current = tFile.read()

with

current = tFile:read()

that should fix it.

No, that won't fix it, since he's using the FS API and not the IO API.

OP, does the file actually exist at /disk/bal? fs.open() will throw an error (which results in the first value returned being nil) if the file doesn't exist when you open it in read mode. Also, I don't think that that program will do what you think it will.
odd_kid #4
Posted 07 August 2012 - 07:02 AM
Have it install the file auto with a floppy installer. If not, just create the new file. It looks like there isnt a file located thats why.
crazylady77 #5
Posted 07 August 2012 - 07:43 AM
Can someone help me make that script run for the io api rather than the fs api
Luanub #6
Posted 07 August 2012 - 08:42 AM
io is fairly close to fs. But I prefer io.

Some things I would do:

Check for file and create if not present

if not fs.exists("/disk/bal") then
local file = io.open("/disk/bal", "w")
file:write()
file:close()
end

Caputre contents of file in a var:

local tFile = io.open("/disk/bal", "r")
current = tFile:read()
file:close()