-4

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
Ivan Chaer
  • 6,980
  • 1
  • 38
  • 48
user3173553
  • 43
  • 1
  • 5
  • `NSInteger yahh = [self.screen.text integerValue];` – holex Jan 10 '14 at 14:52
  • The title doesen't make sense at all. A UILabel can not be converted to int. You can grab the text of the label as an int-value, but that's another thing :) – hfossli Jan 10 '14 at 14:55
  • @hfossli Yes, thats what i was trying to do, thanks for pointing out the title error. – user3173553 Jan 10 '14 at 14:59
  • How are you storing the first input number? You should use the exact same code if the first one is working properly. – Putz1103 Jan 10 '14 at 15:03
  • @user3173553 You should then either update the title or delete this question as it most likely is a duplicate if you change it to "How can I convert NSString to int?" – hfossli Jan 10 '14 at 15:03
  • @Putz1103 I tried that. And i got the same result - 0. I think theres something wrong with the conversion of the current screen to NSString to int. – user3173553 Jan 10 '14 at 15:07
  • You say the current screen. I was assuming your self.screen is an instance of a UILabel that you are using as input. Am I wrong in that assumption? – Putz1103 Jan 10 '14 at 15:11
  • Your are right. self.screen is the UILabel and when I say current screen i mean the number currently on the UILabel screen. Sorry for the confusion. Thanks – user3173553 Jan 10 '14 at 15:15
  • `startedEnteringSecondNumber = @"NO"` You should use BOOL type here `BOOL startedEnteringSecondNumber = NO;` instead of comparing strings. – bsarr007 Jan 10 '14 at 15:46
  • Ok, thanks - going to change it now, again Im new to iOS development so I really appreciate all the tips and help. – user3173553 Jan 10 '14 at 15:48
  • So what is the value of self.screen (not self.screen.text) when you put `NSLog(@"=%p", self.screen)` on viewDidLoad? – bsarr007 Jan 10 '14 at 15:58
  • @bsarr007 its 0. But the UILabel is set to zero, then changed after by tapping numbers etc. – user3173553 Jan 10 '14 at 16:01
  • `NSLog(@"=%p", self.screen)` should give you the address of the object not the value, you should have output like 0xef23a445! – bsarr007 Jan 10 '14 at 16:04
  • I must have had a typo, I don't think thats in my actual code, sorry for the confusion. – user3173553 Jan 10 '14 at 16:08
  • possible duplicate of [How to convert an NSString into an NSNumber](http://stackoverflow.com/questions/1448804/how-to-convert-an-nsstring-into-an-nsnumber) – codercat Jan 10 '14 at 16:39

5 Answers5

2

NSString *flabb = [NSString stringWithFormat:@"%@", self.screen];

This line of code will return a very bad thing. You are taking an object of type UILabel and turning it into a string for some reason. If you want to get the text from that UILabel then you need to use:

NSString *flabb = [NSString stringWithFormat:@"%@", self.screen.text];

Then you can do your int conversion on that NSString and get hopefully what you need.

Putz1103
  • 6,211
  • 1
  • 18
  • 25
2

You might have better luck if you do:

NSString *flabb = self.screen.text;

instead of:

NSString *flabb = [NSString stringWithFormat:@"%@", self.screen];
Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
1

Converting the UILAabel to a String ???

If I read right and self.screen is your label, you access the text inside with self.screen.text

And fix the multiplication :

//doing the multiplication
NSInteger actualProblem = yahh + storedResult;
// multiply, not add : ------  *  ---------- ;
shinyuX
  • 1,519
  • 1
  • 9
  • 20
  • Thanks for pointing out the addition, I was testing something before i pasted in the code. I also just change it to 'self.screen.text' and i am still getting zero. – user3173553 Jan 10 '14 at 14:57
0
//Converting the UILabel to a string
NSString *flabb = [NSString stringWithFormat:@"%@", self.screen];

This should be:

//Converting the UILabel to a string
NSString *flabb = [NSString stringWithFormat:@"%@", self.screen.text];

You have to use self.screen.text instead of self.screen.

bsarr007
  • 1,914
  • 14
  • 14
  • I just fixed it to 'self.screen.text' and i am still getting zero. – user3173553 Jan 10 '14 at 14:56
  • Can you please put a breakpoint just before the `NSInteger actualProblem` line and tell me the value of yahh and storedResult – bsarr007 Jan 10 '14 at 15:10
  • I just added a breakpoint by the actual 'NSInteger actualProblem' and got a Thread1: Breakpoint 1.1 notice. I also logged the value of yahh and stored result and got 0 for yahh and 2 (the number it should be) for storedResult – user3173553 Jan 10 '14 at 15:14
  • Ok this is more clear, can you log the value of `self.screen.text`? – bsarr007 Jan 10 '14 at 15:15
  • So i logged self.screen.text right before actualProblem takes place and got 0. Should I log it in a different spot? – user3173553 Jan 10 '14 at 15:18
  • Verify these things: self.screen is not nil, self.screen is linked on the storyboard. – bsarr007 Jan 10 '14 at 15:20
  • Can you also do a `po self.screen` when you are on the breakpoint? – bsarr007 Jan 10 '14 at 15:22
  • self.screen is linked to the storyboard. What do you mean by 'po self.screen' I am new to iOS development. Thanks. – user3173553 Jan 10 '14 at 15:23
  • When you are on a breakpoint, you can print values on the debug console like this: `(lldb) po self.screen` to print all objects you want. – bsarr007 Jan 10 '14 at 15:25
  • When I log self.screen to the console I get 0 – user3173553 Jan 10 '14 at 15:29
  • Do you update the self.screen text when you press on the operation buttons? – bsarr007 Jan 10 '14 at 15:29
  • Yes, self.screen can't be 0, put a breakpoint on your viewDidLoad and log the value of self.screen – bsarr007 Jan 10 '14 at 15:32
  • yeas, whenever the user presses a number it adds that number to the UILabel. When *,?,+,- are pressed it saves whats currently on the screen in storedResult, then when the user types the next set of numbers it clears whats on the screen and adds the numbers they are entering. When the user presses = it should multiply the storedResult and whatever is on the UILabel (storedResult * yaah) – user3173553 Jan 10 '14 at 15:33
  • Can you just edit your comment and add all your ViewController Code above? it'll be easier! – bsarr007 Jan 10 '14 at 15:35
  • The default text of UIlabel is 0 which is what I get when I log it in the viewDidLoad. When I add a breakpoint to the viewDidLoad I get a thread1: breakpoint 1.1 by the [super viewDidLoad] – user3173553 Jan 10 '14 at 15:35
  • It is saying the code is too long to put in the comments, should i put it in the question? I real appreciate the help, thanks. – user3173553 Jan 10 '14 at 15:36
  • Yes, Sure put it in the question! – bsarr007 Jan 10 '14 at 15:41
  • I put it int the question, its extremely sloppy. I only have number 1, 2, 3 working and excuse the divide button. I wanted to get basic things working like number input and the multiplication b4 i did anything else – user3173553 Jan 10 '14 at 15:43
0

Learn to use the debugger, or at least log statements.

This line:

NSString *flabb = [NSString stringWithFormat:@"%@", self.screen];

Has 2 problems. First, as others have pointed out, if "self.screen" is a label (horrible variable name) then you need use self.screen.text, not self.screen.

Second, there is no point in using stringWithFormat with a format string of "%@". That takes an input string, does a lot of extra work on it, and returns a copy of exactly the same string. Don't do that. It's a useless waste of time.

That line should be

NSString *flabb = self.screen.text;

Now, let's add some log statements:

- (IBAction)equalsWhenPressed:(id)sender
{
  if ([operation  isEqual:@"x"])
  {

    //Get the UILabel's text (no conversion involved)
    NSString *flabb = self.screen.text;

    NSLog(@"IBOutlet self.screen = %@. label text = %@", self.screen, flabb);

    //converting the string to a integer
    NSInteger yahh = [flabb integerValue];

    NSLog(@"converted to an int, text = %d", yahh);


    //doing the multiplication
    NSInteger actualProblem = yahh + storedResult;

    NSLog(@"Integer result = %d", actualProblem);

    NSString *result = [NSString stringWithFormat:@"%ld", (long)actualProblem];

    NSLog(@"result = %@", result);

    //Putting the result of the problem back in the UILabel
    self.screen.text = result;

    operation = nil;
  }
}
Duncan C
  • 128,072
  • 22
  • 173
  • 272
  • When I log 'converted to an int, text = %@' I get 0. From what I understand the problem is the conversion. It just makes the int zero. Thanks. – user3173553 Jan 10 '14 at 16:04
  • The format specifier `@"%@"` is perfectly ok for non string objects, although `[obj description]` is the same but better. – JeremyP Jan 10 '14 at 16:10
  • @JeremyP, Sure, the %@ format fragment has value. My point is that there is no point in using the statement NSString *string = [NSString stringWithFormat: @"%@", inputString]; It is a do-nothing that wastes memory and processor time with no benefit. – Duncan C Jan 10 '14 at 16:17
  • @DuncanC Not disagreeing with you, just expanding the point. – JeremyP Jan 10 '14 at 16:19
  • user, what are you getting from the previous log statement that shows the label text? Copy and paste the output to your post. – Duncan C Jan 10 '14 at 16:19
  • Also the following `NSLog(@"%@", ...)` is definitely of use. – JeremyP Jan 10 '14 at 16:22