Two things I can see going wrong here:
1. "j = -j;" flips j from positive to negative or vice versa. For example:
int j = 154;
NSLog(@"%i", j); // prints 154
j = -j; // flips j from being positive to negative
NSLog(@"%i", j); // prints -154
j = -j; // j is currently negative so it flips back to positive
NSLog(@"%i", j); // prints 154 again
So what is happening is the user types in a negative number, that is obviously less than zero, so your loop enters and turns it into a positive number, this is then clearly greater than zero so the condition is no longer met and your loop exits. If the user enters a positive number, your loop is completely ignored. You might as well have written:
if (j < 0)
j= -j;
If you were intending for j to count down then:
j = j -1; //reduce j by 1
j = j -2; //reduce j by 2
j--; //reduce j by 1 in shorthand
j -= 3; //reduce j by any number in shorthand;
2. Second problem you got going on here is the bit of code here:
int j;
NSLog(@"Type in a number:");
scanf("%i",&j);
The code itself is fine but were its located is not. The problem is that it is not within a function or method that is being called, and therefore its not being run at all. For the purpose of this program I would recommend you just put it in main for the moment like this:
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int j;
NSLog(@"Type in a number:");
scanf("%i",&j);
while (j < 0) {
j = -j;
}
[pool drain];
return 0;
}
The last thing I would suggest is that you might want to print some output at some point so you know if your program has done anything.
#import <Foundation/Foundation.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
int j;
NSLog(@"Type in a number:");
scanf("%i",&j);
while (j > 0) {
NSLog(@"Count: %i", j);
j--;
}
[pool drain];
return 0;
}
Hope this helps