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

Help please! my own bank

Started by Radget Law, 07 July 2015 - 01:54 AM
Radget Law #1
Posted 07 July 2015 - 03:54 AM
ok i will need some help from you guys, and its actually quite embarassing because a fewyears back i used to be the one helping people. But my memory of LUA has faded and please do not be like "Well the answers obvious!, blah blah blah" because i have tried many different commands and different syntax to make this work, it is not even close to finished as a project but im hoping it to be a credit card, you enter a pin, then you either add, subtract, or just check your money. i even tried making them go into their own programs using shell.run() i still have many intentions for this code once i can get this far. so anyways here is my testing code i am working on:
AB = 100
write("Enter PIN: ")
if read("*")==0741 then
textutils.slowPrint("Withdraw, Deposit, or just checking?")

if read() == "withdraw" then
function withdraw()
textutils.slowPrint("How much will you be taking out?")
d = read("$")
num = tonumber(d)
if num~=nil then
if num < AB then
AB = AB - num
textutils.slowPrint("New Balance: "..AB)
end
if num > AB then
textutils.slowPrint("Not enough money in your account…")
end
end
end
if num==nil then
textutils.slowPrint(d.." is not a number")
end
end
end
if read() == "deposit" then
function deposit()
textutils.slowPrint("How much money did you make?")
d = read("$")
num = tonumber(d)
if num~=nil then
AB = AB + num
textutils.slowPrint("New Balance: "..AB)
end
end
else
print("Wrong PIN")
end
Grim Reaper #2
Posted 07 July 2015 - 06:36 AM
What's the error/problem you're getting?

EDIT: From what I've deduced from your code (I cleaned up your code a bit),
Spoiler

AB = 100
write("Enter PIN: ")

if read("*" )== 0741 then
	textutils.slowPrint("Withdraw, Deposit, or just checking?")

	if read() == "withdraw" then
		function withdraw()
			textutils.slowPrint("How much will you be taking out?")
			d = read("$")
			num = tonumber(d)

			if num~=nil then
				if num < AB then
					AB = AB - num
					textutils.slowPrint("New Balance: "..AB)
				end

				if num > AB then
					textutils.slowPrint("Not enough money in your account...")
				end
			end
		end

		if num==nil then
			textutils.slowPrint(d.." is not a number")
		end
	end
end

if read() == "deposit" then
	function deposit()
		textutils.slowPrint("How much money did you make?")
		d = read("$")
		num = tonumber(d)

		if num~=nil then
			AB = AB + num
			textutils.slowPrint("New Balance: "..AB)
		end
	end
else
	print("Wrong PIN")
end
you're not actually calling either of those functions when the user enters their choice; you're just declaring them.

In the following program, I've simply taken your code and declared the functions before your main if statements, then I've called them when the user chooses them.
Spoiler

AB = 100
write("Enter PIN: ")

if read("*" )== 0741 then
	textutils.slowPrint("Withdraw, Deposit, or just checking?")

	if read() == "withdraw" then
		local num -- Declare the variable so we can access outside the function.

		function withdraw()
			textutils.slowPrint("How much will you be taking out?")
			d = read("$")
			num = tonumber(d)

			if num~=nil then
				if num < AB then
					AB = AB - num
					textutils.slowPrint("New Balance: "..AB)
				end

				if num > AB then
					textutils.slowPrint("Not enough money in your account...")
				end
			end
		end

		withdraw() -- Call the function.

		if num==nil then
			textutils.slowPrint(d.." is not a number")
		end
	end
end

if read() == "deposit" then
	function deposit()
		textutils.slowPrint("How much money did you make?")
		d = read("$")
		num = tonumber(d)

		if num~=nil then
			AB = AB + num
			textutils.slowPrint("New Balance: "..AB)
		end
	end

	deposit() -- Call the function.
else
	print("Wrong PIN")
end
Edited on 07 July 2015 - 04:44 AM
flaghacker #3
Posted 11 July 2015 - 02:25 PM

if read("*")==0741 then
should be

if read("*")=="0741" then

Read returns a string, and you're comparing it with a number. Numbers can't start with a 0 anyway, it will be discarded.