2011-10-08 88 views
0

我有以下代碼,但無法讓它顯示名稱。 如果我有scanf("%s", inputBuffer);我只得到第一個單詞。它打破空白。所以我將其更改爲scanf("%[\n]", inputBuffer);,但仍然無效。任何幫助,請...Objective C用白色空格讀取字符串

> 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); 

    } 

回答

3

這個工作對我來說:

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