Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

eForce

macrumors newbie
Original poster
Oct 21, 2012
3
0
1st post here. Thanks in advance.

The following code never satisfies any of the conditions.

Code:
Therefore, dblAnswer is ALWAYS [_lblNum1.text doubleValue];

- (IBAction)cmdOK:(UIButton *)sender {
    double dblAnswer;
    double dblAnswer2;
    
    [_txtAnswer resignFirstResponder];
    
    dblAnswer = [_lblNum1.text doubleValue];
    dblAnswer2 = [_lblNum2.text doubleValue];
    
    if (_lblOperand.text == @"+") {
        dblAnswer = dblAnswer + dblAnswer2;
    }
    if (_lblOperand.text == @"-") {
        dblAnswer = dblAnswer - dblAnswer2;
    }
    if (_lblOperand.text == @"X") {
        dblAnswer = dblAnswer * dblAnswer2;
    }
    if (_lblOperand.text == @"/") {
        dblAnswer = dblAnswer / dblAnswer2;
    }

    _lblResult.Text = [NSString stringWithFormat:@"%f", dblAnswer];
}
 
Last edited by a moderator:
You should probably be looking at the button itself, not the text on the button. I would suggest having each button set to invoke an IBAction.

As is, if you decide to change what's written on the button (say you decide to replace the "X" with a "*" or some other unicode character... or maybe you decide to just use a drawing of some sort rather than text) you'll have to change both your interface file (storyboard or XIB) and your code, rather than just your interface file.

Also, you should be using "else if", not just "if" for the other statements. As it's currently written, your code will have to compare your text against "-" even if it was already determined to be "+".

... a switch case actually might be your best choice...
 
You should probably be looking at the button itself, not the text on the button. I would suggest having each button set to invoke an IBAction.

As is, if you decide to change what's written on the button (say you decide to replace the "X" with a "*" or some other unicode character... or maybe you decide to just use a drawing of some sort rather than text) you'll have to change both your interface file (storyboard or XIB) and your code, rather than just your interface file.

Also, you should be using "else if", not just "if" for the other statements. As it's currently written, your code will have to compare your text against "-" even if it was already determined to be "+".

... a switch case actually might be your best choice...

agreed with the switch. It's just I read everywhere that the switch doesn't work properly with strings.
 
Try the compare method as defines for NSString

http://developer.apple.com/library/...lasses/NSString_Class/Reference/NSString.html

You are actually comparing pointer; one is dynamic from memory management; the others are constants. Will not give meaningful result in general.

Compare is the better/right way.




By the way: welcome on MR!


Thank you. So will an If ElseIf == not work? Forgive me, I'm brand new to xCode but I've programmed in VB for many years.
 
agreed with the switch. It's just I read everywhere that the switch doesn't work properly with strings.

Something like this might work for you if you want the switch functionality:

Code:
  char* cStr;
  [_lblOperand.text getCString:cStr maxLength:1 encoding:NSASCIIStringEncoding];

  switch (cStr[1]) {
    case '+':
      // do something
    case '-':
      // do something
    case '*':
      // do something
    case '/':
      // do something
  }
 
Code:
  char* cStr;
  [_lblOperand.text getCString:cStr maxLength:1 encoding:NSASCIIStringEncoding];

This code is wrong. You must pass an actual buffer address to the method, not an uninitialized pointer. Refer to the NSString class reference doc.

I suggest the characterAtIndex: method with an index of 0.

----------

Thank you. So will an If ElseIf == not work? Forgive me, I'm brand new to xCode but I've programmed in VB for many years.

An if .. else if .. using == will not work. It fails for the same reason using == with NSString fails (i.e. read the earlier posts that explain it).

An if .. else if .. using isEqualToString: will work. It works for the same reason given in the earlier posts.

And Objective-C doesn't have an ElseIf keyword.
 
This code is wrong. You must pass an actual buffer address to the method, not an uninitialized pointer. Refer to the NSString class reference doc.

I suggest the characterAtIndex: method with an index of 0.

----------



An if .. else if .. using == will not work. It fails for the same reason using == with NSString fails (i.e. read the earlier posts that explain it).

An if .. else if .. using isEqualToString: will work. It works for the same reason given in the earlier posts.

And Objective-C doesn't have an ElseIf keyword.

My bad - thanks for the correction. I should probably compile and run these things before posting :p
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.