I have been trying go make a calculator app and have been having trouble with getting the equals button to work. I have gotten to the root of the problem and whats wrong but have no idea how do fix it.
When the user enters a number, presses the multiply button (or +, -, /) the instance variable storedResult saves the number before they enter the next set of numbers to multiply. When the user presses =, whats supposed to happen is current screen (a UILabel) get multiplied by the storedResult variable and convert that back to a string and put it in the UILabel. This does not happen. The UILabel always reads zero.
From playing around with the console I found that the problem seems to be that when converting current screen (UILabel) to an integer (or double, float, int) the number in the screen gets set to zero. If i were to set it up for addition, the number outputted to the UILabel would be what ever stored result would be.
Here is the code:
- (IBAction)equalsWhenPressed:(id)sender {
if ([operation isEqual:@"x"]) {
//Converting the UILabel to a string
NSString *flabb = [NSString stringWithFormat:@"%@", self.screen.text];
//converting the string to a integer
NSInteger yahh = [flabb integerValue];
//doing the multiplication
NSInteger actualProblem = yahh * storedResult;
NSString *result = [NSString stringWithFormat:@"%ld", (long)actualProblem];
//Putting the result of the problem back in the UILabel
self.screen.text = result;
operation = nil;
}}
Thanks so much! I am still getting 0 after fixing the 'self.screen.text' in my code so I changed it above just to avoid repetitive answers. Thank You
Here is the viewController.m :
- (void)viewDidLoad
{
//NSLog(@"%@",self.screen);
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
//self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"Digits Black Theme BG1"]];
startedEnteringSecondNumber = @"NO";
startedEnteringFirstNumber = @"NO";
}
- (IBAction)buttonOnePress:(id)sender {
if ([self.screen.text isEqual:@"0"] ) {
self.screen.text = @"1";
startedEnteringSecondNumber = @"NO";
} else if ([operation isEqual:@"x"]) {
if ([startedEnteringSecondNumber isEqual:@"NO"]) {
self.screen.text = @"1";
startedEnteringSecondNumber = @"YES";
} else {
self.screen.text = [NSString stringWithFormat:@"%@1", self.screen.text];
}
} else {
self.screen.text = [NSString stringWithFormat:@"%@1", self.screen.text];
}
}
- (IBAction)buttonTwoPress:(id)sender {
if ([self.screen.text isEqual:@"0"] ) {
self.screen.text = @"2";
startedEnteringSecondNumber = @"NO";
} else if ([operation isEqual:@"x"]) {
if ([startedEnteringSecondNumber isEqual:@"NO"]) {
self.screen.text = @"2";
startedEnteringSecondNumber = @"YES";
} else {
self.screen.text = [NSString stringWithFormat:@"%@2", self.screen.text];
}
} else {
self.screen.text = [NSString stringWithFormat:@"%@2", self.screen.text];
}
}
- (IBAction)buttonThreePress:(id)sender {
if ([self.screen.text isEqual:@"0"] ) {
self.screen.text = @"3";
startedEnteringSecondNumber = @"NO";
} else if ([operation isEqual:@"x"]) {
if ([startedEnteringSecondNumber isEqual:@"NO"]) {
self.screen.text = @"3";
startedEnteringSecondNumber = @"YES";
} else {
self.screen.text = [NSString stringWithFormat:@"%@3", self.screen.text];
}
} else {
self.screen.text = [NSString stringWithFormat:@"%@3", self.screen.text];
}
}
- (IBAction)buttonPressMultiply:(id)sender {
NSString *flabb = [NSString stringWithFormat:@"%@",[self.screen text]];
NSInteger lastInput = [flabb integerValue];
storedResult = lastInput;
operation = @"x";
}
- (IBAction)buttonPressDivide:(id)sender {
NSString *flabb = [NSString stringWithFormat:@"%@", [self.screen text]];
NSInteger actualDivider = [flabb integerValue];
NSInteger result = actualDivider / 4;
NSString *printedresult = [NSString stringWithFormat:@"%ld",(long)result];
self.screen.text = printedresult;
}
- (IBAction)clearAllButton:(id)sender {
self.screen.text = @"0";
operation = nil;
}
- (IBAction)equalsWhenPressed:(id)sender {
//NSLog([NSString stringWithFormat:@"b1a %ld", (long)storedResult]);
// self.screen.text = [NSString stringWithFormat:@"%f", storedResult];
//self.screen.text = [NSString stringWithFormat:@"%f", (long)[self.screen.text integerValue] * storedResult];
// NSLog(@"%@", self.screen);
if ([operation isEqual:@"x"]) {
// NSLog([NSString stringWithFormat:@"%ld", (long)storedResult]);
// NSLog(@"%@",operation);
//Converting the UILabel to a string
NSString *flabb = self.screen.text;
flabb = [NSString stringWithFormat:@"%@", self.screen.text];
//converting the string to a integer
NSInteger yahh = [flabb integerValue];
NSLog(@"%ld", (long)yahh);
NSLog(@"%ld", (long)storedResult);
NSLog(@"%@", self.screen.text);
//doing the multiplication
NSInteger actualProblem = yahh * storedResult;
NSString *result = [NSString stringWithFormat:@"%ld", (long)actualProblem];
//NSLog(@"%@", result);
self.screen.text = result;
//operation = nil;
}}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end