A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: Not performing calculation

  1. #1
    Junior Member
    Join Date
    Mar 2012
    Posts
    5

    Unhappy Not performing calculation

    So i think i have fully coded my calculator but it still doesn't seem to be performing the calculations correctly. I can't see where i am going wrong. If i perform a calculation let's say 6*2 it outputs NaN then i straight away + by let's say 5 it outputs NaN again. And it doesn't make the operators display only once, so if i click on '+' twice it will display twice in the display when it is meant to only display once and in one of my functions i cleared the display field so that it would replace the number's when the next operation is performed but it doesn't seem to be doing that. I have been going over the code several times but i can't seem to identify where the problem is coming from.


    Actionscript Code:
    import flash.events.MouseEvent;

    var btn:Array = new Array();
    for(var i = 0; i < 10; i++) {
                    var myBtn:Btn = new Btn();
                    myBtn.buttonMode = true;
                    myBtn.mouseChildren = false;
                    myBtn.num = Number(i);
                    myBtn.caption.text = String (i);
                    addChild(myBtn);
                    btn.push(myBtn);
                    btn[i].addEventListener(MouseEvent.CLICK,pressNumber);//EVENT TO ADD NUMBERS TO DISPLAY                                      
    }

    //OPERATORS//
    var operate:Array=new Array("","x","-","+");
    var operators:Array = new Array();
    for(var io:int = 0; io < 4; io++) {
                    var opBtn:Btn_operator = new Btn_operator();
                    opBtn.buttonMode = true;
                    opBtn.mouseChildren = false;
                    opBtn.op = Number(operators);
                    addChild(opBtn);
                    operators.push(opBtn);                
                    opBtn.addEventListener(MouseEvent.CLICK, pressOperator);//EVENT TO ADD OPERATORS TO DISPLAY
    }

    //EQUALS
    var equals:Btn_equals = new Btn_equals();
                    equals.buttonMode = true;
                    equals.mouseChildren = false;
                    equals.caption_op.text = String ("=");
                    addChild(equals);

    equals.addEventListener(MouseEvent.CLICK, performEquals);//EVENT TO CALCULATE



    //VARIABLE HANDLE OPERATION NOT BUTTON
    var operates:String;

    //HANDLE FIRST AND SECOND VALUE
    var num1:Number;
    var num2:Number;

    var currentNumber:String ="";
    function pressNumber(e:MouseEvent):void{
                           display_txt.appendText(e.target.num);
                                       currentNumber+=(e.target.num);
                                       trace(e.target.num);
    }

    function setNumber():void{
                    if (isNaN(num1)){
                 num1 = Number(currentNumber);
                 }
                 else
                                                     {
                   num2 = Number(currentNumber);
                 }
                 currentNumber = "";
                 }

    //DISPLAY OPERATORS
    function pressOperator(event:MouseEvent):void{          

                    operates = operate[operators.indexOf(event.target)];
                    display_txt.appendText(operate[operators.indexOf(event.target)]);
                    trace(operate[operators.indexOf(event.target)]);
                    setNumber();
                    //trace(operates);
                    //CHECKING FOR VALUES AND STORING NUMBERS        
                    if(!(isNaN(num1)) || (isNaN(num2))){
                                    performCalculation();
                    }
    }

    function performCalculation():void{

                    switch (operate){
                    case "/":
                                    num1/=num2;
                    break;
                    case "x":
                                    num1*=num2;
                    break;
                    case "-":
                                    num1-=num2;
                    break;
                    case "+":
                                    num1+=num2;
                    break;
                                    default:
                    break;
    }

    }

    function performEquals(e:MouseEvent):void{
                    display_txt.text=String(num1);
                    num2=NaN;
                    performCalculation();
                    answer_txt.text=String(num2);
                                    trace(num2);

                    }

    I have not added the full code as it is too long, i have just added the parts which relate to each other. I am just doing everything entirely through actionscript, nothing is on stage apart from the display and answer text fields. Help would be greatly appreciatted, thank you.

  2. #2
    Senior Member
    Join Date
    Jul 2008
    Posts
    391
    Inside performEquals you set num2 to NaN right before calling performCalculation, which only changes the value of num1, then you set the answer to the value of num2, which is NaN.

  3. #3
    Junior Member
    Join Date
    Mar 2012
    Posts
    5

    Decimals

    I have managed to solve the calculations however i am having a problem with my decimal places. It only lets me input it with the first number so i need it to work with the second number however i am not entirely sure that it calculates as a decimal. When i try 5.2 + 4 it outputs as a whole number instead 56. Here is the code i have:


    Actionscript Code:
    import flash.events.MouseEvent;
     
    var btn:Array = new Array();
    for (var i = 0; i < 10; i++) {
        var myBtn:Btn = new Btn();
        myBtn.y = 15;
        myBtn.x = i * 100 + 15;
        myBtn.width = 48;
        myBtn.height = 48;
        myBtn.buttonMode=true;
        myBtn.mouseChildren=false;
        myBtn.num=Number(i);
        myBtn.caption.text=String(i);
        addChild(myBtn);
        btn.push(myBtn);
        myBtn.addEventListener(MouseEvent.CLICK,pressNumber);
    }
    //EVENT TO ADD NUMBERS TO DISPLAY                                       ;
     
    btn[0].y += 370;
    btn[0].x += 10;

    btn[1].y += 310;
    btn[1].x += -90;

    btn[2].y += 310;
    btn[2].x += -130;

    btn[3].y += 310;
    btn[3].x += -170;

    btn[4].y += 250;
    btn[4].x += -390;

    btn[5].y += 250;
    btn[5].x += -430;

    btn[6].y += 250;
    btn[6].x += -470;

    btn[7].y += 190;
    btn[7].x += -690;

    btn[8].y += 190;
    btn[8].x += -730;

    btn[9].y += 190;
    btn[9].x += -770;

    //OPERATORS//
    //OPERATORS//
    var operatorSelected:Boolean;
    var operate:Array=new Array("","x","-","+");
    var operators:Array = new Array();
    for (var io:int = 0; io < 4; io++) {
        var opBtn:Btn_operator = new Btn_operator();
        opBtn.width = 48;
        opBtn.height = 48;
        opBtn.buttonMode=true;
        opBtn.mouseChildren=false;
        opBtn.op=Number(operators);
        addChild(opBtn);
        operators.push(opBtn);
        opBtn.addEventListener(MouseEvent.CLICK, pressOperator);
    }
    //EVENT TO ADD OPERATORS TO DISPLAY;

    operators[0].y += 383;
    operators[0].x += 198;
    operators[0].caption_op.text = String("/");

    operators[1].y += 323;
    operators[1].x += 198;
    operators[1].caption_op.text = String("*");

    operators[2].y += 263;
    operators[2].x += 198;
    operators[2].caption_op.text = String("-");

    operators[3].y += 203;
    operators[3].x += 198;
    operators[3].caption_op.text = String("+");

    /*operators[4].y += 263;
    operators[4].x += 256;
    operators[4].caption_op.text = String("-/+");*/




    //ADD DOT
    var dot:Btn_dot = new Btn_dot();
        dot.width = 48;
        dot.height = 48;
        dot.buttonMode = true;
        dot.mouseChildren = false;
        dot.caption_op.text = String (".");
        addChild(dot);

    dot.y += 383;
    dot.x += 78;
    dot.addEventListener(MouseEvent.CLICK, addDot);//EVENT TO ADD DOT TO DISPLAY

    //BACKSPACE
    var goBack:Btn_backspace = new Btn_backspace();
        goBack.width = 48;
        goBack.height = 48;
        goBack.buttonMode = true;
        goBack.mouseChildren = false;
        goBack.caption_op.text = String ("<--");
        addChild(goBack);

    goBack.y += 203;
    goBack.x += 256;
    goBack.addEventListener(MouseEvent.CLICK, backSpace);//EVENT TO GO BACK SPACE IN DISPLAY

    //clearAll
    var clearAll:Btn_clear = new Btn_clear();
        clearAll.width = 48;
        clearAll.height = 48;
        clearAll.buttonMode = true;
        clearAll.mouseChildren = false;
        clearAll.caption_op.text = String ("C");
        addChild(clearAll);

    clearAll.y += 323;
    clearAll.x += 256;
    clearAll.addEventListener(MouseEvent.CLICK, clearFields);//EVENT TO GO BACK SPACE IN DISPLAY



    //EQUALS
    var equals:Btn_equals = new Btn_equals();
    equals.width = 48;
    equals.height = 48;
    equals.buttonMode=true;
    equals.mouseChildren=false;
    equals.caption_op.text=String("=");
    addChild(equals);
       
    equals.y += 383;
    equals.x += 138;
    equals.addEventListener(MouseEvent.CLICK, performEquals);
    //EVENT TO CALCULATE


    //VARIABLE HANDLE OPERATION NOT BUTTON
    var operates:String;

    //HANDLE FIRST AND SECOND VALUE
    var num1:Number;
    var num2:Number;

    var num1S:String="";
    var num2S:String="";

    var currentNumber:String="";
    function pressNumber(e:MouseEvent):void {
        display_txt.appendText(e.currentTarget.num);
        if (operatorSelected) {
            num2S+=e.currentTarget.num;
            num2=Number(num2S);
        } else {
            num1S+=e.currentTarget.num;
            num1=Number(num1S);
        }
    }


    //DISPLAY OPERATORS
    function pressOperator(event:MouseEvent):void {
        operatorSelected = true;
        operates=operate[operators.indexOf(event.target)];
        display_txt.appendText(operate[operators.indexOf(event.target)]);
        trace(operate[operators.indexOf(event.target)]);
    }

    //CLEARS DISPLAY AREA
    function clearFields(event:MouseEvent):void{
        display_txt.text = "";
        answer_txt.text = "";
        num1=NaN;
        num2=NaN;
        }

    //ADDS DECIMAL PLACE
    function addDot(event:MouseEvent):void{
        if(event.currentTarget.num == Number(display_txt.text))
        {
        display_txt.text = "0";
        }
        if (display_txt.text.indexOf(".")==-1){
            display_txt.appendText(".");

        }
       
        }
    //BACKSPACE DISPLAY AREA
    function backSpace(e:MouseEvent):void{
        var temp_str:String = display_txt.text;
        display_txt.text = temp_str.substr(0, (temp_str.length-1)); // Get rid of last character in the string, which in this case is the phantom \r or \n character
    }

    var devideZero:Boolean;
    function performCalculation():void {
        operatorSelected = false;
        switch (operates) {
            case "" :
                if(num2 != 0) {
                num1 /= num2;
                }else{
                answer_txt.text = "Cannot devide by zero";
         devideZero = true;
         }
                break;
            case "x" :
                num1*=num2;
                break;
            case "-" :
                num1-=num2;
                break;
            case "+" :
                num1+=num2;
                break;
            default :
                break;
        }
    }
     
    function performEquals(e:MouseEvent):void {
        num1S="";
        num2S="";
         performCalculation();
         answer_txt.text=num1.toPrecision(2);
        trace(num1.toPrecision(2));//traces
        if(!devideZero){
        answer_txt.text=String(num1);
        }
        num2 = NaN;
    }

  4. #4
    Clarance the Clear Boy
    Join Date
    Mar 2012
    Posts
    5
    The calculation sees "5.2" as 52...

    From looking at the code, it appears that the value of num1 & 2 is only changed when number buttons are pressed, and not the decimal point.

    The addDot function only adds the decimal to the display_txt field. It needs to alter the value of num1S or num2S to be factored in.

  5. #5
    Senior Member guardiankitty's Avatar
    Join Date
    Dec 2006
    Location
    Here
    Posts
    215
    Type cast everything.

    umm... I just wrote something like this just a moment ago for a thread that was necroed, check it out here : http://board.flashkit.com/board/showthread.php?t=748932

    In short, if you dont say, flash might think you are trying to add ints or some sort of strange combination of numbers and datatypes, resulting in all sorts of fun.

    I am too lazy to try to get your supplied code to work--- seeing that some of the dependent classes are not attached (...) so, upload the file and I can take a look at it... or look at my cruddy quickly thrown together code from the above link.

    Best of luck!

  6. #6
    Junior Member
    Join Date
    Mar 2012
    Posts
    5
    It is able to perform the calculations now but the problem i have now is the addDot function won't work. I have used two functions where i want to add a zero at the beginning of the dot when the display box is empty, however none of them seem to work. One method i have doesn't insert the zero before the decimal place however the other does add a zero but when i try to perform a calculation, it doesn't recognise the 0 as a decimal. For both the methods it also only adds a decimal for the first number but not the second number, however the first method i have calculates the numbers as a decimal but outputs as a whole number. So 6.2 + 2 = 64. Help would be greatly appreciatted, Thanks. Here is a link to download the calculator:

    https://rapidshare.com/#!download|35...ornew2.fla|468

  7. #7
    Clarance the Clear Boy
    Join Date
    Mar 2012
    Posts
    5

    The Solution:

    Quote Originally Posted by clarance View Post
    The addDot function only adds the decimal to the display_txt field. It needs to alter the value of num1S or num2S to be factored in.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center