PDA

View Full Version : (Obj-C) odd int value (odd output in general)




iMasterWeb
Sep 1, 2009, 09:44 PM
Hi! Well I'm reading Stephen Kochan's Book: Programming in Objective-C 2.0 and one of the excersizes is to draw a rectangle based on height and width using the | and _ characters, e.g.:
____
| |
| |
| |
____

Here's my code:
@interface
#import <Foundation/Foundation.h>


@interface Rectangle: NSObject {
int height;
int width;
}
@property int height, width;

-(void) draw;
-(void) getMeas;

@end

@implementation
#import "Drawing.h"


@implementation Rectangle

@synthesize height, width;

-(void) getMeas;
{
int usrHeight, usrWidth;
scanf("%i %i", &usrHeight, &usrWidth);
height = usrHeight;
width = usrWidth;
}
-(void) draw {
int c;
for(c = 0; c != height; ++c) {
NSLog(@"%i", &height);
NSLog(@"_");
}
int d;
int e;
do {
NSLog(@"|");
do {
NSLog(@" ");
++d;
} while(d != c);
NSLog(@"|");
++e;
} while(e != height);
c = 0;
do {
NSLog(@"_");
++c;
} while (c != height);
}
@end
main
#import "Drawing.h"

int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
Rectangle *myRect = [[Rectangle alloc] init];

// insert code here...
NSLog(@"Please enter your height and width (in that order)");
[myRect getMeas];
[myRect draw];
[myRect release];
[pool drain];
return 0;
}

And here's the output:
2009-09-01 20:40:46.394 Drawing Application[14128:903] Please enter your height and width (in that order)
4 7
2009-09-01 20:40:53.916 Drawing Application[14128:903] 1068468
2009-09-01 20:40:53.917 Drawing Application[14128:903] _
2009-09-01 20:40:53.918 Drawing Application[14128:903] 1068468
2009-09-01 20:40:53.919 Drawing Application[14128:903] _
2009-09-01 20:40:53.919 Drawing Application[14128:903] 1068468
2009-09-01 20:40:53.920 Drawing Application[14128:903] _
2009-09-01 20:40:53.921 Drawing Application[14128:903] 1068468
2009-09-01 20:40:53.921 Drawing Application[14128:903] _
2009-09-01 20:40:53.922 Drawing Application[14128:903] |
2009-09-01 20:40:53.922 Drawing Application[14128:903]
2009-09-01 20:40:53.922 Drawing Application[14128:903]
2009-09-01 20:40:53.923 Drawing Application[14128:903]
2009-09-01 20:40:53.923 Drawing Application[14128:903]
2009-09-01 20:40:53.924 Drawing Application[14128:903]
2009-09-01 20:40:53.924 Drawing Application[14128:903]
2009-09-01 20:40:53.924 Drawing Application[14128:903]
2009-09-01 20:40:53.925 Drawing Application[14128:903]
2009-09-01 20:40:53.925 Drawing Application[14128:903]
2009-09-01 20:40:53.925 Drawing Application[14128:903]
2009-09-01 20:40:53.926 Drawing Application[14128:903]
...
2009-09-01 20:40:58.332 Drawing Application[14128:903]

And it keeps on going for a LONG time (never tested to see if it DOES end)

Any help greatly appreciated! Thanks!!!

-iMaster



lee1210
Sep 1, 2009, 09:52 PM
&height is the address of height, not its value.

You don't initialize d or e. They're some value, who knows what. Eventually the program will terminate, but it may require an overflow and wrap-around. Just set these to 0 (if this is what you want them to be).

-Lee

EDIT: Also, as has been discussed here before, you can't really do this excercise with NSLog, as you get a line break with each call of NSLog. There are options like building an NSString of the appropriate length containing ---- or | |, etc. but as I understand it this isn't covered by this point in the text.

skochan
Sep 2, 2009, 11:54 AM
@iMaster,

Lee is doing a great job answering your questions! I just wanted to make sure you were aware of the forum dedicated to the book at classroomM.com/objective-c. You'll find others there who have had similar problems/questions that may have been addressed by myself or by other forum members.

You can continue to post here if you like, but I just wanted to make sure you knew about this other resource.

Best of luck,

Steve Kochan

iMasterWeb
Sep 2, 2009, 12:43 PM
Oh cool! I'll go sign up!!