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

Help with code!

Started by Kadix, 21 March 2013 - 11:16 AM
Kadix #1
Posted 21 March 2013 - 12:16 PM
Title: Help with code!

Hi guys, I did this code a few days ago, and I'm having a problem, I can only use it once, then it stops working!
Here is the code:

--[[
   Calculadora Completa
  
]]--
--Funcoes auxiliares
function adicionar()
   limpar_tela()
   print("Voce escolheu adicionar.")
   print()
   term.write("Digite o primeiro numero: ")
   n1 = io.read()
   term.write("Digite o segundo numero: ")
   n2 = io.read()
   resultado = n1 + n2
   print("O resultado eh: "..resultado)
end
function subtrair()
   limpar_tela()
   print("Voce escolheu subtrair.")
   print()
   term.write("Digite o primeiro numero: ")
   n1 = io.read()
   term.write("Digite o segundo numero: ")
   n2 = io.read()
   resultado = n1 - n2
   print("O resultado eh: "..resultado)
end
function dividir()
   limpar_tela()
   print("Voce escolheu dividir.")
   print()
   term.write("Digite o numero a ser dividido: ")
   n1 = io.read()
   term.write("Digite o divisor: ")
   n2 = io.read()
   resultado = n1 / n2
   print("O resultado eh: "..resultado)
end
function multiplicar()
   limpar_tela()
   print("Voce escolheu multiplicar.")
   print()
   term.write("Digite o primeiro numero: ")
   n1 = io.read()
   term.write("Digite o segundo numero: ")
   n2 = io.read()
   resultado = n1 * n2
   print("O resultado eh: "..resultado)
end
function raiz()
   limpar_tela()
   print("Voce escolheu tirar raiz.")
   print()
   term.write("Digite o numero para tirar a raiz: ")
   n = io.read()
   resultado = math.sqrt(n)
   print("O resultado eh: "..resultado)
end
function eleva()
   limpar_tela()
   print("Voce escolheu elevar um numero.")
   print()
   term.write("Escolha o numero a ser elevado: ")
   ne = io.read()
   term.write("Escolha o numero para elevar: ")
   nr = io.read()
   resultado = ne ^ nr
   print("O resultado eh: "..resultado)
end
function limpar_tela()
   term.clear()
   term.setCursorPos(1,1)
end
function menu()
   print("Bem-Vindo a Calculadora Completa!!")
   print()
   print("Escolha a opcao: ")
   print()
   print("[A]dicionar")
   print("[S]ubtrair")
   print("[D]ividir")
   print("[M]ultiplicar")
   print("[T]irar Raiz")
   print("[E]levar a um numero")
   print("[F]echar")
   term.write("=> ")
end
while escolha ~= "f" do
  limpar_tela()
  menu()
  escolha = ""
  escolha = io.read()
 
    if escolha == "a" then
	 adicionar()
    elseif escolha == "s" then
	 subtrair()
    elseif escolha == "d" then
	 dividir()
    elseif escolha == "m" then
	 multiplicar()
    elseif escolha == "t" then
	 raiz()
    elseif escolha == "e" then
	 eleva()
    elseif escolha == "f" then
	 print("Obrigado por utilizar a Calculadora")
	
    else
	 print("Nao sei que operacao eh essa.")
  end
end

It's in portuguese, sorry :P/>
PixelToast #2
Posted 22 March 2013 - 03:18 AM
what is the problem / error
Kingdaro #3
Posted 22 March 2013 - 03:28 AM
You have interesting logic in this script. Not bad, but interesting.

Also, I just tested, and it seems to work fine for me, the only problem being that it doesn't show my answer and returns directly to the main menu. You should pull a key event after showing the answer, then go ahead and tell the user to press any key to continue.


function eleva()
   limpar_tela()
   print("Voce escolheu elevar um numero.")
   print()
   term.write("Escolha o numero a ser elevado: ")
   ne = io.read()
   term.write("Escolha o numero para elevar: ")
   nr = io.read()
   resultado = ne ^ nr
   print("O resultado eh: "..resultado)
   print("Prima qualquer tecla para continuar.")
   os.pullEvent('key')
end

I sort of translated the "press any key to continue" line, feel free to correct it if google is unreliable. :lol:/>
GravityScore #4
Posted 22 March 2013 - 03:29 AM
First off, ComputerCraft defines its own read function, so just call read(), instead of io.read().

Secondly, the function limpar_tela is defined below most of the functions that call it. This might be a problem, but I'm not sure if is. Try putting it before all the functions you define at the top.

Thirdly, the reason why it isn't working (I think) is because escolha is global. This means that the variable is available not only within your program, but to every other program that runs after it, including your program if you run it again. So, setting escolha to f, then exiting the program does not actually clear the variable, and the next time you run the program, it still is equal to f, meaning that the while loop does not start again, but instead doesn't run at all, because escolha is f! To fix this, put this at the top of your code, to define escolha as local, meaning that it is only available within your program:

local escolha = ""

When programming anything, you should really define any variables as local when you first use them, so this sort of thing doesn't happen. There is a tutorial sort of on local variables here, near the end of the first post. Also, in this tutorial here, in the section on variables.
Kadix #5
Posted 22 March 2013 - 12:41 PM
Then I need to type in local before setting variables?
Kadix #6
Posted 22 March 2013 - 12:48 PM
Understand now guys, thanks :D/>
Just one more question, when I want the program to stop when I type in "f", how do I do it ?
SuicidalSTDz #7
Posted 22 March 2013 - 01:00 PM
If this program is click-based then all you need to do is this:

while true do
 local event, p1 = os.pullEvent()
 if event == "mouse_click" then
  --Do something with the mouse
 elseif event == "key" and p1 == 33 then --The numeric value for key 'f' is '33'
  --Exit the program via break
 end
end

Side note:
A chunk is also a block, and so local variables can be declared in a chunk outside any explicit block. The scope of such local variables extends until the end of the chunk. This means that any other program/chunk/block cannot use this variable if it is defined as 'local'. Defining something local at the top of the script will allow it to be used throughout the entire script, instead of being limited to that chunk/block. In this case, event and p1 are defined as local variables. They cannot be used outside of the 'while true do' block, so calling them will return nil.