Challenge: using readline()

In the challenge at the end of Chapter 16 we’re asked to rewrite the “Coolness” project using NSString and NSLog. But I can’t seem to make it work. I also can’t find the documentation for readline().

        NSLog(@"Who is cool?");
        const char *cn = (const char *)readline(NULL);
        NSString *name = [[NSString alloc] initWithUTF8String:cn];
        NSLog(@"%@ is cool?", name);

The following topics might help.
viewtopic.php?f=139&t=7569
viewtopic.php?f=170&t=7589#p21242

The following worked fine for me.

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

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

    @autoreleasepool {
        
        NSLog(@"Who is cool? ");
        NSString *name = [NSString stringWithUTF8String:readline(NULL)];
        NSLog(@"%@ is cool!", name);
        
    }
    return 0;
}

Also remember to import the libreadline.dylib library as shown in chapter 8 or you will get an error.

thanks to both comments got it working!

Can anyone check my solution ?

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

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

    @autoreleasepool {
        
        NSLog(@"Enter your Number!");
        NSString *readingNum = [NSString stringWithUTF8String:readline(NULL)];
        NSInteger num = [readingNum integerValue];

        for (num = num; num >= 0; num--) {
            if (num % 3 == 0) {
                NSLog(@"found one");
            }
            NSLog(@"%li", num);
        }
        
    }
    return 0;
}

Hello everyone.

Mine is a little different from everyones. I used functions that we really haven’t touched in the book, but I have used them in the past so I used them here.

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

int main(int argc, const char * argv[])
{
@autoreleasepool
{
NSLog(@“Who is cool?\n\n>”);
NSString *coolName =[[NSString alloc]init];
char coolNameArray[17];
scanf("%s",coolNameArray);
coolName = [NSString stringWithUTF8String:coolNameArray];
NSLog(@""%@" is COOL.",coolName);
}

return 0;

}[/code]

The output is:[quote]Who is cool?

Michael
"Michael" is COOL.
Program ended with exit code: 0[/quote]…any comments?

[color=#004080]JR[/color]

[quote] char coolNameArray [17]; scanf ("%s", coolNameArray); [/quote]
Be afraid of scanf and its relatives, very afraid!

Did you read the manual page for scanf, especially the section (%s) for what it does when you ask it to read with %s into a (finite-size) buffer?

Had awful trouble with this one :laughing:

Ended up with this - is it the right idea?

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

int main(int argc, const char * argv[]) {
@autoreleasepool {
NSLog(@“Enter a number”);
NSString *num = [NSString stringWithUTF8String:readline(NULL)];
int number = [num intValue];
while (number > 0 ){
if (number % 3 == 0){
NSLog(@"%d\nFound one!\n", number);
number = number - 3;
} else {
NSLog(@"%d\n", number);
number = number - 3;
}
}

}
return 0;

}
[/code]

Here’s my solution for the challenge:

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

int main(int argc, const char * argv[]) {
@autoreleasepool {
// re-write chapter 8’s readline() challenge using NSString and NSLog

    NSLog(@"Where should I start counting?");
    NSString *startCounting = [NSString stringWithUTF8String:readline(NULL)];
    NSInteger intValue = [startCounting integerValue];
    
    for (long i = intValue; i > 0; i--) {
        NSLog(@"%ld", i);
        if (i % 5 == 0) {
            NSLog(@"Found one!");
        }
    }
}
return 0;

}[/code]

I could not get this to work. I wasn’t exactly sure of the specific challenge: I thought the idea was to convert the C-string to a NSString. But I didn’t know how to make a C-string in Obj-C. So I tried to use the stringWithUTF8String two different ways, with redline() and without. Apparently deadline() isn’t working with XCode6?

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

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        NSLog(@"Who is cool?");
        
        const char *name = "Timmy";
        NSString *newName = [NSString stringWithUTF8String: (name)];
        NSLog(@"\n\n%@ is cool!", newName);
        NSLog(@"\nWho else is cool?");
        NSString *readName = [NSString stringWithUTF8String:readline(NULL)];
        NSLog(@"\n\n%@ is cool!\n", readName);
    }
    return 0;
}

I get the following errors:
"_readline", referenced from:
_main in main.o
Symbol(s) not found for architecture x86_64
Linker command failed with exit code 1

You probably missed a step or two - The book should ask you to add a library (containing the definition of the readline function) to your project.

I’m wondering if this is a depreciation? The documentation doesn’t even mention it. I’ve tried including the various libraries I’ve seen in the other posts with no success, but most of those a from last year, and I think using the older Xcode. The program accepts the formatting of the string, just not the readline() function.

What version of Xcode are you using? The library libreadline.dylib is still available in Xcode 6.2.

//  ReadLine.m

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

int main (int argc, const char * argv[]) {
    @autoreleasepool {
        NSLog (@"--> %s", readline ("Enter some text> "));
    }
    return 0;
}

I’m on Xcode 6.3 This is the code I’m using and still getting the errors:

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

int main(int argc, const char * argv[]) {
@autoreleasepool {
NSLog(@“Who is cool?”);

    const char *name = "Timmy";
    NSString *newName = [NSString stringWithUTF8String: (name)];
    NSLog(@"\n\n%@ is cool!", newName);
    NSLog(@"\nWho else is cool?");
    NSLog(@"Readline test %s", readline("Test"));
    
    //NSString *readName = [NSString stringWithUTF8String:readline(NULL)];
    //NSLog(@"\n\n%@ is cool!\n", readName);
}
return 0;

}
[/code]

Make sure that the library libreadline.dylib still exists, and that you add it to your project.

I know this is a late reply but I just wanted to include it in case people were having the same problem like Haolejon . Make sure to select your project on the left where the project navigator is located at (This is the very most top folder). Then in the work space select Build Phases > collapse Link Binary with Libraries > click the + symbol and add libreadline.dylib .

This should solve those extra errors showing up; took me a few minutes myself to realize I forgot this step. I’m using Xcode 6.4 beta by the way