Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

aemti

macrumors newbie
Original poster
Jun 17, 2010
2
0
Can someone see where this code is wrong I can't see it but i've been staring at it for three hours! could it be in a different file? ie.) implementation files?

Code:
#import <UIKit/UIKit.h>
#import "CalculatorBrain.h"

@interface CalculatorViewController : UIViewController
{
	IBOutlet UILabel *display;
	CalculatorBrain *brain;
	BOOL userIsInTheMiddleOfTypingANumber;
}

- (IBAction)digitPressed:(UIButton *)sender
{    <---this is where the error expected";"before"{" token comes in--->
		
	if (userIsInTheMiddleOfTypingANumber) 
	{ 
		[display setText:[[display text] stringByAppendingString:digit]]
	} 
	else 
	{ 	
		[display setText:digit] 
		userIsInTheMiddleOfTypingANumber = YES 
	}
	NSString *digit = [[sender titleLabel] text]
	
}
- (IBAction)operationPressed:(UIButton *)sender
{
	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
 
You're right

Sure, try to use *.m file as well.

In you *.h file:

Code:
#import <UIKit/UIKit.h>
#import "CalculatorBrain.h"

@interface CalculatorViewController : UIViewController
{
IBOutlet UILabel *display;
CalculatorBrain *brain;
BOOL userIsInTheMiddleOfTypingANumber;
}

- (IBAction)digitPressedUIButton: (id)sender;
- (IBAction)operationPressedUIButton: (id)sender;
@end

and then in *.m file put implementation


Code:
- (IBAction)digitPressedUIButton: (id)sender { 
if (userIsInTheMiddleOfTypingANumber) 
 ...
 
ccamelot is correct, it's because you're putting code inside the @interface.

If you want to put it in one file like that, you still need to define the interface separately from the implementation.
 
could you clarify which file?

I've tried both with no success. When you say *.m and *.h are you referring to the view controller files or the calculatorbrain.m and .h files? Thanks again!
 
The code ccamelot posted needs to go in your CalculatorViewController.h file, then your method implementation goes in the CalculatorViewController.m file.

CalculatorViewController.h should look something like (as ccamelot posted):
Code:
#import <UIKit/UIKit.h>
#import "CalculatorBrain.h"

@interface CalculatorViewController : UIViewController
{
IBOutlet UILabel *display;
CalculatorBrain *brain;
BOOL userIsInTheMiddleOfTypingANumber;
}

- (IBAction)digitPressedUIButton: (id)sender;
- (IBAction)operationPressedUIButton: (id)sender;
@end

CalculatorViewController.m should look something like:
Code:
#import "CalculatorViewController.h"

@implementation CalculatorViewController

- (IBAction)digitPressed:(UIButton *)sender
{    <---this is where the error expected";"before"{" token comes in--->
		
	if (userIsInTheMiddleOfTypingANumber) 
	{ 
		[display setText:[[display text] stringByAppendingString:digit]]
	} 
	else 
	{ 	
		[display setText:digit] 
		userIsInTheMiddleOfTypingANumber = YES 
	}
	NSString *digit = [[sender titleLabel] text]
	
}
- (IBAction)operationPressed:(UIButton *)sender
{
	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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.