C프로그래밍 질문

조회수 414회

void add_a_purchase(struct purchase *item_slot){

printf("Enter a purchase information\n");
printf(" - Item number (positive integer): ");
scanf("%d",&item_slot->item_no);
getchar();
printf(" - Item name:");
fgets(item_slot->name,40,stdin);
printf(" - Unit price: ");
scanf("%d",&item_slot->price);
printf(" - Amount: ");
scanf("%d",&item_slot->amount);
int length=strlen(item_slot->name);
return;

}

void print_receipt(struct purchase purchases[], int max_len){

int sum=0;
printf("NO     NAME                               AMOUNT PRICE  \n");
printf("------- ------- ------- ------- ------- ------- ------- \n");
for(int i=0;i<max_len;i++){
    printf("%-7d %s %d %d\n", purchases[i].item_no,purchases[i].name,purchases[i].price,purchases[i].amount);
    sum+=purchases[i].price;
}
printf("------- ------- ------- ------- ------- ------- ------- \n");
printf("TOTAL %49d",sum);

}

이 두 코드를 실행하면 print receipt에서 %-7d %s이거까지는 잘 나오는데 이 다음에 자꾸 줄바꿈이 일어납니다ㅠ 왜 그럴까요? 도와주세요ㅠㅠ

  • \n 라인 피드 문자라고 합니다. 빼세요... 정영훈 2019.12.8 21:04

2 답변

  • printf("%-7d %s %d %d\n",

    \n 이걸 빼면 해결 될 듯합니다

    • (•́ ✖ •̀)
      알 수 없는 사용자
  • 제 소견에는 fgets(item_slot->name,40,stdin);하면서 name에 "\n"이 들어가서 printf로 출력할때 행바꾸기가 진행되는것 같군요.

    • (•́ ✖ •̀)
      알 수 없는 사용자

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)