Objective C使用空格读取字符串

时间:2011-10-08 21:03:30

标签: objective-c whitespace scanf

我有以下代码,但无法让它显示名称。 如果我有scanf("%s", inputBuffer);我只得到第一个字。它打破了空白。所以我将其更改为scanf("%[\n]", inputBuffer);但仍然无效。任何帮助请...

> int main (int argc, char *argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int number;
    double payRate, hours, totalPay;
    NSString *name;
    char inputBuffer[200];


    NSLog (@"Enter the number of entries to be processed: ");
    scanf ("%i", &number);

    for(int i = 1; i <= number; i++){
        NSLog (@"Enter the name:");
        scanf("%[\n]", inputBuffer);
        name = [[NSString alloc] initWithUTF8String:inputBuffer];

        NSLog(@"Name: %@", name);
        NSLog(@"Hours:%.2lf", hours);
        NSLog(@"Pay Rate:%.2lf",payRate);
        NSLog(@"Total Pay:%.2lf", totalPay);

    }

1 个答案:

答案 0 :(得分:3)

这对我有用:

scanf("%[^\n]", inputBuffer);