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

Calculator

Started by Hydrotronics, 26 November 2015 - 05:01 PM
Hydrotronics #1
Posted 26 November 2015 - 06:01 PM
so, i'm making a calculator. Simple. I think I have the symbol selection working and i have the input working.
my problem is when you hit the + sign, it just stops and returns you to basic input.
when you hit the - sign, it proceeds to the second step, then after telling it to calculate, it also, returns you to basic input
when you hit the / sign, it adds, then loops to the second number area again then crashes, same with *.

Could someone explain this? Would be great!

Code:


sin = 0
function menu(text, id)
  if sin == id then
	write("[ "..text.."  ]")
  else
	write("  "..text.."   ")
  end
  print""
end
term.clear()
term.setCursorPos(1,1)
print"Enter First Number"
write""
input = tonumber(read())
term.clear()
term.setCursorPos(1,1)
print"Select A Symbol"
while true do
term.setCursorPos(1,4)
menu("+", 0)
menu("-", 1)
menu("/", 2)
menu("*", 3)

local event, key = os.pullEvent("key")
if key == 208 then
  if sin > 2 then
	sin = sin - 2
  else
	sin = sin + 1
  end
elseif key == 200 then
  if sin < 1 then
	sin = sin + 2
  else
	sin = sin - 1
  end
elseif key == 28 then
  if sin == 0 then
	break
  end
	term.clear()
	term.setCursorPos(1,1)
	print"Enter Second Number"
	write""
	input2 = tonumber(read())
	output = input2 + input
	term.clear()
	term.setCursorPos(1,1)
	print(output.."		Press Enter To Continue")
	sleep(1)
	if key == 28 then
	  term.clear()
	  term.setCursorPos(1,1)
	end
  if sin == 1 then
	break
  end
	term.clear()
	term.setCursorPos(1,1)
	print"Enter Second Number"
	write""
	input2 = tonumber(read())
	output = input2 - input
	term.clear()
	term.setCursorPos(1,1)
	print(output.."		Press Enter To Continue")
	sleep(1)
	if key == 28 then
	  term.clear()
	  term.setCursorPos(1,1)
	end
  if sin == 2 then
	break
  end
	term.clear()
	term.setCursorPos(1,1)
	print"Enter Second Number"
	write""
	input2 = tonumber(read())
	output = input2 / input
	term.clear()
	term.setCursorPos(1,1)
	print(output.."		Press Enter To Continue")
	sleep(1)
	if key == 28 then
	  term.clear()
	  term.setCursorPos(1,1)
	end
  if sin == 3 then
	break
  end
	term.clear()
	term.setCursorPos(1,1)
	print"Enter Second Number"
	write""
	input2 = tonumber(read())
	output = input2 * input
	term.clear()
	term.setCursorPos(1,1)
	print(output.."		Press Enter To Continue")
	sleep(1)
	if key == 28 then
	  term.clear()
	  term.setCursorPos(1,1)
	end
  end
end

Thanks in advance!
Edited on 26 November 2015 - 05:08 PM
RoD #2
Posted 26 November 2015 - 11:20 PM
This code is so big. You should probably want to:
1-Make a function to gather the NUMBER input.
2-Ask for 2 numbers AND then the operation.
That will make it easier for you to understand your code. If you need help doing that just say, but i would rather like to leave it to you so you can learn it better.
valithor #3
Posted 26 November 2015 - 11:42 PM
So let's walk through what exactly your code is doing.

Part I didn't see any problems with.
Spoiler

sin = 0
function menu(text, id)
  if sin == id then
		write("[ "..text.."  ]")
  else
		write("  "..text.."   ")
  end
  print""
end
term.clear()
term.setCursorPos(1,1)
print"Enter First Number"
write""
input = tonumber(read())
term.clear()
term.setCursorPos(1,1)
print"Select A Symbol"
while true do
term.setCursorPos(1,4)
menu("+", 0)
menu("-", 1)
menu("/", 2)
menu("*", 3)

This part is good, from what I can tell just getting input and drawing the place where you can choose onto the screen.


local event, key = os.pullEvent("key")

Grabbing key event


if key == 208 then
  if sin > 2 then
		sin = sin - 2
  else
		sin = sin + 1
  end
elseif key == 200 then
  if sin < 1 then
		sin = sin + 2
  else
		sin = sin - 1
  end

Checking for left/right arrows in order to change selection.

addition
Spoiler

elseif key == 28 then --# looking for enter key
  if sin == 0 then --# if addition is selected run the following.
		break --# tells the program to exit the loop right there.  Does not do anything beneath this point, so if addition is selected nothing happens.
  end
		term.clear()
		term.setCursorPos(1,1)
		print"Enter Second Number"
		write"" --# write nothing?  Cursor pos doesn't change, nothing is written, what is accomplished?
		input2 = tonumber(read())
		output = input2 + input
		term.clear()
		term.setCursorPos(1,1)
		print(output.."		 Press Enter To Continue")
		sleep(1)
		if key == 28 then --# You never changed the value of key since you last checked it.  Why would it be different?
		  term.clear()
		  term.setCursorPos(1,1)
		end

Your problem for the other signs are basically the same. Just wrap all of the code related to each sign in a if statement to check for the sign, and move the break's to the end of each if statement. The things i pointed out were just from a skim of the code, I very well might have missed something else, so make the changes and if you are still experiencing problems come back and we can try to help more.
Edited on 26 November 2015 - 10:42 PM
Hydrotronics #4
Posted 29 November 2015 - 07:56 PM
have completed calculator now!! thx :)/> if you want, here is the pastebin.
Thank you!