Amazon.com Widgets Whats Wrong with while....
Welcome, Guest. Please login or register.
Did you miss your activation email?
September 01, 2014, 09:17:20 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 5 - Program Looping
| | | |-+ Whats Wrong with while....
Pages: [1] Go Down
Print
Author Topic: Whats Wrong with while.... (Read 1467 times)
macifone
Newbie
*
Posts: 14


Email




on: October 31, 2009, 05:31:56 AM

I get an error when I d this...
Code: (Objective-C)
#import <Foundation/Foundation.h>
int j;

NSLog(@"Type in a number:");
scanf("%i",&j);

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

while (j < 0) {
j = -j;
}



    [pool drain];
    return 0;
}

Whats wrong with this?
Logged
toby
Full Member
***
Posts: 104






Reply #1 on: October 31, 2009, 06:17:48 AM

Two things I can see going wrong here:

1. "j = -j;" flips j from positive to negative or vice versa. For example:
Code: (Objective-C)
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:
Code: (Objective-C)
if (j < 0)
   j= -j;

If you were intending for j to count down then:
Code: (Objective-C)
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:
Code: (Objective-C)
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:
Code: (Objective-C)
#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. 
Code: (Objective-C)
 
#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
Logged
macifone
Newbie
*
Posts: 14


Email




Reply #2 on: October 31, 2009, 07:26:30 AM

Thank you the part where you said to put the statements in the for function helped.
Logged
toby
Full Member
***
Posts: 104






Reply #3 on: October 31, 2009, 07:36:05 AM

no worries Smiley
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.