0

Sorry to necro an old issue but (I've searched high and low for an answer to this) I noticed that the solution below does not work if the user enters spaces and multiple words. Surely a character array can accept spaces? If the user types a sentence, it only captures the first word:

char name[100]; // declaring char array
NSString *firstName; // declaring the NSString

printf("Please enter first name \n");
printf("=> ");
scanf("%s", name);

firstName = [NSString stringWithCString:name encoding:NSASCIIStringEncoding];
  • 2
    The problem is not with C-style strings but with `scanf()`. – mouviciel Nov 30 '13 at 21:51
  • 5
    Well, actually, the problem is not with scanf, but with the failure to read the doc for scanf. – Hot Licks Nov 30 '13 at 22:08
  • Zaph: I'm not sure I understand you. Were you talking to me? This code is complete, aside from the #import, int main, @autoreleasepool, etc. mouviciel: So what do you suggest? I mean I've used scanf("%s", &name); but the & sign in there causes Xcode to complain about it having a pointer or something. – user3020447 Dec 01 '13 at 01:39
  • A complete example would show the inputs, outputs, and any error messages. – Hot Licks Dec 01 '13 at 03:54
  • And, of course, before you post anything read the documentation on interfaces you're trying to use. We're not here to read to you. – Hot Licks Dec 01 '13 at 03:55

1 Answers1

2

This is a duplicate of this question:

How do you allow spaces to be entered using scanf?

The Objective-C code is not relevant.

Community
  • 1
  • 1