objective-cnsstringtextfieldxcode4.6.3

Complex Calculation


I'm having a heck of a time getting this code to shake out correctly. I'm currently stuck at the very last line of text. I'm new to writing code all together. Everything I've learned has been found online and through this site. Please see the underlined notes below. Please help so i can see if my calculation even works...

//.h


@interface ViewController : UIViewController

@property (weak, nonatomic) IBOutlet UITextField *Price87;
@property (weak, nonatomic) IBOutlet UITextField *MPG87;
@property (weak, nonatomic) IBOutlet UITextField *PriceE85;
@property (weak, nonatomic) IBOutlet UITextField *MPGE85;
@property (weak, nonatomic) IBOutlet UITextField *GasTankSize;

- (IBAction)Settings:(id)sender;
- (IBAction)pergallon:(id)sender;
- (IBAction)pertank:(id)sender;

@end


//.m

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController   <-----------  Getting a Incomplete Implementation here...

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (IBAction)Settings:(id)sender {
    Settings *settings = [[Settings alloc] initWithNibName:nil bundle:nil];
    [self presentViewController:settings animated:YES completion:NULL];
}

- (IBAction)addNums:(id)sender {
    int a = ([_Price87.text floatValue]);
    int b = ([_MPG87.text floatValue]);
    int c = ([_PriceE85.text floatValue]);
    int d = ([_MPGE85.text floatValue]);
    int e = ([_GasTankSize.text floatValue]);
    int ans = ((a*e)-((e+(a*e)-(c*e)/b)*d)/e);

    [ans setText:[NSString stringWithFormat:@"%i", pergallon]];   <---------  This is the line giving me trouble.  I'm getting a "use of undeclaired identifier 'pergallon'

}

@end

Solution

  • Well you've a number of errors, consider:

    int a = ([_Price87.text floatValue]);
    

    On the righthand size you as for the floatValue - a floating point number with a fractional part, yet on the left hand size you declare a to be int - a whole number without any fractional part. The assignment will truncate the number discarding the fractional part, e.g. 1.82 will become 1. That is probably not what you want. Did you mean to declare a to be a float?

    But let's look at your math and units - based on the names you've given your fields.

    a is presumably in $/gal, e is in gal, so a*e is in $ (the price to fill the tank). Let's put the units into part of your equation:

    e+(a*e) => gal + $
    

    Now adding gallons and dollars makes no sense - you'll get a number but its a nonsense number. The rest of the expression makes little sense as well.

    The compiler won't spot any of the above, computers are fast idiots - ask them to compute nonsense and they'll do it.

    What the computer will spot is simple errors. The line it has complained about refers to pergallon, which doesn't exist. You probably meant to use ans. While fixing that might keep the compiler happy it won't address your unit issues, you need to figure out your math.

    HTH.