Posted 16 February 2015 - 01:42 PM
Hi, First of all thank you for taking the time to read this.
I need some help, I have tried for the past several hours to sort this problem out myself and haven't been able too.
What I want to do is convert a string given from another file using the "h = fs.open()" function and then convert it to a integer so i can use it for math, but i have tried several ways to do this and have came up short as i get the error message "addFunds:40: attempt to perform arithmetic __add on string and number" but then when i put "old = tonumber(oldAmount)" it then returns the same error but says "nil and number"
as I am on a server with a couple of my friends and i am creating a banking system for funds
all help will be highly appreciated as i am stuck and confused on what to do.. thanks
I have included the code I am using and also have sectioned it off so that it is easily found.
I need some help, I have tried for the past several hours to sort this problem out myself and haven't been able too.
What I want to do is convert a string given from another file using the "h = fs.open()" function and then convert it to a integer so i can use it for math, but i have tried several ways to do this and have came up short as i get the error message "addFunds:40: attempt to perform arithmetic __add on string and number" but then when i put "old = tonumber(oldAmount)" it then returns the same error but says "nil and number"
as I am on a server with a couple of my friends and i am creating a banking system for funds
all help will be highly appreciated as i am stuck and confused on what to do.. thanks
I have included the code I am using and also have sectioned it off so that it is easily found.
---------------------------------------------------------------
--Paullies Bank Code, Adding Funds to Bank Account
---------------------------------------------------------------
term.clear()
local nTime = os.time()
local nFile = "/disk/funds"
local nUser = "/disk/user"
--Getting User Details
if fs.exists(nUser) == true then
h = fs.open(nUser, "r")
text = h.readAll()
h.close()
end
--Showing roughly what time the transaction is made
term.setCursorPos(2,2)
print(textutils.formatTime(nTime,false))
term.setCursorPos(5,9)
print("Please Enter the Amount you wish to add to")
term.setCursorPos(17,10)
print(text.."'s Account.")
term.setCursorPos(15,12)
write(": £")
local amount = tonumber(io.read())
----------------------------------------------------------------
--This is the code which isnt working
--------------------------------------------------------------
if fs.exists(nFile) == true then
h = fs.open(nFile, "r")
local oldAmount = h.readAll()
h.close()
newAmount = (oldAmount + amount)
h = fs.open(nFile, "w")
h.write(newAmount)
h.close()
end