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

kingv84

macrumors newbie
Original poster
Apr 19, 2010
19
0
Wiesbaden, Germany
Am trying to add subtraction function to my code below. Am learning how to code and I am stumped in a way to integrate addition and subtraction. Thanks for any help.


Code:
#import <Foundation/Foundation.h>


//---- @interface section----

@interface SimpleCalc: NSObject

{
    int number1;

    int number2;
}

-(void) setNumber1: (int) n1;
-(void) setnumber2: (int) n2;
-(int) addition;

@end


//---- @implementation section----

@implementation SimpleCalc
-(void) setNumber1:(int)n1

{
    number1 = n1;
}

-(void) setnumber2:(int)n2

{
    number2 = n2;
}

-(int) addition

{
    return number1+number2;
}
@end


//----program section----

int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

//----addition functionality----

SimpleCalc *myCalc;

myCalc = [SimpleCalc alloc];
myCalc = [myCalc init];
[myCalc setNumber1:30];
[myCalc setnumber2:30];

//message

NSLog (@"The Addition of two numbers= %i", [myCalc addition]);

       [pool drain];
       return 0;
       }
 
I get the following output:

2012-06-18 15:56:08.590 main[9707:707] The Addition of two numbers= 60

What are you getting? What are you expecting?
 
Thanks for the reply. Am trying to do the same thing, but with subtraction. Example- The Subtraction of two numbers = 0

I know how to make it subtract, but I want to also add subtraction to the code so the output will display the answer for addition and subtraction.



I get the following output:

2012-06-18 15:56:08.590 main[9707:707] The Addition of two numbers= 60

What are you getting? What are you expecting?
 
Add a method named subtraction. Then call it and print the result. It should work like this:
Code:
NSLog (@"The Subtraction of two numbers= %i", [myCalc subtraction]);

If you're asking "How can I write a single method that returns both the sum (addition) and difference (subtraction) of the two numbers?", the short answer is "You can't.". There are more complicated ways of returning a single object with multiple members, but they're too complex for where you're at right now, in my opinion.

Learn to make simple methods that return simple results for simple things. Learn complex methods for complex things later. Walk first, then run, then pole-vault.
 
Thank You everyone. My problem is solved.:)

Code:
#import <Foundation/Foundation.h>


//---- @interface section----

@interface SimpleCalc: NSObject

{
    int number1;

    int number2;
}

-(void) setNumber1: (int) n1;
-(void) setnumber2: (int) n2;
-(int) addition;
-(int) subtraction;

@end


//---- @implementation section----

@implementation SimpleCalc
-(void) setNumber1:(int)n1

{
    number1 = n1;
}

-(void) setnumber2:(int)n2

{
    number2 = n2;
}

-(int) addition

{
    return number1+number2;
}

-(int) subtraction

{
    return number1-number2;
}
@end


//----program section----

int main (int argc, char *argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

//----addition functionality----

SimpleCalc *myCalc;

myCalc = [SimpleCalc alloc];
myCalc = [myCalc init];
[myCalc setNumber1:30];
[myCalc setnumber2:30];
    
//----subtraction----

    SimpleCalc *myCalcS;
    
    myCalcS = [SimpleCalc alloc];
    myCalcS = [myCalcS init];
    [myCalcS setNumber1:30];
    [myCalcS setnumber2:30];
     

//message

    NSLog (@"The Addition of two numbers= %i and the Subtraction of two numbers=%i", [myCalc addition],[myCalcS subtraction]);
    

       [pool drain];
       return 0;
       }
 
Nicely done, although it's not necessary to alloc and init a second SimpleCalc object. Since both objects are given the same numbers (30 and 30), the same object can be used to give the result of addition and subtraction.

If you were giving them different numbers, even then, a single object would suffice:
1. Make the object.
2. Set two numbers for addition.
3. Show the result of addition.
4. Set two numbers for subtraction.
5. Show the result of subtraction.

At steps 3 and 5, you could also get each result and store it in a separate int variable, then show both values later.

Finally, since you're not releasing either SimpleCalc object you alloc'ed, that means you're leaking them both. It's unimportant in such a simple program, but you will eventually need to understand how memory management works. I mention this mainly for completeness and for future reference.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.