Gah! I accidentally deleted a topic


#1

So, I moved a question about the NSString challenge (which was done right) from the general 2nd edition board to this subforum, and then accidentally deleted it. I’m sorry!

If this was yours, please feel free to re-post it!

~Mikey


#2

Hi Mikey,

It was mine.

I had the following as the answer to the readline() challenge for chapter 16 and it works but it is technically a miss-mash of C (because it uses atoi) and Obj-C.

[code]#import <Foundation/Foundation.h>
#import <readline/readline.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {

    NSLog(@"What number would you like to start with?");
    //now countdown by 3 and find numbers that are evenly divisible by 5
    const char *startingNumberChar = readline(NULL);
    NSString *startingNumberNum = [NSString stringWithUTF8String:startingNumberChar];
    NSLog(@"You entered %@",startingNumberNum);
    NSInteger i = 0;
    for (i=atoi(startingNumberChar); i >= 0; i-=3) {
        NSLog(@"i = %ld",i);
        if (i % 5 == 0) {
            NSLog(@"Found one! i=%ld",i);
        }
    }
    
}
return 0;

}
[/code]

I had asked about using integerValue instead of atoi() but kept getting undefined variables for it. It turns out that I had the syntax incorrect.

I now have:

[code]#import <Foundation/Foundation.h>
#import <readline/readline.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {

    NSLog(@"What number would you like to start with?");
    //now countdown by 3 and find numbers that are evenly divisible by 5
    const char *startingNumberChar = readline(NULL);
    NSString *startingNumberNum = [NSString stringWithUTF8String:startingNumberChar];
    NSLog(@"You entered %@",startingNumberNum);
   // NSInteger i = 0;
    for (NSInteger i = [startingNumberNum integerValue]; i >= 0; i-=3) {
        NSLog(@"i = %ld",i);
        if (i % 5 == 0) {
            NSLog(@"Found one! i=%ld",i);
        }
    }
    
}
return 0;

}

[/code]

And it works fine now.

–Harold