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

[Error] A.T.M Arithmetic error | Urgent | Please Help!|

Started by Machinecraft-server, 24 September 2012 - 07:28 PM
Machinecraft-server #1
Posted 24 September 2012 - 09:28 PM
Ok, So I am writing an A.T.M machine for our server bank, I have been hitting problems on line 79, and 56. Any help is welcome,

PS, Who ever figures this out is a legend :P/>/>

--Global Vars
version="0.1 Alpha"
--STARTUP CODE FOR DIRECTORY
if(fs.exists("/bank/")) then
print("Bank Has Been Set Up Locally, version "..version.." Doesn't support wire-less storage")
else
fs.makeDir("/bank")
end
sleep(5)
--end
--MONEY DECLORATIONS
--local money="0"
--local account_money=money
--end
--MAIN LOOP
while(true) do
if money~="0" then
--account_money=money
--money="0"
end
term.clear()
term.setCursorPos(1,1)
print("Money Server "..version)
print("")
print("Please note, this is monitored 24/7")
write("Account name: ")
account=read()
if(account) then
if(fs.exists("bank/"..account)) then
local file1 = fs.open("bank/"..account,"r")
readln=file1.readLine()
if(readln=="") then
account_money1="0"
--account_money1=money
end
if(readln) then
account_money1=readln
else
print("No Data To Read!")
end
file1.close()
--print(account.." contains $"..account_money1)
write("Edit? ")
ans=read()
if(ans=="yes") then
if(readln) then
account_money1=readln
end
write("Amount to withdraw: ")
if(account_money-read()>0) then
money=account_money-read()
file1.close()
else
file1.close()
print("Can't withdraw, no money left in account")
sleep(3)
--money=0
end
local file = fs.open("bank/"..account,"w")
--file.clear("bank/"..account)
file.writeLine(money)
file.close()
end
if(ans=="debug") then
term.clear()
term.setCursorPos(1,1)
write("Account name: ")
debuguser=read()
if(debuguser) then
term.clear()
term.setCursorPos(1,1)
write("Amount to add: ")
debugamount=read()
money=account_money+debugamount
end
end
else
term.clear()
term.setCursorPos(1,1)
print("Account does not exist, please contact a member of staff!")
sleep(2)
term.clear()
end
end
end
GopherAtl #2
Posted 24 September 2012 - 09:37 PM
you have to call os.sleep(time), not just sleep(time). You should really post the actual error messages, not just the line numbers, but I assume the errors were "attempt to call nil."
Noodle #3
Posted 24 September 2012 - 09:38 PM
Okay,
Line 4: fs.isDir("bank")

Can you explain this
write("Account name: ")
account=read()
if(account) then
if(fs.exists("bank/"..account)) then
local file1 = fs.open("bank/"..account,"r")
readln=file1.readLine()
if(readln=="") then
account_money1="0"
--account_money1=money
end
if(readln) then
account_money1=readln
else
print("No Data To Read!")
end

write("Amount to withdraw: ")
amt = read()
if (account_money - tonumber(amt)) > 0 then
money=account_money-tonumber(amt)
Fixed line 50 error.

you have to call os.sleep(time), not just sleep(time). You should really post the actual error messages, not just the line numbers, but I assume the errors were "attempt to call nil."
Wrong..
sleep(time) works just as fine.

Account_money1 and account_money..
if(account) then
if(fs.exists("bank/"..account)) then
local file1 = fs.open("bank/"..account,"r")
readln=file1.readLine()
if(readln=="") then
account_money1="0"
--account_money1=money
elseif(readln) then -- ELSEIF
account_money1=readln
else
print("No Data To Read!")
end

Your code is really fuqed up..

Get rid of these file1.close()
Just have readln = file1.readLine()
file1.close()
Readline will stay the same.

if(readln) then
account_money=readln
end
You don't need if you already have the readline amount.

Finished and 100% fixed code:
--Global Vars
version="0.1 Alpha"
--STARTUP CODE FOR DIRECTORY
if(fs.exists("/bank/")) then
print("Bank Has Been Set Up Locally, version "..version.." Doesn't support wire-less storage")
else
fs.makeDir("/bank")
end
sleep(1)
--end
--MONEY DECLORATIONS
--local money="0"
--local account_money=money
--end
--MAIN LOOP
while(true) do
if money~="0" then
--account_money=money
--money="0"
end
term.clear()
term.setCursorPos(1,1)
print("Money Server "..version)
print("")
print("Please note, this is monitored 24/7")
write("Account name: ")
account=read()
if(account) then
if(fs.exists("bank/"..account)) then
local file1 = fs.open("bank/"..account,"r")
readln=file1.readLine()
file1.close()
if(readln=="") then
account_money="0"
--account_money1=money
elseif(readln) then
account_money=readln
else
print("No Data To Read!")
end
--print(account.." contains $"..account_money1)
write("Edit? ")
ans=read()
if(ans=="yes") then
write("Amount to withdraw: ")
amt = read()
if( account_money - tonumber(amt) ) > 0 then
money = account_money - tonumber(amt)
else
print("Can't withdraw, no money left in account")
sleep(3)
--money=0
end
local file = fs.open("bank/"..account,"w")
--file.clear("bank/"..account)
file.writeLine(money)
file.close()
end
if(ans=="debug") then
term.clear()
term.setCursorPos(1,1)
write("Account name: ")
debuguser=read()
if(debuguser) then
term.clear()
term.setCursorPos(1,1)
write("Amount to add: ")
debugamount=read()
money = account_money + debugamount
end
end
else
term.clear()
term.setCursorPos(1,1)
print("Account does not exist, please contact a member of staff!")
sleep(2)
term.clear()
end
end
end
GopherAtl #4
Posted 24 September 2012 - 09:42 PM
really? sleep() is defined in the gobal scope? I had no idea. Well, line 56 is sleep(5), which is one of the places he said he was getting the error. Looking at the code again, guess it would've crashed long before that if sleep wasn't global as well, since he calls it at the top too. My bad.
Noodle #5
Posted 24 September 2012 - 09:52 PM
really? sleep() is defined in the gobal scope? I had no idea. Well, line 56 is sleep(5), which is one of the places he said he was getting the error. Looking at the code again, guess it would've crashed long before that if sleep wasn't global as well, since he calls it at the top too. My bad.
He said..
It was an arithmetic error on line 50, 51, and a few others. I just fixed some of his code..
Remember kids: elseif works too.
Machinecraft-server #6
Posted 24 September 2012 - 09:53 PM
write("Account name: ") –Ask for user input
account=read() –Get that input and throw in into a var
if(account) then –if Account has been changed do these lines
if(fs.exists("bank/"..account)) then –If the bank account exists do the next lines
local file1 = fs.open("bank/"..account,"r") –Load the bank account into the file scope
readln=file1.readLine() –Read the top line
if(readln=="") then –If the file is empty do the next lines
account_money1="0" –Change this var
account_money1=money –Comment, never mind this
end –End If
if(readln) then –If there is something to read,
account_money1=readln –Change this var
else –else
print("No Data To Read!") –Print this
end –end

My head now hurts at reading this mess :P/>/>
@Noodle Do you not need to close the file, as I am reading and writing to it at 2 different points?
Luanub #7
Posted 24 September 2012 - 10:13 PM
You want to close the file handle before moving on to doing anything else. The way noodle has it coded looks correct.
Machinecraft-server #8
Posted 24 September 2012 - 10:18 PM
Thank you very much, My ATM is now working, Now just to work on the delivery of money :P/>/>