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

Using math on inputs causing problems

Started by asparagus, 20 November 2015 - 07:14 PM
asparagus #1
Posted 20 November 2015 - 08:14 PM
I'm reading an input from the user and comparing it against a saved number, all of it works flawlessly for having quickly put it together. However, it seems that the input is dividing it self by 100 without any math being acted upon it. It always returns that the input is greater than the overall number if you do anything less than 100/total number. So, if the total was 500 and I inputted 99 it would say that it was wrong.
P.S: messy code cause it's wip.

playername="user"
zero = "0"

if fs.exists(playername) then
local playeraccount = fs.open(playername, "r")
playerbalance = playeraccount.readLine(1)
playeraccount.close()
term.setCursorPos(1,2)
dbalance = textutils.serialize(playerbalance)
displayBalance = playerbalance
protoBalance = playerbalance
term.setCursorPos(2,7)
print(displayBalance)
term.setCursorPos(1,2)
local input = read()
math.ceil(input)
term.setTextColour(colours.white)
if input > protoBalance then
print("Too Much/Not A Number!")
print(input)
sleep(3)
shell.run("startup")
elseif (input>zero) and (input <= protoBalance) then
print("Withdrawing amount...")
protoBalance = protoBalance - input
local playeraccount = fs.open(playername, "w")
playeraccount.writeLine(tostring(protoBalance))
playeraccount.writeLine(protoBalance)
playeraccount.close()
print("Done.")
sleep(5)
os.reboot()
else
shell.run("startup")
term.setCursorPos(1,1)
shell.run("startup")
end
end
KingofGamesYami #2
Posted 20 November 2015 - 10:14 PM
1) h.readLine does not take an argument (eg. it won't allow you to read a specific line)
2) h.readLine returns a string, not a number. Converting it to a string is as simple as using the tonumber function


local playerbalance = tonumber( playeraccount.readLine() ) or 0
asparagus #3
Posted 20 November 2015 - 11:22 PM
-snip-
Ahhh! Fantastic. I completely knew that yet for some reason I guess I drew a blank. Thank you.