deawood
deawood

Reputation: 13

Failing to calculate amount

This C program is to print an invoice, the only problem is that it won't output the unit price and calculate the amount. Can someone show me how. This is now the full code. And if there is any other error, please help.

 struct PRODUCTSINFO {
int code; // products number
int qty;
char name[ 50 ]; // products name
double unit_price; // account unit_price
  }; // end structure PRODUCTSINFO
 struct PRODUCTSINFO products[100] ;

 int main(){
 int p;
int x ;
int i=0;
double amount=0;

printf("Enter the amount of products to be purchased : ");
scanf("%d",&x);

for(i=0;i<x;i++){
    printf("\nEnter product code #%d : ",i+1);
    scanf(" %d",&products[i].code);

    printf("\nEnter product name#%d:",i+1);
    scanf("%s",&products[i].name);

    printf("\nPlease quantity#%d : ",i+1);
    scanf("%d",&products[i].qty);

    printf("Enter unit price#%d:",i+1);
    scanf("%.lf",&products[i].unit_price);

    fflush(stdin);
}
system("cls");
printf("************************INVOICE*******************************\n");
printf("-----------------------------------------------\n);
printf("S/N | CODE |  NAME OF PRODUCTS |  QUANTITY | UNIT PRICE |AMOUNT \n");
printf("------------------------------------------------------\n");

for(i=0;i<x;i++){
    printf("\n%d",i);
    printf("\t  %d",products[p].code);
    printf("\t %s",products[p].name);
    printf("\t\t\t%d",products[p].qty);
    printf("\t\t%.2f",products[p].unit_price);
    p++;  

    amount=products[p].qty*products[p].unit_price;
    printf("\t%.2f\n",amount);
} 
 }

Upvotes: 1

Views: 74

Answers (2)

paulsm4
paulsm4

Reputation: 121759

You haven't shown the complete code.

Is x declared to be an integer (or better,unsigned)?

Are you sure x > 0 when you start the loops?

... AND ...

If you're running the program as a CMD prompt in Windows ... be sure to add getchar() before the end of the program. Otherwise, the program will exit and your window could disappear before you see any output.

ALSO:

Note Weather Vane's suggestion about your scanf formatting errors. Here are two good links:

Upvotes: 0

sreepurna
sreepurna

Reputation: 1666

Your code has lot of syntax errors. I just fixed it. And here is the working code of it. Compare this code with the previous one to realize your mistakes.

struct PRODUCTSINFO {
  int code; // products number
  int qty;
  char name[ 50 ]; // products name
  double unit_price; // account unit_price
}; // end structure PRODUCTSINFO
struct PRODUCTSINFO products[100] ;

int main(){
  int p;
  int x ;
  int i=0;
  double amount=0;
  int total = 0;

  printf("Enter the amount of products to be purchased : ");
  scanf("%d",&x);

  for(i=0;i<x;i++){
    printf("\nEnter product code #%d : ",i+1);
    scanf(" %d",&products[i].code);

    printf("\nEnter product name#%d:",i+1);
    scanf("%s",products[i].name);

    printf("\nPlease quantity#%d : ",i+1);
    scanf("%d",&products[i].qty);

    printf("Enter unit price#%d:",i+1);
    scanf("%lf",&products[i].unit_price);

  }
  printf("************************INVOICE*******************************\n");
  printf("-----------------------------------------------\n");
  printf("S/N | CODE |  NAME OF PRODUCTS |  QUANTITY | UNIT PRICE |AMOUNT \n");
  printf("------------------------------------------------------\n");

  for(i=0;i<x;i++){
    printf("\n%d",i);
    printf("\t  %d",products[i].code);
    printf("\t %s",products[i].name);
    printf("\t\t\t%d",products[i].qty);
    printf("\t\t%.2f",products[i].unit_price);
    p++;

    amount=products[i].qty*products[i].unit_price;
    total += amount;
    printf("\t%.2f\n",amount); 
  }
  Printf("%d",total);
  return 0;
}

Upvotes: 0

Related Questions