Reputation: 11
I'm working on a app in Xcode and have run into a problem. My app needs to collect input from a TextField (where the user enters information) an put it into my equation & eventually give the user a result.
For example - a user enters their Weight, Height and Age.
My app then needs to take these inputs and place them into the following equation:
Men: RMR = 66,473 + (13,751*WEIGHT) + (5,0033*HEIGHT) - (6,755*AGE)
But how do I code this? What I have made so far:
My .h file is as follows:
#import <UIKit/UIKit.h>
IBOutlet UITextField *Weight;
IBOutlet UITextField *Height;
IBOutlet UITextField *Age;
IBOutlet UILabel *rectResult;
@interface ViewController : UIViewController
@property (weak, nonatomic) IBOutlet UITextField *Weight;
@property (weak, nonatomic) IBOutlet UITextField *Height;
@property (weak, nonatomic) IBOutlet UITextField *Age;
@property (weak, nonatomic) IBOutlet UILabel *rectResult;
-(IBAction)calculate:(id)sender;
@end
and my .m file:
@implementation ViewController
-(IBAction)calculate:(id)sender {
float floatRectResult=[Weight.text floatValue]*
[Age.text floatValue];
NSString *stringRectResult=[[NSString alloc]
initWithFormat:@"%1.2f",floatRectResult];
rectResult.text=stringRectResult;
}
- (void)viewDidLoad
{
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
PS.: Sorry for my English - Can anybody help me? :)
Upvotes: 1
Views: 1125
Reputation: 8576
Try something like this:
-(IBAction)calculate:(id)sender {
//66,473 + (13,751*WEIGHT) + (5,0033*HEIGHT) - (6,755*AGE)
float result = (66473 + (13751*[Weight.text floatValue]) + (50033*[Height.text floatValue]) - (6755*[Age.text floatValue]));
rectResult.text = [NSString stringWithFormat:@"%.2f", result];
}
Since you appear to be using some constants, it may be better to #define
them so that you can easily change them later. Example:
//Above your implementation
#define kWeightConst 13751.0 //Can be used later as just kWeightConst
Upvotes: 1