Objective C帮助(新手)

时间:2013-08-08 23:10:42

标签: objective-c xcode

好的,所以我是目标C的新手并且给自己一些练习我做了一个小小的程序。程序假设你要知道你有多少天。我有这个错误,我无法弄清楚。下面是代码:

Age.h

#import <Foundation/Foundation.h>

@interface Age : NSObject {
int age;
int age365;
int lyear;
}
-(void) howOld;


@end

Age.m

#import "Age.h"

@implementation Age


-(void) howOld{
NSLog(@"How old are you currently?\n");
scanf("age");
NSLog(@"You entered %i", age);



}


@end

Month.h

#import "Age.h"

@interface Month : Age{
int month;
}

-(void) whatMonth;

@end

Month.m

#import "Month.h"

@implementation Month

-(void) whatMonth{

NSLog(@"What month is it? (Number)\n");
scanf("month");
NSLog(@"you entered %i", month);



}


@end

Day.h

#import "Month.h"

@interface Day : Month{
int day;
}
-(void) whatDay;
-(void) printAge;
@end

Day.m

#import "Day.h"

@implementation Day

-(void) whatDay{
NSLog(@"What day is it? (Number)");
scanf("day");
NSLog(@"You entered %i", day);
day = month*30+day+age365;

}

-(void) printAge{
age365 = age * 365;
lyear = age/4;
age = lyear + age365;
NSLog(@"You are %i days old", day);
}

@end

的main.m

#import <Foundation/Foundation.h>
#import "Age.h"
#import "Month.h"
#import "Day.h"

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

    @autoreleasepool {

    Age *a = [[Age alloc]init];

    [a howOld];


    Month *m = [[Month alloc]init];

    [m whatMonth];

    Day *d = [[Day alloc]init];

    [d whatDay];
    [d printAge];


}
return 0;
}

输出 * HERES THE ERROR *

**Lets me type**(
2013-08-08 17:27:26.121 HMDAI[10989:303] How old are you currently?
3)

**Does by itself**(
2013-08-08 17:27:43.854 HMDAI[10989:303] You entered 0
2013-08-08 17:27:43.854 HMDAI[10989:303] What month is it? (Number)
2013-08-08 17:27:43.855 HMDAI[10989:303] you entered 0
2013-08-08 17:27:43.855 HMDAI[10989:303] What day is it? (Number)
2013-08-08 17:27:43.855 HMDAI[10989:303] You entered 0
2013-08-08 17:27:43.856 HMDAI[10989:303] You are 0 days old
)

我做错了什么?如果有帮助,我使用了XCode。

1 个答案:

答案 0 :(得分:1)

scanf表示从标准输入扫描格式化字符串。该函数采用格式字符串和一组指针来扫描内容。

您的代码失败了,因为您没有告诉scanf()它应该扫描一个数字。

维基百科的文章实际上非常有用:http://en.wikipedia.org/wiki/Scanf_format_string

(并且比我们的颈部生活和死亡的典型手册页密集得多)。


请注意,如果您的目标是为了Mac OS X或iOS编程而学习ObjC,那么您已经选择了一个很好但很奇怪的地方。最好从众多如何编写应用程序教程开始。