C Programming Questions

Asked 1 years ago, Updated 1 years ago, 85 views

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

2022-09-21 15:21

2 Answers

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

\nIf we take this out, we'll figure it out


2022-09-21 15:21

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.


2022-09-21 15:21

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.