Ok, so I have been staring at this code for a couple of days and the nearest I can tell, it;s right (though I know something is off).
it's a calculator program (I found this part of it online).
I think it is something with the switch statement because the calculator behaves as if it is never receiving the initial 0 for CurrentOperator. I went so far as to add a setter method for that specific reason in the viewdidload with no results. and with that, I'm stumped.
it's a calculator program (I found this part of it online).
Code:
#import "Calculator.h"
@implementation Calculator
{
float result;
int currentOperation;
float currentNumber;
NSMutableString *CalculatorScreen;
}
-(void)DigitPressed:(int) digit;
{
currentNumber = currentNumber * 10 + (float) digit;
CalculatorScreen = [NSString stringWithFormat: @"%.2f", currentNumber];
}
-(void)OperationPressed:(int) sender {
if (currentOperation == 0) result = currentNumber;
else {
switch (currentOperation) {
case 1:
result = result + currentNumber;
break;
case 2:
result = result - currentNumber;
break;
case 3:
result = result * currentNumber;
break;
case 4:
result = result / currentNumber;
break;
case 5:
break;
}
}
currentNumber = 0;
CalculatorScreen = [NSString stringWithFormat:@"%.2f",result];
currentOperation = sender;
}
-(void) clear
{
[CalculatorScreen setString:@""];
}
-(NSString *) getDisplay
{
return CalculatorScreen;
}
-(float) getResult
{
return result;
}
-(void) setCurrentOperator
{
currentOperation=0;
}
@end
I think it is something with the switch statement because the calculator behaves as if it is never receiving the initial 0 for CurrentOperator. I went so far as to add a setter method for that specific reason in the viewdidload with no results. and with that, I'm stumped.