mmoore410
mmoore410

Reputation: 427

Need advice with an Objective-C program

Still learning in objective c and have a problem I'm just not able to figure out. I'm trying to write an app to figure angles and I've got 4 textfields, 2 sides and 2 angles. The user can either enter the 2 side dimensions and app will figure the angles or user can enter 1 side and an angle and the app will figure the other side and angle. In this my process of figuring out how to get this done I did this code:

- (IBAction)calcPressed {
    float a = ([sideA.text floatValue]/[sideB.text floatValue]);
    float b = atan(a)*180/3.141592653589;
    float e = 90 - b;
    angleA.text = [[NSString alloc] initWithFormat:@"%f", b];
    angleB.text = [[NSString alloc] initWithFormat:@"%f", e];

to take the 2 sides entered and figure the angles and it works fine. So now I need to figure out how to allow either 2 sides to be entered or 1 side and 1 angle so I've come up with this:

- (IBAction)calcPressed {
    float a = ([sideA.text floatValue]);
    float b = ([sideB.text floatValue]);
    float c = ([angleA.text floatValue]);
    float d = ([angleB.text floatValue]);

    if(angleA.text, angleB.text = @"") {
        float e = a/b;

        float f = atan(e)*180/3.141592653589;
        float g = 90 - f;
        angleA.text = [[NSString alloc] initWithFormat:@"%f", f];
        angleB.text = [[NSString alloc] initWithFormat:@"%f", g];
    } else if (sideB.text, angleB.text = @"") {
        float e = tan(c);
        b = a/e;
        d = 90 - c;
        sideB.text = [[NSString alloc] initWithFormat:@"%f", b];
        angleB.text = [[NSString alloc] initWithFormat:@"%f", d];
    }
}

The first part works fine but the second part doesn't. Like I said I'm pretty new at this and I'm sure there is a better way of doing things, like there's probably a better way to divide by pi but that part I could get to work. Any pointers would be greatly appreciated.

Upvotes: 0

Views: 72

Answers (1)

dasdom
dasdom

Reputation: 14063

Change this

if(angleA.text, angleB.text = @"") {

to

if ([angleA.text isEqualToString: @""] && [angleB.text isEqualToString: @""]) {

and same to the else if part.

Upvotes: 1

Related Questions