Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Problem with Objective C

Name: Anonymous 2006-01-11 12:42

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

@interface Calc: Object
{
    double total;
}
//Total methods
-(void) setTotal: (double) value;
-(void) clear;
-(double) total;

//Arithmatic methods
-(void) add: (double) value;
-(void) subtract: (double) value;
-(void) multiply: (double) value;
-(void) divide: (double) value;
@end

@implementation Calc;
-(void) setTotal: (double) value
{
    total = value;
}

-(void) clear
{
    total = 0;
}

-(double) total
{
    return total;
}

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

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

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

-(void) divide: (double) value
{
    total /= value;
}
@end

int main (int argc, char *argv[])
{
    double value1, value2;
    char operator;
    Calc *myCalc = [[Calc alloc] init];
   
    printf ("Type in your expression\n");
    scanf ("%1f %c %1f", &value1, &operator, &value2);
   
    [myCalc setTotal: value1];
    if ( operator == '+' )
        [myCalc add: value2];
    else if ( operator == '-' )
        [myCalc subtract: value2];
    else if ( operator == '*' )
        [myCalc multiply: value2];
    else if ( operator == '/' )
        [myCalc divide: value2];
       
    printf ("%.2f\n", [myCalc total]);
    [myCalc free];
   
    return 0;
}

I've been trying to figure out why it keeps return nonsensical results for the past two days, and I'm sure its something really stupid which I missed. Just learning object based stuff, its pretty cool. Please help :(

Name: Anonymous 2006-01-11 13:14

I refuse to read this until you repost it with the [code] tag.

Name: Anonymous 2006-01-11 13:21

Your scanf is fucked. Use scanf ("%lf %c %lf", &value1, &operator, &value2); if you want to read into doubles.

Name: Anonymous 2006-01-11 13:27

>>3
I love you. :D
Thanks!

Name: test 2009-10-06 18:39

  __     __
 / ☣ \   / ☣  \
 〔(◣◎◢)〕     〔(◣◎◢)〕 < Don't worry ma'am, we're from the 4chan Damage Control Ministry.
 (つ㍍ づ   (つ㍍ づ    
 | | |   | | |     
 (__)_)  (__)_) 

Name: Anonymous 2011-01-31 20:30

<-- check em dubz

Don't change these.
Name: Email:
Entire Thread Thread List