0

I've just started on xcode today, so please excuse the total noob question here.

My code is:

NSString *name = @"";
int age = 0;

NSLog(@"What is your name?");
scanf("%s", &name);
NSLog(@"How old are you?");
scanf("%d", &age);


NSLog(@"Your name is %@ and you are %d", name, age);

And the output I get is:

Your name is 2001-01-01 00:00:00 +0000 and you are 29

As you can see, the string is not printing as expected. Please help???

Thank you in advance!

rmaddy
  • 307,833
  • 40
  • 508
  • 550

3 Answers3

2

Scanf doesn't work with objects. Here's a SO answer that shows how to read data and put it into a string.

Community
  • 1
  • 1
Mark S.
  • 3,727
  • 3
  • 21
  • 20
1

scanf does not work with any object types. If you have a C string and want to create an NSString from it, use -[NSString initWithBytes:length:encoding:].

Shen Hutah
  • 227
  • 2
  • 14
0

Use

[NSString stringWithUTF8String:(const char *)];

demongolem
  • 9,148
  • 36
  • 86
  • 104
Oleg Sobolev
  • 3,226
  • 2
  • 15
  • 28