2

i am new to programming and i need your help... i have a label named "typeLabel" that has a default label text se to "NiMH". This text changes to NiCad through a pickerView. I want through an if-statement to change a multiplier value however "unused variable floatTime" halts the program. The code is:

-(IBAction)calculate:(id)sender {
    if ([typeLabel.text isEqualToString:@"NiCad"])
    {
        float floatTime=([mAhLabel.text floatValue]/chargerSlider.value)*1.4;
    } else {
        float floatTime=([mAhLabel.text floatValue]/chargerSlider.value)*1.5;
    }

    int intHourhs=(floatTime);
    float floatHours=(intHourhs);
    float floatMinutes=(floatTime-floatHours)*60;
    NSString *stringTotal = [NSString stringWithFormat: @"%1.1i Hours and %.0f Minutes", intHourhs, floatMinutes];
    chargeLabel.text=stringTotal;
}
5
  • Note that the "unused variable" message is a warning, not an error. There should be a separate message about floatTime being undeclared, which is an error. Also, this is a language problem (Objective-C), not an IDE problem (XCode). Commented Oct 2, 2011 at 21:23
  • You can format lines as code by indenting them four spaces. The "{}" button in the editor toolbar does this for you. CocoaFu did it for you this time, but next time try it yourself. Click the orange question mark in the editor toolbar for more information and tips on formatting. Commented Oct 2, 2011 at 21:27
  • I declared the variable as proposed and that fixed the problem. However, there is another problem! Although the "typeLabel.text" changes values from "NiCad" to "NiMH", the result of "chargeLabel.text" remains the same, multiplied by the If-statement by 1.5 Commented Oct 2, 2011 at 22:46
  • SO is a Q&A site, not a forum. Read the FAQ section for more info on SO's format. If you have a new issue, you should ask a new question; just make sure you include a complete, concise sample. Comments are largely for clarifications. There's also the chat section (see link at top of page). Commented Oct 3, 2011 at 1:17
  • Thanks, i will have have it in mind. Commented Oct 3, 2011 at 8:02

3 Answers 3

7

You need to move the declaration above the if statement:

float floatTime;
if ([typeLabel.text isEqualToString:@"NiMH"])
{
    floatTime=([mAhLabel.text floatValue]/chargerSlider.value)*1.5;
} else {
    floatTime=([mAhLabel.text floatValue]/chargerSlider.value)*1.4;
}

When you declare a local variable inside a scope it is not visible outside that scope.

Sign up to request clarification or add additional context in comments.

1 Comment

I declared the variable as proposed and that fixed the problem. However, there is another problem! Although the "typeLabel.text" changes values from "NiCad" to "NiMH", the result of "chargeLabel.text" remains the same, multiplied by the If-statement by 1.5
1

float floatTime is defined inside the if statements braces and they define it's scope. Move it out:

float floatTime;
if ([typeLabel.text isEqualToString:@"NiMH"])
{
    floatTime=([mAhLabel.text floatValue]/chargerSlider.value)*1.5;
} else {
    floatTime=([mAhLabel.text floatValue]/chargerSlider.value)*1.4;
}

Curly braces define scope, things defined inside them are not available outside.

Comments

0

You should declare float time in the main block of code, outside the if/else, that's why you are getting an error. Try this:

-(IBAction)calculate:(id)sender {
float floatTime;
  if ([typeLabel.text isEqualToString:@"NiMH"])
{
   floatTime=([mAhLabel.text floatValue]/chargerSlider.value)*1.5;
} else {
   floatTime=([mAhLabel.text floatValue]/chargerSlider.value)*1.4;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.