Jump to content




tonumber() help


2 replies to this topic

#1 ClassicRockFan

  • Members
  • 56 posts

Posted 09 March 2014 - 03:58 AM

Hey guys, I'm trying to write a program that basically acts as a cash register. But I'm having trouble getting the computer to calculate price. Basically, I want to use tonumber() to allow me to take a number that the user inputs rather than a string. But, I also want to be able to weed out any inputs that aren't numbers. Can anyone help me? Here is the pastebin number(LLPEhFxi) if you want to download it and here is the code for those that don't. Thanks all for your help - CRF (I commented on the line before where the error would occur and I know that this code needs to be cleaned up a little bit before actual use.)
local maxX = 25
local maxY = 21
local total = 0
local storeName = "Classic's General Store"
local CashierID = {"00000"}
local CashierName = {"ClassicRockFan"}
local i = #CashierID
local cashierName = " "
local restart = true
local valid = nil
local p = peripheral.wrap("printer_1")
local m = peripheral.wrap("monitor_0")
local inventoryItems = {"blah1", "blah2"}
local inventoryPrice = {5, 10}
local inventory = textutils.serialize(inventoryItems)
local printInv = true
function login()
while valid == nil do
  if restart == true then
   term.clear()
   term.setCursorPos(1,1)
  end
term.write("Enter your cashier ID: ")
inputID = read()
print("Validating ID...")
sleep(1)
for i = 1, i do
   if inputID == CashierID[i] then
	 cashierName = CashierName[i]
	 valid = true
   else
	 valid = nil
   end
end
if valid == true then
   register()
else
   print("Invalid ID")
   sleep(2)
   restart = true
end
end
end
function PrintCenter(msg)
msgLen = string.len(msg)
screenWidth,_=p.getPageSize()
xCoords = tonumber(math.ceil((screenWidth/2) - (msgLen/2)))
_,termY = p.getCursorPos()
p.setCursorPos(xCoords, termY)
p.write(msg)
end
function register()
  term.clear()
  term.setCursorPos(1,1)
  print(storeName.."Computer.  Cashier: "..cashierName)
  term.setCursorPos(1,3)
  print("[1]  Print Recepit")
  print("[2]  Do Inventory")
  print("[3]  Logout")
  term.setCursorPos(1,7)
  term.write("What would you like to do: ")
  action = read()
  if action == "1" then
   p.newPage()
   p.setPageTitle(storeName.." Receipt")
   p.setCursorPos(1,maxY-1)
   PrintCenter("Your Cashier was "..cashierName)
   p.setCursorPos(1,maxY)
   PrintCenter("Thank You!")
   while true do
   if printInv == true then
	p.setCursorPos(1,1)
	print(inventory)
	printInv = false
   elseif printInv == false then
	print("Enter the Inventory Number of the purchased Items")
	term.write("(Leave blank to print): ")
	purchased = read()
	if #purchased == 0 then
	  printInv = done
	else
	 purchased = tonumber(purchased)
	 term.write("Enter how many were purchased: ")
	 numPurchased = read()
	 --Here is where the issue would be
	 numBought = tonumber(numpurchased)
	 if numBought == nil then
	   print("Invalid Entry")
	   printInv = true
	 else
	  price = inventoryPrice[purchased]
	  itemTotal = price*numBought
	  total = total+itemTotal
	  m.clear()
	  m.print(total)  
	  printInv = true
	end
   end
   elseif printInv == done then
	 p.endPage()
   end
  end
  elseif action == "2" then
	 print("test2")
  elseif action == "3" then
	  print("test3")
  else
	print("Invalid Input")
  end
end
--login()
register()

Edited by ClassicRockFan, 09 March 2014 - 04:04 AM.


#2 MostwantedRBX

  • Members
  • 29 posts
  • LocationWell. I suppose between here and there.

Posted 09 March 2014 - 04:53 AM

You forgot to camelCase numPurchased in numBought = tonumber(numpurchased) . Them darn camelCases.... I personally capitalize the start of each word, makes it more readable for me, I suppose.

#3 valithor

  • Members
  • 1,053 posts

Posted 09 March 2014 - 10:57 PM

For tonumber() if a string is inserted then it returns a nil value so to check for nils an example would be

string = "this is a string"
number = tonumber(string)
if number == nil then
  number = 0
end

You could do whatever you want to do in the if statement that just sets number to 0 if a string is inserted.

Btw read() always returns a string if you want it to return a number then you can use

variable = tonumber(read())

Edited by valithor, 09 March 2014 - 11:00 PM.






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users