Jump to content




Error


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

#1 crazylady77

  • New Members
  • 8 posts

Posted 07 August 2012 - 04: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.

#2 Zalerinian

  • New Members
  • 68 posts
  • LocationThe Internet

Posted 07 August 2012 - 05:21 AM

View Postcrazylady77, on 07 August 2012 - 04:20 AM, said:

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.

#3 Lyqyd

    Lua Liquidator

  • Moderators
  • 8,465 posts

Posted 07 August 2012 - 05:32 AM

View PostZalerinian, on 07 August 2012 - 05:21 AM, said:

View Postcrazylady77, on 07 August 2012 - 04:20 AM, said:

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.

#4 odd_kid

  • New Members
  • 35 posts
  • LocationProfessional Bug Finder, In San Antonio

Posted 07 August 2012 - 06: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.

#5 crazylady77

  • New Members
  • 8 posts

Posted 07 August 2012 - 06:43 AM

Can someone help me make that script run for the io api rather than the fs api

#6 Luanub

    Lua Nub

  • Members
  • 1,135 posts
  • LocationPortland OR

Posted 07 August 2012 - 07: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()






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users