Ready to Play


#1

Is there an easy way of asking a user to input a number (or a string) which will then be held in a variable?


#2

The best way to get user input is using readline. You’ll need to add libreadline.dylib in the link build phase.

[code]#include <readline/readline.h>

char *input;
input = readline(“Your favorite color?”);
printf(“You like %s!?\n”, input);[/code]

readline is pretty powerful, actually:
cnswww.cns.cwru.edu/php/chet/readline/rltop.html


#3

Cool thankyou


#4

Lost me a bit there, is this somehting I’ll get when I move into objective c. For now I;d like to playt around with C.

When I try:

[code] #include <stdio.h>
#include <readline/readline.h>

int main(int argc, const char * argv[])
{
char *input;
input = readline(“Your favorite color?”);
printf(“You like %s!?\n”, input);

return 0;

} [/code]

The build fails?
I see form the link there is something to download, so maybe readline is not int he standard library??
Where would I store it so my programme can ‘#include’ it?


#5

readline() is defined in the libreadline.dylib, so you need to link in that library. Poke around in Xcode (or its user guide) until you find “Link Binary with Libraries”.


#6

Copy/pasted the code Aaron posted on the 10th. Linked the library (once I discovered add+) in the Link Binary with libraries drop down. Something odd: when I type green for the input, ggrreenn is displayed as I type, but green is displayed after I hit enter. Restarted Xcode and same result. Someone know what’s going on here?


#7

I suspect the debugger is doing something “helpful” for you: echoing what is input to the output of the debugger.

You could run the app from Terminal.app. To figure out where the program is, look at the outline view on the left side of Xcode. Under “Products” you should see your program. Right-click on it to choose “Show In Finder”


#8

workin on it. thanks aaron.