Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
June 26, 2017, 06:58:20 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register Chat  
Pages: [1]   Go Down
  Print  
Author Topic: Program 5.4 - Question  (Read 10727 times)
MCaEd
Newbie
*
Posts: 49


« on: September 01, 2011, 02:41:33 AM »

Hi,
Can anyone, please, explain why do you use \n in the following (I know that \n is a new line, however I don't understand the meaning of it at this place):

#import <Foundation/Foundation.h>
int main (int argc, char *argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int n, number, triangularNumber;
   
    NSLog (@"What triangular number do you want?");
    scanf ("%i", &number);
   
    triangularNumber = 0;
   
    for ( n = 1; n <= number; ++n )
        triangularNumber += n;
   
    NSLog (@"Triangular number %i is %i\n", number, triangularNumber);
   
    [pool drain];
    return 0;
}

Thanks
Logged
dharr19
Full Member
***
Posts: 175


Email
« Reply #1 on: September 01, 2011, 10:12:56 AM »

Hi,

NSLog (@"Triangular number %i is %i\n", number, triangularNumber);

During each repetition of the loop each line of output is printed followed by a newline.  To do that you want to place the newline after the last %i so that the entire line is printed followed by '\n'.

Triangular number is 5 and triangularNumber is 10 // followed by a newline then the next line will start
Triangular number is 8 and triangularNumber is 15 // followed by a newline then the next line will start

I just made up the numbers for completeness but you get the idea.

Cheers
David
Logged
MCaEd
Newbie
*
Posts: 49


« Reply #2 on: September 01, 2011, 12:37:15 PM »

David,
If you look at the program 5.5 in the book you will see that there is no \n and each line of output is displayed on a new line.
But I got your idea; and thanks for the reply.
Logged
seerex
Full Member
***
Posts: 177


Email
« Reply #3 on: September 03, 2011, 09:10:37 AM »

It's not needed. I believe it to be a typo. Every time you call NSLog, it will be displayed on a new line. However, if you were using printf to make the printing, you would need the \n at the end, to make sure the next printf came at a new line.

so simply put, i BELIVE it is an error, as leaving it out makes no difference. NSLog will automatically create that extra line, and making that \n doesn't give an "empty line of space" since it can't do that unless such a call is made
NSLog (@" ");

So don't worry Smiley
Logged

Best Regards -
J.B.J
If you think i helped you could return the favor Wink

Subscribe to my channel on youtube: http://www.youtube.com/user/JBJProgramming

My apps:
- iTap Fruits (all countries)
- Toilet-Quiz (Denmark only)
- Zoo Kids (Denmark only)
- Diablo Item Database (All countries)
dharr19
Full Member
***
Posts: 175


Email
« Reply #4 on: September 03, 2011, 09:24:09 AM »

Thanks Seerex Smiley,

Great call. Your right it is an error.  But now I'm wondering what if you did want the output from two NSLog statements to be on the same line  Huh

Cheers
David
Logged
RobertKr
Newbie
*
Posts: 1

327637878 roberttib@outlook.com
« Reply #5 on: December 27, 2014, 12:24:39 PM »

Who knows if the call was really from Wyndham.  I would think that a substantial change in a program would be on the website.
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  

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 ゥ 2009 classroomM.com. All rights reserved.