这个C程序是打印发票,唯一的问题是它不会输出单价和计算金额。有人能告诉我怎么做吗。这是完整的代码。如果有其他错误,请帮忙。

 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);
}
 }

最佳答案

你的代码有很多语法错误。我刚修好。这是它的工作代码。
将此代码与前一个代码进行比较,以了解您的错误。

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;
}

关于c - 无法计算金额,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42459541/

10-14 06:08