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

Converting string to number to use for math

Started by Paullie, 16 February 2015 - 12:42 PM
Paullie #1
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.


---------------------------------------------------------------
--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
Lignum #2
Posted 16 February 2015 - 02:58 PM
tonumber will give you nil if your string does not contain a valid number. Since you're using readAll(), I'm going to guess and say that you're also passing the newline character into tonumber, which is invalid. So, try using readLine() instead. If it still doesn't work, it would help if you posted the file you're trying to read.
Paullie #3
Posted 16 February 2015 - 03:04 PM
Thanks ill give it a go, and the file i am trying to use is basically a number to state the amount of money it has, like for instance "100", which means £100, but when i print it ill do

function printBal()
  h = fs.open(nFile, "r")
  bal = h.readAll()
  print(bal)
end
Paullie #4
Posted 16 February 2015 - 03:31 PM
Thanks Lignum, i've got it all sorted now :)/>
Lignum #5
Posted 16 February 2015 - 03:34 PM
You're welcome :)/>!