Help with my Code please!

Discussion in 'iOS Programming' started by JDSFTW, Oct 14, 2011.

  1. JDSFTW, Oct 14, 2011
    Last edited by a moderator: Oct 14, 2011

    JDSFTW macrumors newbie

    Joined:
    Oct 14, 2011
    #1
    Hello MacRumors,

    I'm new here and just started obj - C programming last week. Today I was performing the ritual of trying to create a calculator app using the xcode and userinterface.

    I finished but when I debugged, I got some messages that I didn't quite understand.

    It was saying that some of my methods were undeclared, which doesn't make sense since I defined them in my @interface section...

    Anyways, here's the code

    Code:
    #import "Calculator4_vbaViewController.h"
    
    @implementation Calculator4_vbaViewController
    -(CalculatorBrain *)brain
    {
    	if (!brain) {
    		brain = [[CalculatorBrain alloc] init];
    	}
    	return brain;
    -(IBAction)digitPressed:(UIButton *)sender;
    {
    	if (userIsInTheMiddleOfTypingANumber) {
    		[display setText: [[display text] stringByAppendingString:digit]];
    	} else {
    		[display setText:digit];
    		userIsInTheMiddleOfTypingANumber = YES;
    	}
    }
    -(IBAction)operationPressed:(UIButton *)sender;
    {
    	NSString *digit = [[sender titleLabel] text];
    	if (userIsInTheMiddleOfTypingANumber) {
    		[[self brain] setOperand:[[display text] doubleValue]];
    		userIsInTheMiddleOfTypingANumber = NO;
    	}
    		
    	NSString *operation = [[sender titleLabel] text];
    	double result = [[self brain] performOperation:operation];
    	[display setText: [NSString stringWithFormat:@"%g", result]];
    }
    
    @end
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Post the actual text of the actual error messages.

    This code:
    Code:
    @implementation Calculator4_vbaViewController
    -(CalculatorBrain *)brain
    {
    if (!brain) {
    brain = [[CalculatorBrain alloc] init];
    }
    return brain;
    [COLOR="Red"]// missing a } here[/COLOR]
    
    has the problem hilited in red.
     
  3. jiminaus macrumors 65816

    jiminaus

    Joined:
    Dec 16, 2010
    Location:
    Sydney
    #3
    My first reading revealed some syntax errors in your code. Blue code needs to be added, red code needs to be removed.

     
  4. JDSFTW, Oct 14, 2011
    Last edited by a moderator: Oct 14, 2011

    JDSFTW thread starter macrumors newbie

    Joined:
    Oct 14, 2011
    #4
    Update on corrections

    ok so I did exactly what you two said and now I only have 1 error as opposed to 8 or something I had earlier.

    It is still saying 'digit' undeclared in this line of code:


    if (userIsInTheMiddleOfTypingANumber) {
    [display setText: [[display text] stringByAppendingString:digit]];

    Thanks for your guys's help I just started programming and wanted to get my feet wet with the calculator app.
    Code:
    #import "Calculator4_vbaViewController.h"
    
    @implementation Calculator4_vbaViewController
    -(CalculatorBrain *)brain
    {
    	if (!brain) {
    		brain = [[CalculatorBrain alloc] init];
    	}
    	return brain;
    }
    -(IBAction)digitPressed:(UIButton *)sender
    {
    	if (userIsInTheMiddleOfTypingANumber) {
    		[display setText: [[display text] stringByAppendingString:digit]];
    	} else {
    		[display setText:digit];
    		userIsInTheMiddleOfTypingANumber = YES;
    	}
    }
    -(IBAction)operationPressed:(UIButton *)sender
    {
    	NSString *digit = [[sender titleLabel] text];
    	if (userIsInTheMiddleOfTypingANumber) {
    		[[self brain] setOperand:[[display text] doubleValue]];
    		userIsInTheMiddleOfTypingANumber = NO;
    	}
    		
    	NSString *operation = [[sender titleLabel] text];
    	double result = [[self brain] performOperation:operation];
    	[display setText: [NSString stringWithFormat:@"%g", result]];
    }
    
    @end
    
     
  5. chown33, Oct 14, 2011
    Last edited: Oct 14, 2011

    chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    1. Please use CODE tags around your code. Read this:
    http://forums.macrumors.com/showthread.php?t=747660

    It's from the Sticky at the top of the iPhone/iPad Programming forum.
    The Sticky is labeled "Sticky: Posting code, please use the
    Code:
     tags"
    
    
    2. Post the contents of "Calculator4_vbaViewController.h".
    
    
    3. In your code:
    [CODE]
    [COLOR="Red"]-(IBAction)digitPressed:(UIButton *)sender
    {
    	if (userIsInTheMiddleOfTypingANumber) {
    		[display setText: [[display text] stringByAppendingString:digit]];
    	} else {
    		[display setText:digit];
    		userIsInTheMiddleOfTypingANumber = YES;
    	}
    }
    [/COLOR][COLOR="Blue"]-(IBAction)operationPressed:(UIButton *)sender
    {
    	NSString *digit = [[sender titleLabel] text];
    	if (userIsInTheMiddleOfTypingANumber) {
    		[[self brain] setOperand:[[display text] doubleValue]];
    		userIsInTheMiddleOfTypingANumber = NO;
    	}
    		
    	NSString *operation = [[sender titleLabel] text];
    	double result = [[self brain] performOperation:operation];
    	[display setText: [NSString stringWithFormat:@"%g", result]];
    }
    [/COLOR]
    There is no variable named 'digit' in the body of the method hilited in red. There is a 'digit' in the body of the method hilited in blue. Unfortunately, that digit is not accessible from the red method. If you don't understand why that's so, you need to review how variables are declared, and how a variable defined in one block is not accessible to blocks declared outside it.

    Overall, you should probably spend more time reviewing how to write code properly. If the Calculator is your very first app, and you haven't completed any tutorial walk-through on how to write code, then you're already in over your head. It might help if you told us what tutorials or books you've gone through to reach this point.
     
  6. JDSFTW thread starter macrumors newbie

    Joined:
    Oct 14, 2011
    #6

    Yeah ok so like I said I am brand new at programming

    I am doing the stanford University iOS programming lectures on Itunes U but more so I am reading and doing the exercises of the Kochan book that is highly recommended.

    I acknowledge that I am in over my head, but it is important to start getting hands on experience with code and not just try to bookworm it. I'm pretty happy actually that I've done as much as I've done for only doing it for around a week.

    I know I have to define the variable, but what do I define it as?


    and I apologize about the code tags, I've tried to use them multiple times but it doesn't seem to work with the (#) method.
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    Objectively speaking, that's the opinion of a person with only one week of programming experience.

    You already defined it once. If you understand that code, then you should know what to do. If you don't understand that code, then it's time to bookworm it.

    There's no way anyone can debug that. You haven't described a procedure. You've simply given an opinion of an outcome: "It doesn't seem to work". You may also have described what you did, "the (#) method", but it's not at all clear what "the (#) method" means.

    To debug something effectively, you should:
    1. Be specific.
    2. Post your code (or other procedure).
    3. Describe what you expected to happen.
    4. Describe what actually happened.

    So exactly what procedure did you follow? Exactly what actions did you perform? Post an example, describing what you did and what happened.

    My example:

    1. I typed the sentence: This is an example of CODE tags.
    2. I selected the sentence.
    3. I clicked the # icon-button, i.e. the CODE tag button.
    4. I observed that the sentence was surrounded by CODE and /CODE tags enclosed in []s.
    5. I clicked the "Preview Post" button to preview my post.
    6. The sentence shows up as a CODE section.
    Here it is:
    Code:
    This is an example of CODE tags.
    

    Post your example.
     
  8. JDSFTW thread starter macrumors newbie

    Joined:
    Oct 14, 2011
    #8
    Ok?

    Nevertheless I fixed it so all is resolved
     
  9. chown33, Oct 15, 2011
    Last edited: Oct 15, 2011

Share This Page