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);
}
When I run these two codes, print receipt %-7d%s is good, but then I keep changing the line. Why is that? Help me
line-break
printf("%-7d %s %d %d\n",
\nIf we take this out, we'll figure it out
In my opinion, fgets(item_slot->name,40,stdin); then the name goes "\n" and then the line changes when printing it out as printf.
© 2024 OneMinuteCode. All rights reserved.