none
Small basic - Making a calculator with specific rules. RRS feed

  • Question

  • hey so im making a calculator, but parts aren't working and i cant figure some other things out, so please could you help me fix it and make the code as original as possible. so the rules are the it cant calculate any number over 999 whether its the answer or in the equation, this should be reported as an error. for example 990 + 9 = 999 which is fine, but 990 + 10 = error is what should happen, also something like 1000+1= error should be there as well. also could you make two different codes for this part, such as one says the error when you enter  a value over 999 and the other is when the answer is over 999. next rule is it only has to be able to subtract and add and a clear button that clears all the data from the last equation. And please note that it has to say error when an equation is over 999 or under 0. here is my code and please if you could explain everything about it this would help so much. 

    GraphicsWindow.Title = "Calculator"
    GraphicsWindow.Show()
    GraphicsWindow.Height = "200"
    GraphicsWindow.Width = "265"

    GraphicsWindow.DrawBoundText(1,1,100,"Answer: ")
    Answer = Controls.AddTextBox(100,1) 

    Minus = Controls.AddButton("  -  ",120,90)
    Plus = Controls.AddButton(" +  ",120,60)
    Equals = Controls.AddButton("     =     ",95,120)
    Zero = Controls.AddButton(" 0 ", 30,120)
    Point = Controls.AddButton(" .  ", 62,120)
    Clear = Controls.AddButton(" C  ", 120,30)

    For i = 1 to 9
     x = 30*(1+Math.Remainder(i-1,3))
     y = 30*(1+Math.Floor((i-1)/3))
     button[i] = Controls.AddButton(i,x,y)
     buttonvalue[i] = i
    EndFor

    buttonvalue[1] = "1"
    buttonvalue[2] = "2"
    buttonvalue[3] = "3"
    buttonvalue[4] = "4"
    buttonvalue[5] = "5"
    buttonvalue[6] = "6"
    buttonvalue[7] = "7"
    buttonvalue[8] = "8"
    buttonvalue[9] = "9"
    buttonvalue[0] = "0"
    buttonValue[10] = "="
    buttonValue[11] = "+"
    buttonValue[12] = "-"

     Controls.ButtonClicked = OnButtonClicked

    valueCurrent = ""
     operator = ""

    Sub OnButtonClicked
       buttonClicked = Controls.LastClickedButton
       For i = 1 To Array.GetItemCount(button)
         If (button[i] = buttonClicked) Then
           valueCurrent = Text.Append(valueCurrent,buttonValue[i])
           Controls.SetTextBoxText(Answer,valueCurrent)
         EndIf
       EndFor
       If buttonClicked = Plus Then
         value1 = valueCurrent
         valueCurrent = ""
         operator = "+"
         Controls.SetTextBoxText(Answer,valueCurrent)
       ElseIf buttonClicked = Minus Then
         value1 = valueCurrent
         valueCurrent = ""
         operator = "-"
         Controls.SetTextBoxText(Answer,valueCurrent)
       ElseIf buttonClicked = Equals Then
         value2 = valueCurrent
         If (operator = "+") Then
           valueCurrent = value1+value2
         ElseIf (operator = "-") Then
           valueCurrent = value1-value2
         EndIf
         Controls.SetTextBoxText(Answer,valueCurrent)
       EndIf 
    EndSub

    thank you guys so much for helping

    Sunday, February 9, 2014 7:49 PM

Answers

  • Hey unbitible, nice calculator.

    To do this I chose to create an error check subroutine that checks the variable "currentValue" and if an error occurs return this result in a message box.

    I just called this sub at any point in the program where I wanted to check an error.

    Here's the full sample: RBM261

    Here's the sub:

    Sub CheckValueCurrent
      If valueCurrent < 0 Or valueCurrent > 999 Then
        valueCurrent = ""
        Controls.SetTextBoxText(Answer,valueCurrent)
        GraphicsWindow.ShowMessage("An error has occurred. valueCurrent > 999", "Check valueCurrent")
      EndIf  
    EndSub

    Monday, February 10, 2014 5:29 PM
    Moderator