Jump to content

[Help] Substracting


brocky

Recommended Posts

Posted

I have a problem with a code, Its a simple calculator to substract numbers, but the problem is that it doesn't give the required/correct answer.

  
    editbox1 = guiCreateEdit(22, 41, 135, 24, "", false, window1) 
   editbox2 = guiCreateEdit(21, 80, 136, 24, "", false, window1) 
  buttonSub = guiCreateButton(148, 250, 110, 26, "Subtraction", false, window1) 
  
function substraction() 
     if buttonSub == source then 
           num1 = guiGetText ( editbox1 )  
           num2 = guiGetText ( editbox1 )  
  local   subtheAnswer = num1 - num2 
                  
       outputChatBox("Subtraction answer: "..subtheAnswer) 
              end 
     end 
addEventHandler("onClientGUIClick", buttonSub, substraction, false)   
  
  

Hope you understood

Posted
num1 = guiGetText ( editbox1 )  
num2 = guiGetText ( editbox1 ) 

Both num1 and num2 come from the same edit box, which means you're just doing editbox1 value - editbox1 value (i.e. a-a)

Change line 9 to

num2 = guiGetText ( editbox2 ) --editbox2 not editbox1 

Posted

Use tonumber

local num1 = tonumber(guiGetText ( editbox1 )) 
local num2 = tonumber(guiGetText ( editbox2 )) 
if num1 and num2 then 
    local subtheAnswer = num1 - num2 
    outputChatBox("Subtraction answer: "..subtheAnswer) 
else 
    outputChatBox("Invalid input") 
end 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...