What's wrong with this (rather simple) code?

Discussion in 'Mac Programming' started by Doju, Jul 12, 2010.

  1. Doju macrumors 68000

    Joined:
    Jun 16, 2008
    #1
    I'm beginning programming in Obj-C, and the book I'm learning from seems to have some typos here and there and this example code the book gave fails to compile.

    Code:
    // First program example
    
    int main (int argc, const char * argv[])
    {
    	NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    	
    	int value1, value2, sum;
    	
    	value1 = 50;
    	value2 = 25;
    	sum = value1 + value2;
    	
    	NSLog (@”The sum of %i and %i is %i”, value1, value2, sum);
    	
    	[pool drain];
    	return 0;
    }
    Why is that?
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    What is the error? How did you compile? Is that your complete source, with any #includes?

    -Lee
     
  3. Doju thread starter macrumors 68000

    Joined:
    Jun 16, 2008
    #3
    This is my error:

    [​IMG]

    Build and Run in Xcode. Yes, that's the complete source.
     
  4. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #4
    http://developer.apple.com/iphone/l...Foundation_Functions/Reference/reference.html

    NSLog is not built-in/intrinsic. C and Objective-C don't have such a thing (there is a standard library). The link above tells you what framework NSLog is included with. You need to import the appropriate thing to use any function.

    I'm not trying to be obtuse for fun, but it will rob you of a learning opportunity if I tell you exactly what to do.

    -Lee
     
  5. kainjow Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #5
    Actually I think the problem is the quotes:

    Code:
    NSLog (@[COLOR="Red"][B]”[/B][/COLOR]The sum of %i and %i is %i[B][COLOR="Red"]”[/COLOR][/B], value1, value2, sum);
    Those should be the normal " character.
     
  6. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #6
    Which book? Be specific.
     
  7. cromwell64 macrumors regular

    Joined:
    Jun 30, 2008
    #7
    You need to this import statement at the beginning of your code
    Code:
    #import <Foundation/Foundation.h>
    in order for NSLog to be recognized.

    if you have that, then the other problem looks like it is with the smart, or curly, quotes around the string...

    Code:
    ”The sum of %i and %i is %i”
    should be
    Code:
    "The sum of %i and %i is %i"
    with straight double quote marks
     
  8. pilotError macrumors 68020

    pilotError

    Joined:
    Apr 12, 2006
    Location:
    Long Island
    #8
    You have to be careful with cut/paste from PDF's. They seem to sneak those damn fancy quotes in when your not looking!
     
  9. Doju thread starter macrumors 68000

    Joined:
    Jun 16, 2008
    #9
    Oh, thanks guys. Fixing the quotes fixed the problem. Thanks. :D

    [​IMG]

    And the book is "Programming in Objective-C 2.0".
     

Share This Page