Amazon.com Widgets How to print out result on same line
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 25, 2013, 08:15:59 PM
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
| |-+  Chapter Study
| | |-+  Chapter 2 - Programming in Objective-C
| | | |-+  How to print out result on same line
Pages: [1]   Go Down
Print
Author Topic: How to print out result on same line  (Read 1476 times)
antony
Newbie
*
Posts: 4






« on: March 10, 2010, 06:29:32 PM »

here is code and console view.
Question: if i want to put print out on one line instead of two how do i do that?
so like this "the 1st fraction is 1/3"
instead of
"the 1st fraction is
1/3"


heres code

@interface Fraction : NSObject   
{
   int numerator;
   int denominator;
}

//method of class
-(void) print;
-(void) setNumerator:(int) n;
-(void) setDenominator:(int) d;
@end


@implementation Fraction
-(void) print
{
   NSLog(@"%i/%i", numerator,denominator);
   
}

-(void) setNumerator:(int) n
{
   numerator = n;
   
}
-(void) setDenominator:(int) d
{
   denominator = d;
}



@end



#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
   //Fraction *myFraction;
   //myFraction = [Fraction new];
   
   
   Fraction *myFrac1 = [[ Fraction alloc] init];
   Fraction *myFrac2 =[[ Fraction alloc] init];
   
   
   // set fraction to 1/3
   
   [myFrac1 setNumerator: 1];
   [myFrac1 setDenominator: 3];
   
   [myFrac2 setNumerator: 4];
   [myFrac2 setDenominator: 8];
   
   // display fraction
   
   
    NSLog(@"the 1st fraction is:");
   [myFrac1 print];
   [myFrac1 release];
   NSLog(@"the 2nd fraction is:");
   [myFrac2 print];
   [myFrac2 release];
    [pool drain];
    return 0;
}

Here's console
Loading program into debugger…
Program loaded.
run
[Switching to process 2671]
Running…
2010-03-11 12:19:29.764 interface build[2671:a0f] the 1st fraction is:
2010-03-11 12:19:29.767 interface build[2671:a0f] 1/3
2010-03-11 12:19:29.768 interface build[2671:a0f] the 2nd fraction is:
2010-03-11 12:19:29.769 interface build[2671:a0f] 4/8
Logged
tadej5553
Full Member
***
Posts: 145


Email




« Reply #1 on: March 10, 2010, 10:45:56 PM »

Well, there are two ways:

1. Without using the print method. But to do this, you would have to add numerator and denominator methods to the fraction class.

Code: (Objective-C)
NSLog(@"the 1st fraction is: %i/%i", [myFrac1 numerator], [myFrac1 denominator );

2. Using printf instead of NSLog. The printf is a C function, that is basicly the same as NSLog, but it doesn't produce a line break at the end. If you do this you would have to modify the program like this:
Code: (Objective-C)
@interface Fraction : NSObject   
{
    int numerator;
    int denominator;
}

//method of class
-(void) print;
-(void) setNumerator:(int) n;
-(void) setDenominator:(int) d;
@end


@implementation Fraction
-(void) print
{
    printf("%d/%d \n", numerator,denominator);
   
}

-(void) setNumerator:(int) n
{
    numerator = n;
   
}
-(void) setDenominator:(int) d
{
    denominator = d;
}



@end



#import <Foundation/Foundation.h>

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    //Fraction *myFraction;
    //myFraction = [Fraction new];
   
   
    Fraction *myFrac1 = [[ Fraction alloc] init];
    Fraction *myFrac2 =[[ Fraction alloc] init];
   
   
    // set fraction to 1/3
   
    [myFrac1 setNumerator: 1];
    [myFrac1 setDenominator: 3];
   
    [myFrac2 setNumerator: 4];
    [myFrac2 setDenominator: 8];
   
    // display fraction
   
   
    printf("the 1st fraction is:");
    [myFrac1 print];
    [myFrac1 release];
    printf("the 2nd fraction is:");
    [myFrac2 print];
    [myFrac2 release];
    [pool drain];
    return 0;
}

Notice that there isn't a @ sign before the string anymore? That's because we're not dealing with Obj-C, but with C strings now.
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.