Obj-C Program error

Discussion in 'Mac Programming' started by slooksterPSV, Jun 30, 2006.

  1. macrumors 68030

    slooksterPSV

    Joined:
    Apr 17, 2004
    Location:
    Nowheresville
    #1
    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?

    Code:
    #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;
    }
    
     
  2. macrumors G3

    iMeowbot

    Joined:
    Aug 30, 2003
    #2
    (okay, see you found it, never mind)
     

Share This Page