Jump to content




Calculator


  • You cannot reply to this topic
3 replies to this topic

#1 Hydrotronics

  • Members
  • 131 posts
  • LocationThe hall of 1000 monkeys and only 1 typewriter

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 by Hydrotronics, 26 November 2015 - 06:08 PM.


#2 RoD

  • Members
  • 313 posts

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.

#3 valithor

  • Members
  • 1,053 posts

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

addition
Spoiler

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 by valithor, 26 November 2015 - 11:42 PM.


#4 Hydrotronics

  • Members
  • 131 posts
  • LocationThe hall of 1000 monkeys and only 1 typewriter

Posted 29 November 2015 - 07:56 PM

have completed calculator now!! thx :) if you want, here is the pastebin.
Thank you!





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users