Domanda

I am having a problem in my calculator application, I am making a few different types of calculators and one of them is not working correctly. I have absolutely no idea where the error is, so I have added a link to my project below. It is the SecondCalculatorViewController that is having problems. The calculator is meant to display the users equation in the first label and then the output in the second label when calculate is pressed. At the moment all the other operators such as + - / all work but multiplication is not. Which is weird considering I just copied and pasted into each method. If this is confusing right now, please just take a look at the project and maybe it'll make more sense. Thank you.

https://drive.google.com/file/d/0B7uYznqpZCmcRGdIb09pRVBrWk0/edit?usp=sharing

È stato utile?

Soluzione

That is your switch error. You forget the break after each case. Check all your switch and test again.

- (IBAction)CalculateButton:(id)sender {

    if (RunningTotal == 0) {
        RunningTotal = SelectNumber;
    }
    else{
        switch (Method) {
            case 1:
                RunningTotal = RunningTotal + SelectNumber;
                break;
            case 2:
                RunningTotal = RunningTotal - SelectNumber;
            case 3:
                RunningTotal = RunningTotal * SelectNumber;
            case 4:
                RunningTotal = RunningTotal / SelectNumber;
            default:
                break;
        }
    }
    ......
}

Another suggestion is you don't need to create one action for every number button. You could simply link all the number button to one called:DigitButton:. Then in the method you could get the title of the button to know what digit is select by user.(Or by tag property if you want)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top