Discussion Forums  >  Plugins, Customizing, Source Code

Replies: 4    Views: 70

Kristian_N
Code is Art
Profile
Posts: 91
Reg: Oct 27, 2011
Copenhagen
2,610
03/04/14 03:43 PM (10 years ago)

Learning Objective C - Variables

Hi all, I have started to learn Objective C and have tried to build a simple app that will calculate BMI index. The code works all right - I get no errors, BUT the result is wrong and shows a "0" instead of the BMI index which I had hoped it would show. I am obviously doing something wrong and I suspect that my variables are not declared properly. I have stared at this for hours and hours and I am stuck. Can one of you clever guys see what I am doing wrong? Thanks in advance Kristian // // ViewController.m // BMICalc // // Created by Kristian Nielsen on 28/06/12. // Copyright (c) 2012 Contentservices.dk. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController @synthesize VaegtSlider; @synthesize HoejdeSlider; @synthesize Vaegt; @synthesize Hoejde; @synthesize BMIShow; - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } - (void)viewDidUnload { [self setVaegt:nil]; [self setHoejde:nil]; [self setBMIShow:nil]; [self setVaegtSlider:nil]; [self setHoejdeSlider:nil]; [super viewDidUnload]; // Release any retained subviews of the main view. } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } //Deklare global variables float VaegtValue; float HoejdeValue; float BMIIndex; - (IBAction)Vaegtskiftet:(id)sender { VaegtValue = [(UISlider *) sender value]; Vaegt.Text = [NSString stringWithFormat:@"Vægt: %d", (int) VaegtValue]; } - (IBAction)Hoejdeskiftet:(id)sender { HoejdeValue = [(UISlider *) sender value]; Hoejde.Text =[NSString stringWithFormat:@"Højde: %d", (int)HoejdeValue]; BMIIndex = VaegtValue/(HoejdeValue*HoejdeValue); BMIShow.text = [NSString stringWithFormat:@"BMI INDEX %d", (int)BMIIndex]; } @end
 
Kittsy
buzztouch Evangelist
Profile
Posts: 2251
Reg: Feb 22, 2012
Liverpool
31,360
like
03/04/14 04:58 PM (10 years ago)
It's not the easiest to see some tips though variables should start with lower case helps differentiate from classes Class: NSString, BT_item, Person, Car. Variables: height, weight , vaegt, hoejde, name, andysFat. you have synthesised the slider so no need to cast. - (IBAction)Vaegtskiftet:(id)sender { VaegtValue = [(UISlider *) sender value]; - (IBAction)Vaegtskiftet:(id)sender { VaegtValue = self.VaegtSlider.value; I think the reason it is coming out at zero maybe this though, because when writing some demo code I came across the problem, are the values in metres and kg if you use centimetres the equation is incorrect and works out to 0.0005 or something you are also declaring an integer as the bmi maybe change that to float. it may be rounding down. any way here is a quick look at my rough code I used a xib it has 2 sliders names heightSlider and weightSlider 3 labels heightLabel, weightLabel and bmiLabel. .h // // ViewController.h // tsterbmi // // Created by Andy Kitts on 04/03/2014. // Copyright (c) 2014 Andy Kitts. All rights reserved. // #import <UIKit/UIKit.h> @interface ViewController : UIViewController @property (weak, nonatomic) IBOutlet UISlider *heightSlider; @property (weak, nonatomic) IBOutlet UISlider *weightSlider; @property (weak, nonatomic) IBOutlet UILabel *bmiLabel; @property (weak, nonatomic) IBOutlet UILabel *heightLabel; @property (weak, nonatomic) IBOutlet UILabel *weightLabel; - (IBAction)heightSliderMoved:(id)sender; - (IBAction)weightSliderMoved:(id)sender; @end .m // // ViewController.m // tsterbmi // // Created by Andy Kitts on 04/03/2014. // Copyright (c) 2014 Andy Kitts. All rights reserved. // #import "ViewController.h" @interface ViewController () @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. [self workOutBMI]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } - (IBAction)heightSliderMoved:(id)sender { [self workOutBMI]; } - (IBAction)weightSliderMoved:(id)sender { [self workOutBMI]; } -(void)workOutBMI{ double BMI = self.weightSlider.value /(self.heightSlider.value * self.heightSlider.value); self.bmiLabel.text = [NSString stringWithFormat:@"%f",BMI]; self.heightLabel.text = [NSString stringWithFormat:@"Height : %f m",self.heightSlider.value]; self.weightLabel.text = [NSString stringWithFormat:@"Weight : %f kg",self.weightSlider.value]; } @end hope that helps a bit.
 
Kittsy
buzztouch Evangelist
Profile
Posts: 2251
Reg: Feb 22, 2012
Liverpool
31,360
like
03/04/14 05:03 PM (10 years ago)
also another point might be that interface builder has the values 0 -1 instead of say 0 - 2.5 for height and 0 - 150 for kg
 
Kittsy
buzztouch Evangelist
Profile
Posts: 2251
Reg: Feb 22, 2012
Liverpool
31,360
like
03/04/14 05:05 PM (10 years ago)
also i see you casting the float value of the slider to an integer that will round up and down wickedly if you don't want all the decimal points you clan change it to %.2f for 2 decimal points double BMI = self.weightSlider.value /(self.heightSlider.value * self.heightSlider.value); self.bmiLabel.text = [NSString stringWithFormat:@"%.2f",BMI]; self.heightLabel.text = [NSString stringWithFormat:@"Height : %.2f m",self.heightSlider.value]; self.weightLabel.text = [NSString stringWithFormat:@"Weight : %.2f kg",self.weightSlider.value];
 
Kristian_N
Code is Art
Profile
Posts: 91
Reg: Oct 27, 2011
Copenhagen
2,610
like
03/05/14 02:08 AM (10 years ago)
Hi Kittsy, Many thanks! I'll look at this in detail later tonight! Looks like you have it, though! Regards, Kristian
 

Login + Screen Name Required to Post

pointerLogin to participate so you can start earning points. Once you're logged in (and have a screen name entered in your profile), you can subscribe to topics, follow users, and start learning how to make apps like the pros.