Amazon.com Widgets my first try on objective -c can any one help my
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 21, 2014, 04:55:45 AM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Old Stuff
| |-+ Newbie Corner
| | |-+ my first try on objective -c can any one help my
Pages: [1] Go Down
Print
Author Topic: my first try on objective -c can any one help my (Read 985 times)
ahmed eltaher
Newbie
*
Posts: 1


Email




on: February 25, 2012, 12:49:52 PM

i write my first try in objective -c  ,  my project  add two numbers take from user number 1 & number 2
and return the result
but my result always of adding  = 0
this my error  what ever i change numbeer1 , number2  , the result value =0 , why can any one help me?
look at my code and help me plz .


i write a class called calc
---------------------------------------------------------------
calc.h
Code: (Objective-C)
#import <Foundation/Foundation.h>


@interface Calc : NSObject {
    @public
    double value1 , value2 , value3 ,result ;
    @private
   
}
-(double) add_two_numbers : (double) X : (double) Y ;

@end
----------------------------------------------------------------
calc.m
Code: (Objective-C)
#import "Calc.h"


@implementation Calc

-(double)add_two_numbers:(double)X :(double)Y
{
    return (X + Y);
   
}






- (id)init
{
    self = [super init];
    if (self) {
        // Initialization code here.
    }
   
    return self;
}

- (void)dealloc
{
    [super dealloc];
}

@end

----------------------------------------------------------------
main.m
Code: (Objective-C)
#import <Foundation/Foundation.h>
#import "Calc.h"

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

    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    Calc * mycalc =[[Calc alloc]init];
    double x,y;
    NSLog(@"Enter the  X value : ");
    scanf("%f",&x);
    NSLog(@"Enter the  Y value : ");
    scanf("%f",&y);
    double z =100.0;
    z=[mycalc add_two_numbers:x :y];
    NSLog(@"Hello, World!");
    NSLog(@"theresult = %f",z);

    // insert code here...
   
    [ mycalc release];

    [pool drain];
    return 0;
}
Logged
Cryptic57
Newbie
*
Posts: 1


Email




Reply #1 on: April 01, 2012, 02:17:47 PM

Greetings. I think for your first try you have been a bit ambitious but that's for you to decide. Try the following which is a stand alone with no interface or implementation. Hope it helps.



#import <Foundation/Foundation.h>


int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   
   
   double value1, value2, accumulator;
   char operator;
   
   NSLog(@"Please enter 2 numbers to be added together");
   scanf(" %lf  %c  %lf", &value1, &operator, &value2);
   
   
   accumulator = value1 + value2;
   
   NSLog(@" result is %lf", accumulator);
   
   
   
   
    [pool drain];
    return 0;
}
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.