Amazon.com Widgets need help with ex 5 chapter 7
Welcome, Guest. Please login or register.
Did you miss your activation email?
November 23, 2014, 07:21:23 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
|-+ Programming in Objective-C, 4th edition
| |-+ Exercises
| | |-+ Chapter 7
| | | |-+ need help with ex 5 chapter 7
Pages: [1] Go Down
Print
Author Topic: need help with ex 5 chapter 7 (Read 725 times)
stan4ev
Newbie
*
Posts: 7


Email




on: February 23, 2012, 11:22:45 AM

Hi All,
This is my code for exercise 5 after I have done ex 6!
I have problem with the add method or the main.m
when I run the program it allows me to put the first complex number and when I press enter it does not let me put the second and this is what comes up:


2012-02-23 18:08:01.769 chapter7ex5[817:707] enter the first complex number:
6 + 6i
2012-02-23 18:08:09.652 chapter7ex5[817:707] enter the second complex number:
2012-02-23 18:08:09.652 chapter7ex5[817:707] 0.000000 + 0.000000i
2012-02-23 18:08:09.653 chapter7ex5[817:707]  +
2012-02-23 18:08:09.653 chapter7ex5[817:707] 0.000000 + 0.000000i
2012-02-23 18:08:09.654 chapter7ex5[817:707]  =
2012-02-23 18:08:09.654 chapter7ex5[817:707] 0.000000 + 0.000000i

below is my code please have a look and help! thanks!

main.m

#import "Complex.h"

int main (int argc , char * argv[])
{
    @autoreleasepool {
       
        double r1, i1, r2, i2;
       
       
        NSLog(@"enter the first complex number:");
        scanf("(%f+%f)", &r1, &i1);
       
        NSLog(@"enter the second complex number:");
        scanf("(%f+%f)", &r2, &i2);
       
       
        Complex *complexNum1 = [[Complex alloc]init];
        Complex *complexNum2 = [[Complex alloc]init];
       
        Complex *complexResult;
       
       
        [complexNum1 setReal:r1 setImaginary:i1];
        [complexNum2 setReal:r2 setImaginary:i2];
       
       
       
        [complexNum1 print];
        NSLog(@" + ");
        [complexNum2 print];
        NSLog(@" = ");
       
        complexResult = [complexNum1 add:complexNum2];
       [complexResult print];
       
    }
    return 0;
}

Complex.h

#import <Foundation/Foundation.h>

@interface Complex : NSObject

@property double real, imaginary;

-(void) setReal:(double)r setImaginary:(double) i;

-(void) print; // display as a + bi

-(Complex *) add: (Complex *) complexNum;

@end

Complex.m

#import "Complex.h"

@implementation Complex

@synthesize real, imaginary;
/*
-(void) setReal:(double)a
{
    real = a;
}

-(void) setImaginary:(double)b
{
    imaginary = b;
}
*/
-(void) print
{
    NSLog(@"%f + %fi" , real, imaginary);
}

/*-(double) real
{
    return real;
}

-(double) imaginary
{
    return imaginary;
}
*/
-(void) setReal:(double)r setImaginary:(double)i
{
    real = r;
    imaginary = i;
}

-(Complex *) add:(Complex *)complexNum
{
    //this will store the complex num after adding
    Complex *myComplexNum = [[Complex alloc]init];
   
   
    [myComplexNum setReal:real + complexNum.real
                  setImaginary:imaginary + complexNum.imaginary];
   
    return myComplexNum;

}

@end

Logged
apexmutt
Newbie
*
Posts: 12


Email




Reply #1 on: February 23, 2012, 07:55:47 PM

Just tried an imitation of your program with the same results.

For the scanf statement, use %lf for the string specification when dealing with the data type double.

Second, get rid of the '+' in the scanf string.
Logged
stan4ev
Newbie
*
Posts: 7


Email




Reply #2 on: February 24, 2012, 02:56:14 PM

i did what you told me but it still not letting me put the second number in after i put the first it goes straight to adding the numbers and the second is shown as 0 + 0i
Huh?
Logged
apexmutt
Newbie
*
Posts: 12


Email




Reply #3 on: February 24, 2012, 04:56:03 PM

Hah, sorry I completely missed the parenthesis in the scanf. Take those out as well and you'll be set.

Out of curiosity, is there any reason you chose to put those in there?
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.