PDA

View Full Version : Obj-C Program error




slooksterPSV
Jul 1, 2006, 01:10 AM
EDIT: FIXED - it was supposed to be lf not 1f

I copied this program word for word in a book I'm reading and it gives me the wrong results every time:
I type in 3 - 1 it returns 32, 3 + 3 returns 64. What am I doing wrong?


#import <objc/Object.h>
#import <stdio.h>
//#import "Calculator.h"

@interface Calculator : Object
{
double accumulator;
}

-(void) setAccumulator: (double) value;
-(void) clear;
-(double) getAccumulator;

-(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) getAccumulator
{
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 = accumulator / value;
}
@end

int main(int argc, char *argv[])
{
double num1 = 0, num2 = 0;
char operator;
Calculator *deskCalc = [[Calculator alloc] init];
[deskCalc clear];
printf("Please input an equation: ");
scanf("%1f %c %1f", &num1, &operator, &num2);
[deskCalc setAccumulator: num1];
switch(operator)
{
case '+':
[deskCalc add: num2];
break;
case '-':
[deskCalc subtract: num2];
break;
case '*':
[deskCalc multiply: num2];
break;
case '/':
[deskCalc divide: num2];
break;
default:
break;
}
printf("\n %.2f \n", [deskCalc getAccumulator]);
[deskCalc clear];
[deskCalc free];
return 0;
}



iMeowbot
Jul 1, 2006, 01:28 AM
(okay, see you found it, never mind)