What’s wrong with the code below?

Asked

Viewed 52 times

0

When I clicked on "Run" in the Xcode to run the code and see if everything is all right, I said that everything worked out ("Build Succeeded"), but in the output of the code appears the following:

2015-12-11 17:35:38.289 prog1[36561:1080062] The value of myfraction is: (lldb)

This should not appear? :

2015-12-11 17:35:38.289 prog1[36561:1080062] The value of myfraction is: 1/3.

Follows the code.

#import <Foundation/Foundation.h>

@interface Fraction : NSObject

-(void) print;  
-(void) setnumerator: (int) n;  
-(void) setdenominatior: (int) d;  

@end  

@implementation Fraction  
{  
    int numerator;  
    int denominatior;  
}  
-(void) print  
{  
    NSLog(@"%i/%i", numerator, denominatior);  
}  
-(void) setnumerator:(int) n  
{  
    numerator = n;  
}  
-(void) setdenominatior:(int) d  
{  
    denominatior = d;  
}  
@end


int main(int argc, const char * argv[])  
{
    @autoreleasepool {  
        Fraction *myFraction;  

        myFraction = [Fraction alloc];  
        myFraction = [myFraction init];  

        [myFraction setnumerator: 1];  
        [myFraction setdenominatior: 3];  

        NSLog(@"The value of myfraction is:");  
        [myFraction print];  

    }  
    return 0;
}  
  • The code is correct. Did you by any chance mark that line for debug? Likely to be this...

1 answer

2

To print an integer you should use %d, not %i, as well as C. In this case your print should look like this:

NSLog(@"%d/%d", numerator, denominatior);  

Browser other questions tagged

You are not signed in. Login or sign up in order to post.