New Posts  All Forums:Forum Nav:

Objective-C issue

post #1 of 2
Thread Starter 
Hey all, I'm currently learning Objective-C using the book 'Programming in Objective-C (3rd Edition)' and I've gotten to a part I can't seem to get working right.

In the book I'm shown how to make a pseudo-calculator using a calculator class, but I can't get the code to run correctly.

Code:
#import <Foundation/Foundation.h>

@interface Calculator : NSObject 
{
    double accumulator;
}

//accumulator methods
-(void) setAccumulator: (double) value;
-(void) clear;
-(double) accumulator;

//arithmetic methods
-(void) add: (double) value;
-(void) subtract: (double) value;
-(void) multiply: (double) value;
-(void) divide: (double) value;

@end

@implementation Calculator

-(void) setAccumulator:(double)value
{
    accumulator = value;
}

-(void) clear
{
    accumulator = 0;
}

-(double) accumulator
{
    return accumulator;
}

-(void) add:(double)value
{
    accumulator += value;
}

-(void) subtract:(double)value
{
    accumulator -= value;
}

-(void) multiply:(double)value
{
    accumulator *= value;
}

-(void) divide:(double)value
{
    accumulator /= value;
}

@end

int main (int argc, const char * argv[])
{

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    Calculator *deskCalc = [[Calculator alloc] init];
    
    [deskCalc setAccumulator: 100.0];
    [deskCalc add: 200.];
    [deskCalc divide:15.0];
    [deskCalc subtract:10.0];
    [deskCalc multiply:5];
    
    NSLog(@"The result is %g", [deskCalc accumulator]);
    
    [deskCalc release];
    [pool drain];
    return 0;
}
It seems to compile fine, but when I run it in the compiler it stops at
Code:
-(void) setAccumulator:(double)value
{
    accumulator = value;
}
and won't continue running.

Anyone know what's wrong? Did I mistype something, perhaps?
post #2 of 2
I am not really sure, but it seems to me that you have a method and a variable with the same name, "accumulator".

Quote:
Originally Posted by Red Rabbit View Post
-(double) accumulator
{
return accumulator;
}
Is this the problem? Or is this not the case?
If this is the case, then it could be that the compiler is not sure whether you are referring to the method or the variable, and so it stops.

I don't know much about programming. I apologize in advance for being useless...

edit: forgot to finish post
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming