Question

I am building an app that does a lot of math and calculations. I have slider values that are displayed in labels and I have to use those values in a function to get a final value. I have these slider values(the numbers are what i have as user input to test the formula and see if it works):

Ageslider=20, weightslider=144, feetslider=5,inchesslider=9.03.

I have to first convert weight,feet,and inches into metric to plug into the overall formula, but the user does not need to see those values. to turn those to metric the formula is this:

weightslider/2.2,  feetslider*12*2.54,   inchesslider*2.54.

Now for the overall formula:

66.473+5.0033*((feetslider*12.2.54)+(inchesslider*2.54))+13.7516*(weightslider/2.2)-6.755*ageslider

or simplified this is what the original formula is supposed to be:

66.473+5.0033*(height in cm)+13.7516*(weight in kg)-6.755*age

When i type the formula

(66.473+5.0033*((feetslider*12.2.54)+(inchesslider*2.54))+13.7516*(weightslider/2.2)-6.755*ageslider)

and then run the program and use the test numbers in the program I cannot get the final value to equal what it should. I have put the formula in excel and ran it through my calculator with the correct order of operations and get 1708.7373. When I run it in the program it comes up with 1747.7.

I then tried putting (float) in front of the slider values in the formula and the program got this: 1718.67. Then i tried putting (double) in front of the slider values and the program got this: 1689.62. Here is the actual code copied out of xcode that I am using:

label.text = [[NSString alloc] initWithFormat:@"%.2f",66.473+(5.0033*((feetslider.value*12*2.54)+(inchesslider.value*2.54)))+(13.7516*(weightslider.value/2.2))-(6.755*ageslider.value)];

It is also coming up with the warning: Assignment makes pointer from integer without a cast. I do not know what that means.

Somebody please help me, I have been stuck on this for hours trying to change the formula around to get the correct answer and get rid of the warning but I cannot get it for the life of me!!!

Thanks!

Update!! here is the exact code I have now, still not coming up with right answer. When I run the program I still get the error "Assignment makes pointer from integer without a cast." It shows up on the line with the "if(malebutton = Yes).

-(IBAction)malebutton{ if (malebutton = YES) {

    //Intermediate values
    float totalWeight;
    totalWeight = weightslider.value*.45454545;
    float heightFeetToCM;
    heightFeetToCM =feetslider.value*12*2.54;
    float heightInchesToCM;
    heightInchesToCM = inchesslider.value*2.54;
    float totalHeight; 
    totalHeight = heightFeetToCM + heightInchesToCM;

    //Final Component Values
    float calculatedHeight;
    calculatedHeight = 5.0033*totalHeight;
    float calculatedWeight;
    calculatedWeight = 13.7516*totalWeight;
    float calculatedAge;
    calculatedAge = 6.755*ageslider.value;

    //Total Value
    float finalValue;
    finalValue = 66.473 + calculatedHeight + calculatedWeight - calculatedAge;

    totalcaloriesburnedlabel.text = [[NSString alloc] initWithFormat:@"%.2f",finalValue];

UPDATE 2;

Here is how i have the sliders connected to their labels. Formats are the same for all of them, except for how many decimals I have displayed on each.

-(IBAction)changeLabelFeet { feetLabel.text = [[NSString alloc] initWithFormat: @"%.0f",feetSlider.value]; }

Était-ce utile?

La solution

Update from original answer: I went back and tried it on Xcode myself. I started with just writing the values (float age = 20;) in to see if the problem was in the calculation, and ended up getting the right answer, being 1708.74 (with the .2f setting on the float in the string). I then decided to see if getting the values from the sliders were the issue, starting with just the age on a slider and everything else still hard coded, I got the wrong answer initially and quickly realized I didn't hook up my slider in IB. After correcting that I got the right answer. Here is my code.

.h

@interface ViewController : UIViewController {

IBOutlet UILabel *myLabel;
IBOutlet UISlider *ageSlider;
IBOutlet UISlider *weightSlider;
IBOutlet UISlider *feetSlider;
IBOutlet UISlider *inchesSlider;


}

@property (nonatomic, retain) IBOutlet UILabel *myLabel;
@property (nonatomic, retain) IBOutlet UISlider *ageSlider;
@property (nonatomic, retain) IBOutlet UISlider *weightSlider;
@property (nonatomic, retain) IBOutlet UISlider *feetSlider;
@property (nonatomic, retain) IBOutlet UISlider *inchesSlider;

@end

.m

@interface ViewController ()

@end

@implementation ViewController

@synthesize myLabel;
@synthesize ageSlider;
@synthesize weightSlider;
@synthesize feetSlider;
@synthesize inchesSlider;

-(void)calculateLabel{

//float age = 20;
float age = ageSlider.value;
NSLog(@"My age slider value is %.2f", ageSlider.value);
//float weight = 144;
float weight = weightSlider.value;
NSLog(@"My weight slider value is %.2f", weightSlider.value);
//float heightFeet = 5;
float heightFeet = feetSlider.value;
NSLog(@"My feet slider value is %.2f", feetSlider.value);
//float heightInches = 9.03;
float heightInches = inchesSlider.value;
NSLog(@"My inches slider value is %.2f", inchesSlider.value);


float weightKG = weight*.454545;
float heightFeetToCM = heightFeet * 12 * 2.54;
float heightInchesToCM = heightInches * 2.54;
float totalHeight = heightFeetToCM + heightInchesToCM;

float calculatedHeight = 5.0033 * totalHeight;
float calculatedWeight = 13.7516 * weightKG;
float calculatedAge = 6.755 * age;

float finalValue = 66.473 + calculatedHeight + calculatedWeight - calculatedAge;

myLabel.text = [NSString stringWithFormat:@"%.2f", finalValue];

}

- (void)viewDidLoad
{
    [super viewDidLoad];


    [self calculateLabel];
// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

So first, check to make sure your NSLog looks like this.....

2012-06-17 23:57:53.557 Testing6[1404:10103] My age slider value is 20.00

2012-06-17 23:57:53.559 Testing6[1404:10103] My weight slider value is 144.00

2012-06-17 23:57:53.559 Testing6[1404:10103] My feet slider value is 5.00

2012-06-17 23:57:53.559 Testing6[1404:10103] My inches slider value is 9.03

If it doesn't work after that, the only other thing I can think of is that it would be the fact that your sliders are probably not updating continuously (aka the label showing your slider value is showing 20 on age to the user, but its really set to 19). Which I might be able to help you with that if I dig through my old Xcode projects. Let me know.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top