Trouble with Challenge: using readline()


Can someone help with the below please? I seem to be stuck. Thanks in advance!

The way we converted a string into an integer value in chapter 8 was to use the atoi() function but I seem to be getting errors when using this in a Foundation command line tool. The errors I’m seeing on the atoi() line are:

Implicit conversion of an Objective-C pointer to ‘const char *’ is disallowed with ARC
Incompatible pointer types passing ‘NSString *__strong’ to parameter of type ‘const char *’

Here is my code:

#import <Foundation/Foundation.h>
#include <readline/readline.h>

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

@autoreleasepool {
    NSLog(@"What number should I start with? (0-99)");
    NSString *startNum = [NSString stringWithUTF8String:readline(NULL)];
    int num = atoi(startNum);
    for (num; num >= 0; num -= 3)
        printf("%d\n", num);
        if (num % 5 == 0) {
            printf("Found one!\n");
    return 0;


[quote] ... NSString *startNum = [NSString stringWithUTF8String:readline (NULL)]; int num = atoi (startNum); ... [/quote]

#include <stdlib.h>

int atoi (const char str);[/code]
atoi function requires its argument to be of type [b]const char
[/b], but you are providing an argument of type NSString*.

Try this:

const char *startNum = readline (NULL);
int num = atoi (startNum);


Thanks ibex10! That’s how we got it to work on the chapter 8 challenge, but in this challenge they want us to use NSString instead of const char *. I guess I will need to find a way to convert the NSString from using readline into an integer in order to count down from it. I am still a total beginner so I’m sure there is something obvious I am missing. :slight_smile: Thanks for your help, and in the meanwhile I will keep trying.


You can always ask an NSString object (the value of which is a decimal number) to give you its int value:

NSString *startNum = [NSString stringWithUTF8String:readline (NULL)];
int num = [startNum intValue];

NSString Class Reference:

Getting Numeric Values
– doubleValue
– floatValue
– intValue
– integerValue
– longLongValue
– boolValue


Thanks, that did it! I had actually tried something similar earlier, but I just realized my real problem was forgetting to link libreadline.dylib like we had done in Ch. 8. Anyway, I’m all good now and thanks again for the help! I really appreciate it. :smiley: