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
  • 314,917
  • 42
  • 532
  • 579

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,849
  • 4
  • 20
  • 22
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,474
  • 36
  • 90
  • 105
Oleg Sobolev
  • 3,286
  • 2
  • 16
  • 29