2011-10-09 3 views
1

나는 DONE에서 이제 르 강 유형 때까지 루프에이 코드를하려합니다. while 루프를 사용했지만 embedded while 루프의 일부만 실행됩니다. 다음 프로그램에서 첫 번째 프롬프트 (이름 입력)가 실행되지 않는 이유는 무엇입니까? 감사은 목표 C 루프

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

    NSNumberFormatter *price = [[NSNumberFormatter alloc] init]; 
    [price setNumberStyle:NSNumberFormatterCurrencyStyle]; 
    [price setCurrencySymbol:@"$"]; 

    while(i > 0){ 
     NSLog (@"Enter the name:"); 
     scanf("%[^\n]", inputBuffer); 
     name = [[NSString alloc] initWithUTF8String:inputBuffer]; 

     if([name isEqualToString:@"DONE"]) 
      break; 
     else{ 
      NSLog (@"Enter the total number of hours: "); 
      scanf ("%lf", &hours); 

      NSLog (@"Enter the pay rate: "); 
      scanf ("%lf", &payRate); 

      if(hours <= 40) 
       totalPay = hours * payRate; 
      else 
       totalPay = 400 + (payRate * (hours - 40) * 1.5); 

      NSString *myString = [NSString stringWithFormat:@"%f",totalPay]; 

      NSNumber *myNumber = [NSNumber numberWithDouble:[myString doubleValue]]; 
      amount = [price stringFromNumber:myNumber]; 

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

      NSLog(@"\n"); 
     } 

    } 

    NSLog (@"DONE!"); 

    } 
+0

의미? – zaph

답변

2

귀하의 문제는 버퍼링 문제의 번호와 같은 scanf() ING 번호를 기부 할 수 있지만 다음 줄 피드. 당신은 또한 printf() 또는 scanf()를 사용하는 경우이의 위에, 나는 때때로 표준 입력/표준 출력 버퍼링의 NSLog() 놨을 찾을 수 있습니다. 대개 가능한 한 항상 scanf() 함수를 피하는 것이 가장 좋습니다. 특히 Objective-C와 같은 상위 수준 언어를 사용하는 경우 더욱 그렇습니다.

사용자 입력을 읽는 데 scanf()을 사용하는 대신 콘솔에서 행을 읽고 작은 숫자를 입력하여 NSString으로 반환합니다. 이 함수는 다음과 같이 보입니다 :

NSString * readLine (FILE * input) { 
    NSMutableString * string = [[NSMutableString alloc] init]; 
    int aChar = 0; 
    while ((aChar = fgetc(input)) != EOF) { 
     if (aChar != '\r') { 
      if (aChar == '\n') { 
       break; 
      } else if (aChar > 0) { 
       [string appendFormat:@"%C", aChar]; 
      } 
     } 
    } 
    return [string autorelease]; 
} 

이것을 사용하면 새로운 방법을 사용하여 main() 기능을 다시 작성할 수 있습니다. 다음 코드에서 나는 또한 더 적절한 printf() 호출로 대체 정보를 사용자에게 메시지를 모든 NSLog() 문을 가지고있다. "실행되지"무엇

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

    NSNumberFormatter * price = [[NSNumberFormatter alloc] init]; 
    [price setNumberStyle:NSNumberFormatterCurrencyStyle]; 
    [price setCurrencySymbol:@"$"]; 

    while (YES) { 
     printf("Please, enter your name: "); 
     name = readLine(stdin); 

     if ([name isEqualToString:@"DONE"]) 
      break; 
     else { 
      printf("Enter the total number of hours: "); 
      hours = [readLine(stdin) intValue]; 

      printf("Enter the pay rate: "); 
      payRate = [readLine(stdin) intValue]; 

      if (hours <= 40) 
       totalPay = hours * payRate; 
      else 
       totalPay = 400 + (payRate * (hours - 40) * 1.5); 

      NSNumber * myNumber = [NSNumber numberWithDouble:totalPay]; 
      amount = [price stringFromNumber:myNumber]; 

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

    [price release]; 
    NSLog(@"DONE!"); 

    [pool drain]; 
} 
1

: 사용자로부터 아무것도 읽지 않습니다 scanf("%[^\n]", inputBuffer);, 당신은 inputBuffer에 사용자로부터 읽을 필요가있다.

또한 NSLog 당신이 "C"프로그램을 작성하기 때문에 아마 "C"함수를 사용하여, 사용자에게 텍스트를 보낼 수있는 좋은 방법이 아니다.